views:

215

answers:

1

Background

We are currently working on a strategy web-browser game based on php, html and javascript. The plan is to have 10,000+ users playing within the same world.

Currently we are using memcached to:

  • store json static data, language files
  • store changeable serialized php class objects (such as armies, inventorys, unit-containers, buildings, etc)

In the back we have a mysql server running and holding all the game data aswell. When a object is loaded through our ObjectLoader it loads in this order:

  1. checks a static hashmap in the script for the object
  2. checks memcache if it has already been loaded into it
  3. otherwise loads from database, and saves it into memcache and the static temp hashmap

We have built the whole game using a class-object-oriented approach where functionality is always made between objects. Beause of this we think we have managed to get a nice structure, and with the help of memcached we have received good request times from client-server when interacting with the game.

I'm aware that memcache is not synchronized, and also is not commonly used for holding a full game in memory. In the beginning after a server's startup the load times when loading objects into memcache for the first time will be high, but after the server's been online for a while and most loads are from memcache, the loads will be well reduced.

Currently we are saving changed objects into memcache and database at the same time. Earlier we had an idea to save objects into db only after a certain time or at intervals, but due to risk inconsistency if the memcache/server went down, we skipped it for now.

Client requests to server often return object's status simple json-format without changing the object, which in turn is represented in the browser visually with images and javascript. But from time to time depending on when an object was last updated, it updates them with new information (e.g. a build-queue holding planned buildings time-progress is increased, and/or planned-queue-items-array has changed).

Questions:

  • Do you see how this could work or are we walking in blindness here?
  • Do you expect us to have a lot of inconsistency issues if someone loads and updates the a memcache objects while someone else does the same?
  • Is it even doable to do it in the way he have done it? Seems to be working fine atm, but so far we have only been 4 people online at the same time..
  • Is some other cache program more fit for this class-object approach than memcached?
  • Is there any other tips you have for this situation?

UPDATE

Since it is simply a "normal webpage" (no applet, flash, etc), we are implementing the game so that the server is the only one holding a "real game-state".. the state of the different javascript-objects on the client is more like a approximative version of the server's game state.

From time to time and before you do certain things important things, the client's visual state is updated to the server's state (e.g. the client things he can afford a barracks, asks the server to build a barracks, server updates current resources according to income-data on server and then tries to build a barracks or casts an error-message, and then sends the current server-state on resources, buildings back to the client)..

It is not a fast-paced game lika real strategy game. More like a quite slow 3-4 months playtime game, where buildings can take +1 minute up to several days to complete.

A: 

Whether the inconsistency is actually a problem depends on how you are implementing your game. If the whole game is updated with definitive snapshots at the client end every so often, it shouldn't matter so much as the player will just be getting higher latency (and latency is unavoidable). If your game states aren't synchronised, two players' versions of the world could drift off in completely different directions after a while, which is what you want to avoid.

But are you sure you really need memcache? It just seems to add more complication and potential inconsistency to your solution. It might be a premature optimisation. You're already reliant on the database, what is performance like if you just use the database alone? If set up correctly, I'd expect your database server to be caching everything in RAM anyway.

bobince
From what I heard memcached has good scalability comparing to database as the numbers of active users grow.Currently if a client asks for all content in his current 11x11 tile map screen, it takes 0,2 sec with memcache on, and 0,6 s with it turned off.It might be so that when we designed the whole system we had memcache in mind all the time, trying to optimize things for when memcache is on.As you point out it probably adds more complication and inconsistency, but as it is now it's too late to rewrite all code..
Crowye