Design a real-time top-K game leaderboard with user rank lookup
Role: Software Engineer
Category: System Design
Difficulty: medium
Interview Round: Other
Design a Top-K leaderboard for a game. Scores change constantly as people play, and the system has to keep rankings current while showing a global top 10 to everyone who opens the leaderboard.
The system must support three operations:
1. Update score: a user can update their score.
2. Get top K: users can view the top K users, for example the top 10 globally.
3. Get user rank: a user can see their own rank and score, even when they are not in the top K.
Describe the requirements, data model, API and architecture, and explain how the design meets the scale and latency targets below.
```hint Cost of each operation
Write down what each of the three operations costs in a relational table with an index on score. One of them gets slower the further down the ranking a user sits, and that one should drive the choice of storage.
```
```hint Size it first
Turn the traffic numbers into writes per second, reads per second and gigabytes of ranking state, then check whether one node is enough. If you split users across nodes, work out what "my rank" then requires.
```
### Constraints & Assumptions
- About 10 million daily active users.
- Peak load of 50,000 score updates per second and 500,000 leaderboard reads per second.
- Availability takes priority over consistency: users should always see a leaderboard, even a slightly stale one, with at least 99.9% uptime.
- Leaderboard pages load in under 300 ms, and a score update appears in the rankings within 1 to 2 seconds.
### Clarifying Questions to Ask
- Does an update add points to a running total, or does it replace the score only when the new result is higher?
- Is there one global leaderboard, or many (per game, per season, per region)?
- How should ties be ranked: do tied players share a rank, or does the player who reached the score first rank higher?
- Does a user far down the list need an exact rank, or is an estimate such as a percentile acceptable?
### What a Strong Answer Covers
- Requirements and estimates that turn the user count and traffic into a write rate, a read rate and the memory needed for the ranking state.
- A ranking store that handles score updates, top K and a single user's rank efficiently, with the cost of each, plus a durable store the rankings can be rebuilt from.
- A write path that absorbs bursts, survives a crashed server without losing or double-counting updates, and still meets the visibility target.
- How the rankings are partitioned once one node is not enough, and how top K and an individual rank are computed across partitions.
- The read path: caching the hot top K, keeping staleness inside the target, and what users see when a shard or the cache fails.
### Follow-up Questions
- How would you add monthly leaderboards and a rolling "last 7 days" leaderboard?
- Two players have the same score. How do you rank the one who reached it first higher, and what does the numeric type of the stored score allow?
- With users hashed across N shards, how do you return the exact rank of a player near position 5,000,000, and when would you show an estimate instead?
- A Redis shard and its replica are both lost. How do you rebuild it, and what do users see in the meantime?
Overview: Design a game leaderboard that accepts frequent score updates, returns the global top K players, and shows any player's own rank and score. It tests choosing a ranking data structure, handling 50,000 writes per second, partitioning ranked data across nodes, and keeping reads fast and available.