Design a Multiplayer Game Leaderboard
easy 45 minKeep 250 million players in order while results arrive all the time, and tell any of them their place in milliseconds.
Video lesson · Chapter 1 of 5
The high score survives even if the ranking cache disappears
Theory
Read or listen first. About 1 min.
What it is
A game leaderboard orders players by score and answers three questions: who is at the top, what is my place, and how do my friends compare.
The first question has one answer for everyone, so it can be copied. The second has a different answer for every player, and one new score can change the answer for thousands of others. That difference shapes the whole design.
Why it exists
Players come back to see where they stand. A board that is slow, unfair or full of impossible scores takes that reason away.
The same machine ranks fitness segments, learning points, sales teams and quiz nights, so the design is useful far from games.
A short history
June 1990
William Pugh publishes skip lists, a simple ordered structure with logarithmic search on average.
Redis 1.2
Redis gains sorted sets, which join a skip list with a hash table. The ZADD page lists 1.2.0 as its first version.
February 2021
Redis 6.2 adds the GT option of ZADD, ZMSCORE, and ZRANGE with REV, which replaces ZREVRANGE.
Words to know
- Rank
- Your place: one plus the number of players ahead of you.
- Sorted set
- Members with a number each, always kept in order.
- Percentile
- Your rank divided by the number of players, as in top 5%.
- Period
- The day, week or season a board covers. It is part of the board's name.
- Tie-break
- The rule that orders equal scores, such as who reached it first.
- Score band
- A range of scores with a count of the players in it.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Accept. A game server reports a finished match, and the result is stored first
Understand it
Start here
A row of sunflowers
What we build
Boards for a large game
A service for a game with 100 million players a day: global, regional and mode boards for each week and for all time, a player's own place with neighbours, and a friends board. It owns scores and order. Matches, profiles and friends belong to other services.
Global boards
Everyone against everyone, by mode and region.
Friends
A small board, worked out when asked.
Periods
Daily, weekly, season and all time.
My place
Rank, neighbours and a percentile.
The hard part
One score moves many ranks
Suppose a result lifts a player from place 1,650 to 1,520. The 130 players between them all move down one place. Nobody can write 130 ranks for each result. So a rank is never stored. It is counted from the order when someone asks.
Store the order, count the place
The finished system
Where the design ends up
This is the last version. The Design tab starts with one service and one database.
The whole system
Similar boards
| Kind of product | What is ordered |
|---|---|
| A battle game | Wins or points in a season |
| A chess site | A rating such as Elo |
| A language course | Points earned this week |
| A running app | Time on one stretch of road |
| A puzzle game | Best score for one level |
| A question site | Reputation |
Where to read more
The ideas behind it
Each idea has its own page.
- The edge copy of the top list: CDN and cache TTL.
- Results first, boards later: log-based queues and idempotency.
- Splitting boards: partitioning strategies and sharding.
- Copies that lag: replication fundamentals.
- Counting very busy things: sharded counters.
- Live pushes: real-time APIs.
For the interview: Approach, Scope, Numbers, API and data, Deep dives and Mock.
Summary
The whole topic in one minute.
A leaderboard is an ordered set that is read far more than it is written. Top lists are the same for everyone, so a CDN answers them. Personal ranks go to a sorted set, where a skip list finds a place in about 28 steps among 250 million.
Scores come from game servers, not players. They are stored in a log first and applied so that a repeat changes nothing. Redis can then be lost and rebuilt. Only a board too large for one machine needs splitting, and there an estimate is enough for most places.
- Store scores and keep them in order. Count a rank when it is asked for.
- Top lists are shared, so cache them. Personal ranks are not.
- Trust results from game servers, never from the player's device.
- Put the period in the key and there is no reset.
- Exact places matter at the top. In the middle, a percentile is enough.