AlgoViz

Design a Multiplayer Game Leaderboard

easy 45 min

Keep 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

rank≈1+∑b>b0nb+nb0⋅topb0−swidth\text{rank} \approx 1 + \sum_{b > b_0} n_b + n_{b_0}\cdot\frac{\text{top}_{b_0} - s}{\text{width}}
The last term assumes scores are spread evenly inside the band. In the real data, make narrow bands where players are crowded.

Deep dive 3

Ties in one number

Pack the score with the time left

packed=s⋅220+(220−1−t),0≤t<604,800<220\text{packed} = s\cdot 2^{20} + (2^{20}-1-t), \qquad 0 \le t < 604{,}800 < 2^{20}
t is the number of seconds since the week began. Scores up to 2^33 (8.6 billion) keep the result below 2^53.

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

DataWhereHow long
Top 100 of each boardCDN10 s
Place of one playerService memoryUp to 30 s, dropped after their own result
Names and avatarsProfile cache5 min
Frozen boardsCDNDays: they never change
Lists expire by time. Nothing is purged when a score changes.

When things go wrong

Edge cases

CaseAnswer
Impossible scoreHold it, check the replay, review by a person
Result sent twiceUnique match and player; GT makes the board write harmless
Result out of orderThe best score wins whatever the order
Redis primary lostPromote a replica, replay recent results from the log
Whole board lostRebuild from best scores: 250M at an assumed 200,000 a second is about 21 minutes. Top lists stay cached
Busy top listCDN, joined fills and a copy in service memory
Result at midnight on the last dayServer's end time decides; ten minutes of grace, then freeze
Week numberSeconds since 1970 divided by 604,800 turn over on a Thursday. Subtract four days first for Monday weeks
50,000 friendsClosest 500, or a list made beforehand
Player bannedZREM now, rebuild bands later, keep the results
Profile service downShow IDs or the last known names

Life of a weekly board