Design a Leaderboard
Understanding the Problem
🏆 What is a leaderboard? A leaderboard ranks players by score: the top 100 in the world, where you stand among everyone else ("#48,213"), and how you compare with your friends. In a competitive game it updates the moment a match ends, so a win should move you up the ladder within a second or two. The hard part isn't sorting; it's answering "what is my rank?" for any one of 100 million players, quickly, while scores keep changing.
You'll hear this as "design a real-time game leaderboard," "design the Clash of Clans trophy ladder" or "design a Fortnite tournament leaderboard," often with "plus a friends leaderboard" on the end. They're the same problem. A global ranking is the core, friends and clans are filtered or aggregated views of it, and tournaments and seasons are just more leaderboards with a start and end time.
Functional Requirements
We'll agree with our interviewer that match results arrive from game servers we don't have to design, and we're building the ranking system behind them.
Core Requirements
- Players' scores should update when a match ends, and the leaderboard should reflect the change in near real time.
- Users should be able to view the global top N (say, the top 100).
- Users should be able to see their own rank and the players just above and below them.
- Users should be able to view a leaderboard of only their friends (or clan).
Below the line (out of scope)
- Anti-cheat and validating that a score is legitimate. We trust the game server.
- Matchmaking, rewards and season payouts.
- Rich player profiles on the leaderboard (avatars, badges). We return player IDs and let another service fill in the rest.
Non-Functional Requirements
Ask for the size of the player base and what "real time" means. For our design: 100M players ranked on the global board, 20M daily active players, each playing about 5 matches a day.
Core Requirements
- The system should reflect a new score in ranks within about one second of the match ending.
- The system should return the top N and any player's rank with low latency (under 100ms at p99).
- The system should handle ~6k score updates/sec at peak and ~25k leaderboard reads/sec, with spikes of 10x when a season or tournament ends.
- The system should be durable: a crash must never lose or reset a player's score.
- Ranks should be exact for every player, with a consistent rule for ties.
Below the line (out of scope)
- Strong consistency between what two players see at the same instant. If my rank is a second behind yours, that's fine.
- Multi-region active-active. We'll run one ranking per region or one global board in one region.
The math: 20M players × 5 matches = 100M score updates a day, about 1,160/sec on average, so around 6k/sec at peak. Reads: if each active player opens a leaderboard 10 times a day, that's 200M reads a day, about 2,300/sec on average and maybe 25k/sec at peak.
Now memory. A Redis sorted set entry for a player (an ID of ~10 bytes, an 8-byte score, plus the skiplist node and hash table entry) costs on the order of 100 bytes. 100M players × ~100 bytes ≈ 10 GB. That fits comfortably in RAM on a single machine.
That last number is the most important one in the problem. A single global board of 100M players fits in memory on one node, and updates are O(log N) on a structure with about 27 levels. Many candidates shard the leaderboard on reflex. Show that it fits first, and shard only when the numbers make you.