Requirements
- Update player scores and read ranks in real time.
- Serve the top players and a player own rank quickly.
- Scale to millions of players.
High level design
Scores live in a sorted structure that supports fast updates and rank queries, fronted by an API.
- Sorted set: an in memory sorted set keyed by score gives logarithmic updates and range reads for the top list.
- Rank query: the structure returns a player rank directly without scanning.
- Persistence: a backing store keeps durable scores while the sorted set serves reads.
Bottlenecks
- Scale of players: one giant sorted set can be sharded by score band or region, then merged for global views.
- Write bursts: many updates at once are absorbed by batching and by the logarithmic update cost.
- Ties: equal scores need a deterministic tiebreak such as earliest achieved time.
For huge populations, approximate a player rank within a band rather than computing an exact global position on every read.
Key idea
A leaderboard maps scores into a sorted set for logarithmic updates and instant top and rank queries, sharding and approximating rank at extreme scale.