I started an MMORPG game, and I can already make a character move around the map and see other characters. But I have doubts about my architecture.

I have a loop which, every x milliseconds, request from the database the actual position of all characters around the main character. After that, I remove all the objects from the screen and redraw them based on the data retrived from database. Is this reasonable, or is there a better solution?


1 Answer 1


I wouldn't consider that a good, scalable solution, no. The client should not directly query the database on the server, because that will overload the server/database with requests as you increase in scale. You also should not rely entirely on the server for position information as it will create an unpleasant experience for the client.

Multiplayer games in general typically employ client-side prediction to cope with the latter situation. This involves having the client maintain a local copy of its simulation state. When a player issues a command to the client, the client transforms that into a request for action which it sends to the server.

While the client waits for the server to confirm that the action is legal, it assumes the server will say yes, and updates its local state accordingly. This local state is used to render and subsequently is what a player will base subsequent input on. This allows for a responsive-feeling client (if you are constantly waiting for the server before moving at all, the client will feel sluggish and laggy even under ideal network conditions).

Eventually the server will get the request, process it, and can send a response to the client in the form of updated game state or some such -- the client, when it receives this, simply updates its local state to match, since the server is the authority. You will probably also want to employ smoothing between state updates to help hide unpleasant latency effects this can cause.

You can find lots of resources for client-side prediction and smoothing via Google or your favorite search engine.

As for the former issue... neither the client nor the server in an MMO should read data from the database constantly. It should keep the data and the game state in RAM locally, and push to the DB in intervals so as to avoid flooding it. See this related question for more information.

  • \$\begingroup\$ I understand what you said. Thanks for sharing your knowledge. But there is a thing that shouldnt work using your strategy. If I keep moving around the map without sending instantly the message to the database, then how can I know if someone is walking behind me, or even worse, how can I know that someone is attacking my character? \$\endgroup\$ Oct 16, 2011 at 1:37
  • \$\begingroup\$ The server will tell you what is behind you and what is attacking you. It has that information, as well as information about your position and everything else's position, in RAM. Accessing that information is much faster and more scalable than reading it from a database. \$\endgroup\$
    – user1430
    Oct 16, 2011 at 2:07
  • \$\begingroup\$ What specifically? The server tells the client about the current, correct state of the game, including what is nearby the active player. If you have follow-up questions about what that might mean, you should post a new question or perhaps ask in chat, to avoid letting this comment thread get too chatty. \$\endgroup\$
    – user1430
    Oct 16, 2011 at 2:43
  • \$\begingroup\$ @Seva For MMOs there is a service called Relevancy that tracks all the players and keeps a list of who is Relevant to any player. From this Relevancy list the server knows what updates to gather and who to send them to. This would be the mechanism to make Josh's advice work. \$\endgroup\$ Oct 16, 2011 at 4:18
  • \$\begingroup\$ @Patrick Hughes - 'Relevancy' isn't an industry-wide term, so I don't know why you're capitalising it. Some people refer to this sort of thing as 'area of interest management', for example. But the key thing Seva needs to know is that the database is just the backing store for player positions, and that all that information is usually stored in memory during play. (Although if people use PHP to write MMORPGs, that changes things somewhat, because you don't really have any state that's not in the database.) \$\endgroup\$
    – Kylotan
    Oct 16, 2011 at 11:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .