The Datastore
Hundreds of thousands of players could be playing our game at any given time. This means that we’ll have a lot of Heartbeats to keep track of. The Datastore that GAE supports provides a simple but highly scalable data solution for us. Operations involving the Datastore are held in the HeartbeatDatastore class.
The storeHeartbeat() and removeHeartbeat() methods manage the adding removing of Heartbeats to the Datastore. Instead of actively keeping track of every single Heartbeat and removing them as soon as they timeout, we reduce the overhead by only paying attention to Heartbeats that were received in the last 30 seconds. All other Heartbeats are considered to have been from matches that have premature ended and timed out. A cron job will handle these cases, and clean them up via the cleanupDatastore() method (we’ll discuss this later).
To join a match, game clients will need to get a list of open matches from the service. The getOpenMatches() method provides this data from the datastore. This call will return up to 10 games that are available for joining. Because a match may be unreachable or may be filled before a client can join, the client should iterate through this list of open matches in order to find a reachable match.
To demonstrate some of the more powerful things the service is capable of, I added the getMostPopularGameMode() method. This looks at all the active matches currently being played and tallies the most popular game mode for that country. This is potentially an intensive task, considering we may have hundreds of thousands of active matches. To reduce the cost of calculating this data, we use Memcache to cache the data for 60 seconds, to limit the amount of times it is performed.