Lesson 2 · Minutes 10-20

Real-Time Leaderboard: First Design

Problem

It is minute ten, you have priced a rank at 2 seconds on paper, and you are about to draw the design that costs exactly that. It is the right thing to draw. One relational database with an index on the score records every result correctly, shows the top ten and ranks any player, and on a board the size of a new game's nothing about it is wrong. These ten minutes draw it, walk one result and one rank through it, and say at what size the count stops being fine.

The quiz closing this page holds the four places the interviewer leans on it. Scaling It takes them in the same order.

A Scores Table With an Index

The game server and the phones reach a stateless application tier behind a load balancer. One relational database holds two tables. The scores table has one row per player and season, with an index on the score. The results table has one row per match and player, and that pair is its unique key.

The first design
Phonetop ten, own rank Load balancerany server Game serverreports each match App serversstateless One databasescores, results one transaction count readwrite

One Result, One Rank

A match ends and the game server posts the result. An application server opens one transaction, inserts the result row, and adds the points to the player's score. If the game server lost the answer and retries, the insert hits the unique key, the transaction stops, and the score does not move. This is the dedup record from Chapter 6's Delivery Semantics at Scale, and it is what makes a running total safe.

A player opens the results screen and the phone asks for the board. The top ten reads the first ten entries of the score index, which costs next to nothing. For the rank, the server reads the player's score, then asks the database how many rows have a higher one. The database walks the index from the top down to that score, entry by entry. The count plus one is the rank.

Why the Count Is Fine at Launch

This records results, shows the top ten and gives any player a rank, and a game at launch needs nothing more. Assume 100,000 players. The average count walks 50,000 entries at about 100 nanoseconds each, which is about 5 milliseconds, and no results screen notices that. Say that first, and the limits get their turn after it.

Nothing is lost or counted twice, because one transaction holds both writes. And two players on one score share a rank without anyone deciding it, since each sees the same rows above them. That last point is a decision you have not finished, so park it where the interviewer can hear it: "Equal scores share a rank for now, and I'll come back to that if there's a prize on it."

Where the Count Stops Being Fine

Three limits get named here and fixed later. The count grows with the board: 50 milliseconds at a million players, 2 seconds at 40 million. The index cannot help, because the index is what the count walks. At 9,000 ranks a second, that is 18,000 seconds of work arriving every second.

The one database is also the one failure: lose it, and nothing records and nothing ranks. And it takes 3,000 results a second at peak, each one two row writes. You claim no number for one relational primary. As a yardstick, one key-value partition takes 1,000 writes a second, so that is the work of six partitions.

The first limit is yours to raise, with a board size on it and the symptom you would watch: "Up to about a hundred thousand players one count takes about 5 milliseconds, and nobody notices. At 40 million it's 2 seconds, so I'd watch the results screen slow down as the board grows, and rank is what moves first."

Interview Pitfalls
  • Skipping to a sorted set, a structure kept in score order, in the first breath. It is the right end point, with no working design to push on and no number for why.
  • Adding a stored rank column. One climb rewrites the rank of every player passed.
  • Adding the points with no match ID, so that a retried result counts twice.
  • Saying "the database won't cope" with no number, when the latency ladder gives you one.
What Interviewers Listen For
  • One result followed through its transaction, then one rank read followed down the index.
  • The top ten called cheap and the rank not, with the reason the index does not save it.
  • The player count where a count stops being fine, said before the interviewer asks.

Knowledge Check

"Forty million players, nine thousand rank requests a second at peak, and every one of them is that count. Talk me through it."

  • "Add an index on the score column so the count stops scanning the whole table."
  • "Add read replicas and spread the counts across them until the latency drops."
  • "Each count walks half the index, so move rank into a sorted structure in memory."
  • "Have a batch job rank every player every few minutes, and serve the stored rank."

"That sorted set lives in one process's memory, and the process has just restarted. What did you lose, and what do players see meanwhile?"

  • "The scores table still holds every score, so a replica serves while the set refills."
  • "Snapshot the set to disk every hour and load the newest copy when it comes back."
  • "Make the set the only copy of the scores and log each update to a file beside it."
  • "Let the set fill up again by itself as players finish matches over the coming days."

"The studio wants this in every market: 600 million players, fifteen times today, same habits. Is one node still enough, and how would you split a ranking?"

  • "Move to the largest node there is: 60 GB of entries fits in its 600 GB with room."
  • "Hash the players across shards and ask every shard how many outrank this player."
  • "Keep one set for the top thousand players and stop showing anyone else a rank."
  • "Split the set into score bands, then add the players in higher bands to each rank."

"Two players end on the same score and the top ten has room for one of them. Who gets the slot, and how would your set know?"

  • "The set orders equal scores by player ID, which is stable, so leave it like that."
  • "Keep a second structure for ties, and check it whenever two neighbours are equal."
  • "Ask for the rule; if the earlier player wins, put the time in the score's low bits."
  • "Sort tied players by the time they scored in the app server each time it is read."

You got correct