Design Real-Time Game Stats
Design the backend of an online gaming platform that takes in a heavy stream of events from game servers, turns them into per-match and per-player stats, and shows those stats to players and viewers while matches are still running, with follow-ups on what happens when Redis goes down, how to lay out its keys, and why to use Redis at all.
Problem statement
Design the stats backend of an online gaming platform. For this exercise, assume the game servers that run matches send the platform a nonstop flood of game events: a player scoring points, one player knocking out another, or a match ending.
The platform turns these events into stats and shows them to players and viewers while the matches are still being played.
Also assume every event names one match and one player and the counter changes it causes, such as kills +1 or score +50, and that the platform offers two views: - Live match stats: for a match that is still running, each player's numbers so far (for example kills, deaths and score), ordered by score.
- Player totals: for each player in each game, lifetime numbers such as matches played, wins and total score, covering every finished match.
Start with the APIs and the overall architecture. The follow-ups then assume your design keeps live stats in Redis (an in-memory key-value store): how the system copes when Redis goes down, how to design its keys and indexes, and why Redis is the right pick.
For this exercise, assume the game servers themselves, matchmaking, cheat detection and rankings across all players stay outside this design.
What to build
- Game servers send batches of game events, and the platform accepts them at the peak rate. Once accepted, an event is never lost, even when the steps after it are slow or down.
- For a match that is still running, players and viewers see each player's current stats ordered by score, and the view refreshes by itself as new events arrive.
- For each player and game, the platform keeps lifetime totals that count every finished match exactly once, and players can look them up.
At scale
- For this exercise, assume roughly 2 million players are in about 100,000 live matches at peak, and game servers then send about 500,000 game events a second, each under 1 KB.
- For this exercise, assume that at peak, players and viewers make about 200,000 stat reads a second, and a change should show on screen within about 2 seconds for a live match and within about 10 seconds for a player's totals.
- For this exercise, assume the totals for a finished match must be exact (no accepted event lost or counted twice), and raw events are kept for about 3 days so they can be read again.