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.
Deep dives
Compare the options before you open them, then say which you would pick.
Deep dive 1
How to know a place
Player 40 million asks for their rank. How?
BreaksCount the rows ahead
- How
- SELECT COUNT for a higher score.
- Limit
- Walks 40 million index entries for one answer.
- Choose when
- Small boards of a few thousand.
WorksCompute all ranks every few minutes
- How
- A job sorts the board and writes a rank for each player.
- Limit
- Minutes old, and 250 million writes a run.
- Choose when
- Daily boards, or e-mail summaries.
StrongSorted set
- How
- The skip list stores how many members each jump passes. Adding the jumps gives the place in about 28 steps.
- Limit
- The whole board is on one machine.
- Choose when
- Up to about one machine's memory: our choice.
WorksScore bands
- How
- Count players in each band. Add the bands above, then guess inside your own.
- Limit
- The error can be as large as your band.
- Choose when
- Huge boards, below the top million.
Deep dive 2
Splitting one board
Boards for different modes and periods are separate by nature. Splitting one global board is harder, because a place depends on everybody.
One board has a billion players. How do you split it?
BreaksBy player hash
- How
- Each shard holds a random part.
- Limit
- Every rank must ask every shard, and a top 100 merges all of them.
- Choose when
- Only for the score lookup by player.
WorksBy score range, exact
- How
- Each shard holds a range. Rank is the size of higher shards plus the place in its own.
- Limit
- A player who crosses a border is removed and added, so a reader may see two or none for a moment. Ranges must be rebalanced as scores rise.
- Choose when
- Every place must be exact.
StrongExact top, bands below
- How
- One sorted set for the top million and counts for the rest.
- Limit
- Places in the long tail are estimates.
- Choose when
- Nearly all games: our choice at a billion.
Place from bands
Deep dive 3
Ties in one number
Pack the score with the time left
Equal scores: who is ahead?
BreaksWhatever Redis does
- How
- Equal scores are ordered by member name.
- Limit
- The order is stable, but a name decides a prize.
- Choose when
- Boards nobody competes on.
WorksSame rank for equals
- How
- Count only strictly higher scores: ZCOUNT.
- Limit
- Two firsts and no second. Prizes need a rule.
- Choose when
- Casual boards.
StrongFirst to reach it
- How
- Pack seconds into the low bits.
- Limit
- Scores must be whole numbers below the limit.
- Choose when
- Our choice. Fewer deaths or a faster time fit in the same bits.
Deep dive 4
Trusted scores
Where does a score come from?
BreaksThe player's device
- How
- The app sends its score with a secret inside the app.
- Limit
- Anything on the device can be changed or copied.
- Choose when
- Offline games without prizes.
WorksDevice plus checks
- How
- Limits, history, and a replay that can be run again.
- Limit
- Clever small lies pass.
- Choose when
- Single-player games that run on the device.
StrongThe server that ran the match
- How
- It signs the result. Odd results wait in review and are not shown.
- Limit
- Needs matches on our servers.
- Choose when
- Multiplayer games: our choice.
Deep dive 5
Freshness and live updates
What is cached
| Data | Where | How long |
|---|---|---|
| Top 100 of each board | CDN | 10 s |
| Place of one player | Service memory | Up to 30 s, dropped after their own result |
| Names and avatars | Profile cache | 5 min |
| Frozen boards | CDN | Days: they never change |
When things go wrong
Edge cases
| Case | Answer |
|---|---|
| Impossible score | Hold it, check the replay, review by a person |
| Result sent twice | Unique match and player; GT makes the board write harmless |
| Result out of order | The best score wins whatever the order |
| Redis primary lost | Promote a replica, replay recent results from the log |
| Whole board lost | Rebuild from best scores: 250M at an assumed 200,000 a second is about 21 minutes. Top lists stay cached |
| Busy top list | CDN, joined fills and a copy in service memory |
| Result at midnight on the last day | Server's end time decides; ten minutes of grace, then freeze |
| Week number | Seconds since 1970 divided by 604,800 turn over on a Thursday. Subtract four days first for Monday weeks |
| 50,000 friends | Closest 500, or a list made beforehand |
| Player banned | ZREM now, rebuild bands later, keep the results |
| Profile service down | Show IDs or the last known names |
Life of a weekly board