CHAPTER DEEP READ · System Design Interview Vol 2 · Ch 10

Design a Real-time Gaming Leaderboard

System Design Interview Vol 2 · Ch 10 · Alex Xu & Sahn Lam · 2022

中文 →

What is this chapter about?

The weekly ranking in a mobile game, the step count board in a fitness app, the donor board in a livestream — they all look like nothing more than "sort people by score." This chapter builds one from scratch: millions of players at once, scores changing every second, and every one of them wants to be told, right now, where they stand.

One strange thing first

It sounds like a job for a rack of machines. But the first thing this chapter does is reach for a calculator. And the arithmetic says: a monthly board for five million active players is only a few hundred megabytes, and at its busiest takes a couple of thousand changes per second. One machine holds it. One machine keeps up with it.

That is the real lesson of the chapter: do the arithmetic before you draw the architecture. A great many "we obviously need a distributed system" instincts do not survive contact with a calculator.

Why the old way is hard

Put the scores in an ordinary database and "show me the top ten" is easy — sort by score, take the first ten rows.

The hard question is the other one: "where am I?" A database has no notion of "nth." All it can do is count: how many people scored higher than me? If you sit in the middle of twenty-five million players, it has to count ten million of them before it can answer "you're tenth-millionth." Let everybody ask that once and the machine falls over.

The mechanism, intuitively

① Counting has to be able to skip. Line everyone up by score. Chain them one by one only on the bottom row, then build a few express lanes above it, where a single hop clears a long stretch of people. The trick is that every hop also records how many people it just jumped over. So working out a rank means taking a few express hops and adding up what you skipped, instead of pointing at people one at a time. Twenty-five million players still take only about twenty steps.

② A leaderboard is one thing, and it doesn't cut. If there are too many players for one machine, the instinct is to split them across several. But "where am I" is a question about the whole field. Scatter players randomly across machines and each one knows only the order of its own handful — nobody can answer, so you have to ask all of them and stitch the answers together.

③ So you split by score band instead. Not random players, but score ranges: everyone above 900 on one machine, 800–899 on the next. Now "where am I" is simply how many people sit in the bands above mine (one number per machine) plus my place inside my own band. The price: if everyone's scores crowd into the same band, that machine gets crushed.

④ Don't be exact for everybody. The top ten must be perfect — but nobody alive cares about the difference between eight-millionth and eight-million-and-first place. Telling the vast middle "you're in the top 12%" is far cheaper than counting to the exact integer, and they understand it better anyway.

What it costs

To be this fast the board lives in memory, so a restart can lose the last few seconds — or minutes — of scores; the real ledger has to be kept on disk elsewhere, ready to rebuild the board at any time.

One line to remember

A leaderboard's hard part was never sorting — it is "where am I," a question you cannot answer without seeing the whole field. The fix is express lanes over the sorted line, each hop recording how many people it skipped, so a rank can be added up instead of counted out. When you genuinely must split across machines, you can only split by score band, never by player, or the field-wide ranking dissolves. And for almost everyone, "you're in the top 12%" is the better deal.

Want the mechanisms, the notation and the diagrams? → Switch to the deep read