Lesson 4 · Minutes 30-45

Real-Time Leaderboard: Deep Dives and Curveballs

Problem

Ten minutes ago you asked the interviewer to take the band boundaries as given. From minute thirty to the end of the round, that debt comes due. This lesson compares the ways of computing a rank, picks up the bands, prices the restart at both sizes, then takes four curveballs and the way each level answers.

The Rank Query, Compared

There are three ways to compute a rank. A count over an index is exact, and it grows with the board. A sorted structure is exact and takes about 25 steps. The third is a histogram in each application server, a small array holding how many players fall in each range of scores. It is approximate, and its size is fixed however large the board gets.

The choice here is the sorted structure, because Scope asked for exact at the top. The trigger for the histogram is a product where most ranks are far down and "top 40 percent" is accepted. Assume 10,000 buckets at 8 bytes each, refreshed once a minute: that is 80 KB a server, and those reads never reach a band. Offer it as a question to the interviewer: "I'd ask whether anyone far down needs an exact number. 'Top 40 percent' comes from a small array in every server, and it takes most of the rank reads off the bands."

How a rank is computed, by requirement
Exact, and a board of about 100,000 players→A count over the score index
Exact at the top, on a board of millions→A sorted structure, about 25 steps
Most ranks far down, "top 40 percent" accepted→A histogram in each app server

The Sharded Rank

This is what you parked: the bands, and where their boundaries come from. A band is a range partition, one slice of the score order, and a router holds the boundaries between slices, as in Chapter 3's Range, Hash and Directory. Cut them by traffic, not by score width or player count, because active players cluster.

The trap is this problem's own moving hot spot. A season starts with every score near zero, so the lowest band takes nearly every write, and then players climb through the bands all season. So boundaries come from last season's curve at the same day number, and they are recut daily in the first week, using the four steps in Chapter 3's Hot Partitions.

A player crossing a boundary is added to the new band first and removed from the old one second. For a moment that player is counted twice, and they are never missing, which is the right way round for a board that is allowed to be slightly off.

Score Bands vs Hash Shards

Score bands give each band a slice of the score order, so a rank asks one band and adds a map of counts. Use them when rank reads dominate and a small error far down is acceptable.

Hash shards spread players evenly and never need recutting, and every rank asks all of them. Use them when ranks are rare or must be exact everywhere, and copies can carry the fan-out.

The Restart, Priced

Three answers, three bills. Replica failover takes seconds at any size, for twice the memory. A rebuild from the table takes 40 seconds at 40 million players. At 600 million each band node takes its own 1 million inserts a second, so an average band takes 150 seconds, and all four rebuild side by side in about that time. A snapshot plus a replay reads 15 GB at about 10 GB a second, under two seconds, but those are only the bytes: a store such as Redis inserts every entry back into the set as it loads, and it is one more thing to run.

So choose the replica and the rebuild now. At 600 million, losing a band and its replica together means about 150 seconds of "updating" for that band's players. That is the trigger the last lesson left open: if it passes what the product tolerates, price the snapshot.

The restart at 40 and 600 million
40 million playersone set, 4 GB
Replica: seconds, 8 GB in all. Rebuild: 40 seconds. Snapshot: 4 GB read in under a second, entries still to insert, not needed yet.
600 million playersfour bands, about 15 GB each
Replica: seconds, 120 GB in all. Rebuild: about 150 seconds a band, all four side by side. Snapshot: 15 GB read in 1.5 seconds, every entry inserted, then a replay.

Curveballs

"Add a friends-only board." The reflex is a sorted set per player, and today's 3,000 results a second stop it. Assume 200 friends. Writing each result into every friend's board is 3,000 times 200, which is 600,000 writes a second. So read instead: fetch the 200 scores by player from the set and sort them. Assume one board read in ten is a friends board. That is 900 a second times 200, or 180,000 pipelined lookups a second, within one node's million, and the answer is cached for seconds. The condition is a capped friends list, because the read grows with it.

"Add a board per country." That is one more set per country, so writes double to 6,000 a second. Assume the largest country holds a tenth of the players: 4 million entries, 400 MB. Country boards stay on one node each. At 600 million the largest is 60 million entries and 6 GB, still one node, so only the global board needs bands.

"A cheater's scores must be revoked, three weeks later." Rank is computed, so removing one entry shifts everyone below without touching them. The score is recomputed from the results table, which is why that table outlives the dedup check. Assume 100 bytes a row. A day's 120 million rows are then 12 GB, and a season is about 1.1 TB. Final standings freeze only after a review window, because prizes cannot be recalled.

"The season resets at midnight in every time zone." Ask first, because one global board has one reset, so this means either one stated instant or a board per zone. Then never delete 40 million entries. Start a new set keyed by season, flip the current season in configuration, and keep the old set read-only for final standings. The instant comes from the server. On the sharded board this is also the moment the lowest band takes everything, the hot spot you have already planned for.

How Each Level Answers

A mid-level candidate lands the match ID and a sound API in a working design, gets to the sorted set once the count is pushed on, and rebuilds from the table after a hint. They come to shards when asked, and may start with hash.

A senior candidate opens with what one rank costs and treats the set as derived data before anyone mentions a restart. They work out that the rate and not the memory passes one node, reject hash shards with the arithmetic, state the error a band's count allows, and raise the tie unprompted.

A staff candidate asks whether a player at rank 300 million needs a number at all, since a percentile removes most of the load. They ask what rides on the top ten, because prizes set the review window and who stamps the time. And they treat the season boundary as the system's worst day, and plan the recut for it.

Interview Pitfalls
  • Cutting bands by equal score width, then being asked about day one of a season.
  • Answering the friends board with a set per player, without doing the 600,000.
  • Deleting the old season in place at midnight, at the busiest moment of the quarter.
What Interviewers Listen For
  • The season start named as a moving hot spot before the interviewer does.
  • Each curveball's write fan-out set against its read cost, in numbers.
  • A revocation described as touching one entry, because rank is computed.

Knowledge Check

"Add a friends-only board, about 200 friends each." What is the strongest move?

  • Keep a sorted set per player and write each result into every friend's
  • Recompute all the friends boards in a batch job every few minutes
  • Read the 200 scores by player, sort them, and cache that for seconds
  • Ask the bands for each friend's global rank, then sort them by that

"A cheater's scores must be revoked, three weeks later." What is the strongest move?

  • Remove the one entry, recompute from results, freeze after a review
  • Rewrite the stored rank of each player below the cheater in a batch
  • Rebuild every band from the table and pause all ranks until it finishes
  • Delete the cheater's rows from the results table and leave the set alone

"The season ends tonight." What should happen to the old set?

  • Delete its 40 million entries at midnight so that writes can start clean
  • Keep it live, and set each of its players' scores back to zero in a sweep
  • Drain it into the scores table first, then clear it for the next season
  • Leave it read-only for final standings while a new set takes the writes

"How do your bands behave on day one of a season?" What is the strongest answer?

  • They stay even, because equal score widths give each band a fair share
  • The lowest takes nearly every write, so cut from last season's curve
  • The top band runs hottest, because the best players come back soonest
  • Nothing changes, because a hash of the player keeps spreading the writes

You got correct