2
\$\begingroup\$

My question is fairly simple but maybe hard to answer. I'm currently building my own game server and I've done to login server now. So, on to the actual game server. I have a question for this server though. Say, a thousand players are moving around on this server, how do I manage this with the database (MySQL) that should hold the player positions and such?

I understand I can't query the database every time something small changes or a single player moves because that would be a huge load for that server. How is this done in real game servers? When do they read or write to the database?

\$\endgroup\$
  • 1
    \$\begingroup\$ Why should the db be updating their positions? That should be kept in ram until they logout or disconnect. DB should be for persistent data but you should cache as much as you can into ram. \$\endgroup\$ – jmasterx Jan 4 '14 at 14:12
4
\$\begingroup\$

Database access is always an expensive operation. For that reason it should be minimized as much as possible.

In an ideal case, you would read and write the player data once per session: When the client connects you read it, and when the client disconnects, you write the changed data back. In the meantime it should be kept in the memory of the gameserver.

But in practice you can never be sure that your gameserver never crashes, so you should do regular backup saves to the database so players don't lose too much progress when your network admin stumbles over the servers power cord. Once every few minutes is usually enough.

\$\endgroup\$
  • 1
    \$\begingroup\$ The basic rule for periodic saving of transient data like positions is "how many pissed players do I want to deal with when they lose N number of minutes of gameplay?" The basic rule for permanent data like items gained or big choices made is the same except replace minutes with seconds =) Many recent games seem to fall on the 5 minute mark, from personal experience with crash roll-backs and reloading to play again. \$\endgroup\$ – Patrick Hughes Jan 4 '14 at 22:12
  • \$\begingroup\$ Phillipp is there a design where I can avoid rollbacks if the app crashes, with lets say 2000 players online, and not kill the database? Any link you could give would be nice. I believe a player losing a single item is bad. \$\endgroup\$ – Gam Apr 24 '16 at 17:14
  • \$\begingroup\$ @Phantom When you are so worried about the impact of a rollback of just a few minutes of game time, then you seem to expect this to happen very frequently. In that case you might want to work on the stability of your server. \$\endgroup\$ – Philipp Apr 24 '16 at 17:30
  • \$\begingroup\$ @Philipp It's not like that. Even if the server "dies" once in a year, and those "few minutes of lost game time" means people lost items that are worth 300$ (some are), that's freaking bad. A server's source code being handled by multiple persons, it's hard to make sure all of them do not mess it up. \$\endgroup\$ – Gam Apr 24 '16 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.