Case study demo

Real-time leaderboard

Keep a live ranking with a sorted set.

An interactive demo of how this system behaves. It runs in your browser.

Case study · Live model

How a live leaderboard behaves

Players win points, ask for the top 10, and ask for their own rank. A rank is a player's place when all scores are put in order. Keep the scores in a database table, or in a sorted set, which holds the players in score order. Restart the set, and watch who waits.

Try it

Lab time 3.0 s
rank?save scorePlayers, 10,000 in the gamePlayers10,000 in the game30 ranks a secondGame server, passes requests onGame serverpasses requests on1 questions openSorted set, 10,000 players in orderSorted set10,000 players in order0 in lineDatabase, every score, kept safeDatabaseevery score, kept safethe lasting copy
  • Players — 10,000 in the game — healthy — 30 ranks a second
  • Game server — passes requests on — healthy — 1 questions open
  • Sorted set — 10,000 players in order — healthy — 0 in line
  • Database — every score, kept safe — healthy — the lasting copy
  • A question from a player (1 dot for 5 requests)
  • A score change
  • Look up a rank or the top 10
  • The answer
Time for one rank
under 1 ms
in the store, last 3 s
Steps for one rank
14
steps taken inside the sorted set
Gave up
0/ 124
no answer in 2 s; 0% of the last 5 s
Board is behind
up to date
age of the oldest score change not on it
No answer to give
0
set not ready, or player not on it
Players on the board
10,000
of 10,000; no score has been lost

Numbers add up:124 questions = 123 answered + 0 gave up + 0 not ready + 0 not on the board + 1 in progress · 127 score changes = 126 applied + 0 lost + 1 waitingThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Where the scores are kept
Store

Players are kept in score order all the time, so a rank takes a few steps.

Players

A new choice starts the model again with that many players.

What players do
30 a second
10 a second
40 a second
Sorted set

Off: this system has no database, and the sorted set in memory is the only copy of every score.

After a restart the set is filled again at 20,000 players a second, if there is a database to read from.

Top 10 now

RankPlayerScore
1player 450510,017
2player 26739,997
3player 67679,997
4player 29639,991
5player 97859,990
6player 4789,981
7player 88299,981
8player 61429,979
9player 84869,974
10player 26629,970

Player 42 has 6,894 points and stands at rank 1,651 of 10,000.

Time for one rank, and how far the board is behind (seconds)
Time for one rankBoard is behind
000001s2s3s
Show data table
TimeTime for one rankBoard is behind
1s00
1s00
2s00
2s00
3s00
3s00

What just happened

Nothing yet. Start traffic or run an experiment.

What this model simplifies
  • The store answers one request at a time, and counting one row or taking one step costs 1 millionth of a second. A real database works on several requests at once, but the rows to count stay the same.
  • The table has an index on the score: a list of the scores kept in order. So the top 10 is cheap there too, and only a rank needs the count.
  • A player gives up after 2 s, and the store then skips that question. Many real databases finish the work anyway.
  • This model counts the steps of a search that halves the players each time: 17 for 100,000. A real sorted set, such as the one in Redis, is built another way and takes about twice as many.
  • While the set is filled again, it gives no ranks at all. Some real systems answer from the half-filled set, with wrong ranks.
  • There is one sorted set on one server. A real game with many millions of players splits the board, or keeps a second server ready to take over.