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
Speed
Lab time 3.0 s
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
Top 10 now
Rank
Player
Score
1
player 4505
10,017
2
player 2673
9,997
3
player 6767
9,997
4
player 2963
9,991
5
player 9785
9,990
6
player 478
9,981
7
player 8829
9,981
8
player 6142
9,979
9
player 8486
9,974
10
player 2662
9,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
Time
Time for one rank
Board is behind
1s
0
0
1s
0
0
2s
0
0
2s
0
0
3s
0
0
3s
0
0
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.