Design a Social Game Score Leaderboard
Maintain per-game high scores, show exact global rank after a game, and find nearby global or friend scores under heavy skew.
Problem statement
Design a leaderboard for a video game platform. When a player finishes a game, show the scores closest to theirs among all players or among their current friends, whichever the request asks for, and tell them their exact global rank in that game.
The system has players, games and friendships. Each player has one accepted high score per game: their best accepted score. Only that high score is ranked. Each improvement creates a new high-score version. The data set is large, and finding the scores near a player (a range search) is the main challenge.
Rank. A player's rank is 1 plus the number of ranked players ahead of them. Players are ordered by high score, highest first. Ties go to whoever reached that high score first by server time (a later lower or equal score does not reset this time), then to player ID. So players with equal scores still get different ranks.
Score submissions. Each submission carries a stable attempt ID, so a retried submission is not counted twice. The response to a finished game shows both the score just submitted and the high score being ranked, so the two are not confused.
Freshness. If an accepted higher score hasn't reached the rankings yet, return "pending" or an older result clearly labeled with its older high-score version. Never show an old rank as if it were the new result. Friendship changes are versioned. A friend comparison uses its own friend-list version, and global rank is not a rank among friends.
Out of scope: account privacy rules, anti-cheat review, messaging and matchmaking.
What to build
- A game service can submit a player's score and retry safely. The ranked high score changes only when the new score is higher.
- A player can see the closest lower and higher scores in one game among all eligible players. After a game, the response also gives the player's exact global rank, taken from the same ranking snapshot as those nearby scores (the response names that snapshot), or says the result is pending or older.
- A player can see the closest scores among their current friends.
- Each response says which ranking and friend-list versions it used, so the client knows how stale it may be.
At scale
- 500 million players and 100,000 games.
- 100,000 score submissions and 1 million nearby-score reads per second.
- Nearby-score reads and global-rank reads return within 200 milliseconds at p95. Global rank may never be quietly replaced by an estimate.
- An accepted improved score shows up in the global ranking within 2 seconds at p95.
- A player can have up to 5,000 friends. Friend-only views may lag friendship changes by up to 10 seconds.
- Traffic is very uneven: one viral game can get 30% of all traffic.