Design a Leaderboard (High-Level Design)
High-level design of a real-time leaderboard: a Redis sorted set giving O(log n) score updates, top-K and exact-rank queries, backed by a durable DB, with time-scoped boards, sharding by score range and approximate rank to scale to tens of millions of players.
🟠High-Level Design · Case Study · Senior
"Design a leaderboard" (for a game, a fitness app, or a trading contest) looks simple — sort by score — but at scale it's a lovely problem. Millions of players, scores changing constantly, and you need the top 10, and "what's my rank?" for a player sitting at position 4,271,908 — all in real time. The naive "sort the whole table" answer falls apart fast. Let's design it right.
Step 1 — Requirements
- Functional: update a player's score; get the top N; get a specific player's rank; get the players around a given player (±5); support time-scoped boards (daily/weekly/all-time).
- Non-functional: real-time updates, low-latency reads, scale to tens of millions of players, high availability.
Step 2 — Why a database alone won't do
🟠A SELECT ... ORDER BY score DESC LIMIT 10 is fine for the top 10 with an index. But "what's player X's rank?" means counting how many players score higher — a COUNT over potentially millions of rows per query. With scores changing every second and millions of players checking their rank, that hammers the DB. You need a structure where rank is O(log n), not O(n).
Step 3 — The core: a sorted set
🔴 The canonical answer is a Redis sorted set (a skip list + hash map under the hood). Each member is a player, scored by their points; it keeps members ordered by score automatically.
ZADD leaderboard 4820 "player:123" // set/update score — O(log n) ZREVRANGE leaderboard 0 9 WITHSCORES // top 10 — O(log n + 10) ZREVRANK leaderboard "player:123" // this player's rank — O(log n) ZREVRANGE leaderboard 4270 4280 // players around a rank — O(log n + k)
Every operation you need — update, top-K, exact rank, neighbours — is O(log n). That's the whole trick: the right data structure turns an expensive scan into a cheap lookup.
Step 4 — Durability & time-scoped boards
🟠Redis holds the live leaderboard, but it's a cache — the source of truth is a database. Write score changes to the DB (durable) and update the sorted set (fast reads); on a Redis restart, rebuild from the DB. For daily/weekly boards, use a separate keyed sorted set per window (leaderboard:2026-11-04) with a TTL, so old boards expire automatically and "all-time" is its own set.
Step 5 — Scaling beyond one node
🔴 One sorted set with tens of millions of entries can exceed a single Redis node's memory/throughput. Options to raise:
- Shard by score range or region and merge for global top-K; exact global rank across shards gets tricky (you must sum counts from each shard).
- Approximate rank: for a player deep down the board, an exact rank rarely matters — bucket scores (histograms) to answer "you're in the top 12%" cheaply. Approximation is a great senior trade-off to name.
- Hot updates: batch/throttle very frequent score writes; a queue can absorb spikes and update the set asynchronously.
- Cache the top-N (it's read far more than it changes).
The interview-ready walkthrough
"The killer query is 'what's my rank?', which is O(n) in a plain DB, so I use a sorted set (Redis) where update, top-K, exact rank, and neighbours are all O(log n). The DB stays the durable source of truth and I rebuild the set from it on restart; time-scoped boards are separate keyed sets with TTLs. To scale past one node I'd shard by score range (merging for global top-K) and use approximate/bucketed rank for deep positions, cache the hot top-N, and absorb write spikes via a queue." That shows you know the data-structure trick and the scale trade-offs."
What to read next
- Caching Strategies · Distributed Cache — Redis at scale
- Design an Ad Click Aggregator — another real-time counting problem
- Design an E-commerce Platform — next up, the final expansion
- ← The complete System Design guide (hub)