Lesson 1 · Minutes 0-10

Real-Time Leaderboard: Scope

Problem

The first ten minutes of this round turn on one word in the question: rank. Showing ten names is a query anybody can write. Telling one player out of millions exactly where they stand is a sum, and by minute ten you owe the interviewer its answer, along with the five agreements it rests on and the three calls of the API.

The question hides a second decision in the words "as each match ends". They set a deadline, and that deadline rules out the cheapest design there is. So the questions come before any drawing.

Five Questions With Consequences

Ask these five, and for each one say what the answer would change, so the interviewer hears why you are asking.

  1. How many players are on the board, and how many play in a day? The first number sets what one rank costs, and the second sets the rate. Agreed: 40 million on the board, 10 million a day, 12 matches each.
  2. How soon after a match must the new rank show? This rules out the cheapest design, a batch job. Agreed: on the results screen, within about a second.
  3. Is the score a best result, a running total, or a rating? A running total makes each result an increment, and a retried increment counts twice. Agreed: a running total over a 90-day season.
  4. Must every rank be exact, or only near the top? Nobody can tell rank 31,204,118 from 31,204,500. Agreed: exact at the top, within a percent far down. That sounds like a courtesy, and it pays for the shards later.
  5. Who reports a result, the phone or the game server? A phone can lie. Agreed: only the game server, with a match ID.

If the interviewer is impatient, the freshness question goes first, because one answer removes a whole family of designs. Put it to them as a fork: "One thing first: how soon after a match must the rank change? If a few minutes is fine, this is a batch job. If it's a second, it isn't."

Requirements, Said Aloud

Three requirements are functional: record a match result, show the top ten, and show any player their own rank. Two are non-functional. A new rank shows within about a second. A reported result is never lost and never counted twice, which the running total has just turned from a nicety into a constraint on the design.

Then name what stays out: matchmaking, the anti-cheat system itself, rewards, and any board but one global one. Friends and countries come back later as curveballs, and an interviewer who wants them sooner can say so now.

The Load

Results first, one operation at a time. Ten million players at twelve matches each is 120 million results a day. A day is about 100,000 seconds, so that is about 1,200 results a second. A game has a prime time, so assume an evening peak of two and a half times the average, which gives about 3,000.

Board reads next. A board read is one player looking at the board, the top ten and their own rank together. Assume three per result. That is 3,600 a second, and 9,000 at peak. Neither figure decides anything yet, because a rate means little until you know what one read costs.

What One Rank Costs

Here the two halves of a board read come apart. The top ten is the same for everyone and touches ten entries. A rank differs for each player, and the plain way to get one is to count everybody above them. The average player has half the board above them, which is 20 million index entries.

Assume all of those entries are in memory, which flatters the design. One memory access takes about 100 nanoseconds, so 20 million of them take about 2 seconds. That is one rank, on a screen that has about a second, and it decides the rest of the round. The sum comes from the latency ladder, so it claims nothing about what a database can do, and naming a product does not make it go away.

One rank at 40 million players
Counthalf the board above you
20 million entries, about 2 seconds.
Sorted setlogarithmic walk
About 25 steps, about 2.5 microseconds, 4 GB in memory.
The load, and what it asks for
Results10 million players, 12 matches each
About 1,200 a second, 3,000 at a peak of 2.5.
Board reads3 per result
About 3,600 a second, 9,000 at peak. Counting: 18,000 CPU cores. A sorted walk: 1.

Memory and Bandwidth

The way out of a count is a sorted structure in memory, entries kept in score order. You do not draw one yet, because nothing has pushed you to, but it takes two sentences to size. Assume about 100 bytes a player. Forty million players is about 4 GB, and one mid-size node holds about 100 GB.

So memory is not the problem, and you say so now, before anyone starts sizing around it. In your own words, something like: "Memory isn't the problem here, 4 GB fits anywhere. The problem is that one rank costs 2 seconds if I count."

Nine thousand reads at about 1 KB each is 9 MB a second, against more than a gigabyte a second on a 10-gigabit link, so nothing turns on bandwidth.

Three Calls, Two Entities

The API is three calls. The game server posts to the results path with the match ID, the player and the points. A GET on the board's top path returns ten names and scores. A GET on the rank path, with a player in it, returns that player's score and rank.

The three calls
POST /results
     {"match_id": "m-8841", "player": "p-1207", "points": 120}
     201, and a retry of the same match changes nothing

GET  /board/top
     200 {"top": [{"name": "aki", "score": 918400}]}, ten entries

GET  /board/rank/p-1207
     200 {"score": 41280, "rank": 18204417}

Two entities carry it. A player's score holds the player, the season, the score and when it last changed. A match result holds the match ID, the player and the points, and its unique key is what catches a retry. That second one looks like bookkeeping until the interviewer asks what happens when the game server sends a result twice.

Interview Pitfalls
  • Sizing the top ten, the easy half, and never pricing the player's own rank, so the board has no answer when the interviewer asks for it.
  • Leaving freshness unasked, then proposing a nightly job that the results screen rules out.
  • Treating the score as a value to overwrite when it is an increment, so a retried result counts twice.
  • Drawing an in-memory store before the 2-second figure exists. Asked why it is there, you can only name a product.
What Interviewers Listen For
  • The two reads separated at once: the top ten is the same for everyone, and the rank differs for each player.
  • One rank priced from the latency ladder, with no claim about what a database can do.
  • The question about how exact a rank must be far down, a sign the shards are already in view.

Knowledge Check

One clarifying question decides whether a scheduled job could do this work at all. Which one?

  • How many players are on the board over the whole season
  • Whether the score is a best result or a running total
  • How soon after a match the player's new rank has to show
  • Whether the phone or the game server reports each result

Forty million players, half the board above the average one, 100 nanoseconds an entry. What does one count cost?

  • About 4 seconds, since a count has to walk the whole board
  • About 2 milliseconds, which no player on that screen would notice
  • About 2.5 microseconds, since the index finds it in about 25 steps
  • About 2 seconds, past the second the results screen allows

The score is a running total. Why does every reported result have to carry a match ID?

  • A retry is another increment, so without a key it counts twice
  • A phone can lie, and the ID proves that the match took place
  • The rank query needs the ID to find a player's latest score
  • Without it the transaction cannot tell which season the points are for

Which estimate gets one sentence and is then dropped, because nothing turns on it?

  • The rank, 20 million entries at 100 nanoseconds each
  • The bandwidth, 9 MB a second against a 10-gigabit link
  • The results, about 3,000 a second at the evening peak
  • The board reads, about 9,000 a second at the peak

You got correct