Part 3 of 12 · Data Structures

How does a leaderboard stay sorted under constant updates?

At a million players, a skip list updated a score in 6 µs and found a player's rank in 3 µs. A sorted array found ranks fast but took 3.4 ms per update; a plain map updated in nanoseconds but needed 22 ms per rank. That's why Redis sorted sets use skip lists. Measured, with the code.

A low view along a racing circuit's start line with grandstands, for a leaderboard

Short answer: the hero of this case, as we’ll prove, is a structure that’s cheap both to update and to ask “what’s my rank?”. A skip list with span counts (what Redis uses for sorted sets) does both in O(log n): at a million players, 6 µs per score update and 3 µs per rank lookup. A sorted array is fast to read but slow to update; a plain map is the opposite. In practice: use Redis sorted sets (ZADD, ZREVRANK, ZREVRANGE), or a database index if updates are modest.

Here’s the case. A million players, scores changing every second, and every one of them wants to know, right now, where they stand. Whatever holds the board has to absorb a constant stream of writes and still answer “what’s my rank?” instantly. The two obvious suspects each fail one half of that job, and the structure that passes both gets its guarantees from a coin toss. Skip lists are one of my favourite data structures for exactly that reason. Let’s see how, and then measure it.

The code is in systems-data-structures (Go, standard library only).

Case file

The case A million players, scores changing every second, everyone wants their rank now
Suspects A sorted array, a plain map
Culprit Each suspect fails one half of the job: the array on updates, the map on ranks
The fix A skip list with span counts, as in Redis sorted sets
Evidence Go benchmarks in systems-data-structures, on a laptop (Intel i7-8550U)

The hero’s secret: what a skip list is #

William Pugh’s idea (1990) is simple: a sorted linked list, plus “express lanes”. Every node is on level 0; about a quarter of them also on level 1; a quarter of those on level 2; and so on. A search starts on the top lane, moves right while the next node is still before the target, then drops down a lane. That’s O(log n) expected steps, with no rebalancing: each node’s height is decided by a coin toss when it’s inserted. Randomness does the balancing work that trees need careful rotations for, which is the kind of quiet cleverness I never tire of.

The rank trick is the bit that makes leaderboards click: each forward link also stores its span, how many level-0 nodes it skips. Add up the spans along the search path and you have the rank. That’s what makes ZRANK fast in Redis.

Flowchart: head L2, 40 L2, end, head L1, 60 L1, 40 L1, 20 L1, head L0, 70, 60, 50, 40, 30, 20Flowchart: head L2, 40 L2, end, head L1, 60 L1, 40 L1, 20 L1, head L0, 70, 60, 50, 40, 30, 20
Diagram source
flowchart LR
  H2[head L2] -->|span 4| D2[40 L2] -->|span 3| NIL2[end]
  H1[head L1] -->|span 2| B1[60 L1] -->|span 2| D1[40 L1] -->|span 2| F1[20 L1] -->|span 1| NIL1[end]
  H0[head L0] --> A0[70] --> B0[60] --> C0[50] --> D0[40] --> E0[30] --> F0[20] --> NIL0[end]

Ranking “40”: follow the level-2 link (span 4) and land on 40: rank 4.

The evidence: 10,000 to 1,000,000 players #

Here’s where the theory meets a stopwatch. Each board is filled with random scores, then measured on random score updates, rank lookups and top-10 reads:

Players Structure Update a score Rank of a player Top 10
10,000 skip list 1.1 µs 453 ns 2.8 µs
10,000 sorted array 4.1 µs 203 ns 254 ns
10,000 map + scan 52 ns 175 µs 2.4 ms
100,000 skip list 2.5 µs 1.1 µs 577 ns
100,000 sorted array 280 µs 517 ns 288 ns
100,000 map + scan 99 ns 1.6 ms 31 ms
1,000,000 skip list 6.3 µs 3.2 µs 429 ns
1,000,000 sorted array 3.4 ms 845 ns 676 ns
1,000,000 map + scan 354 ns 21.6 ms 391 ms
Chart: Skip list: time to update a score (µs) as the board growsChart: Skip list: time to update a score (µs) as the board grows
Diagram source
xychart-beta
  title "Skip list: time to update a score (µs) as the board grows"
  x-axis ["10,000 players", "100,000 players", "1,000,000 players"]
  y-axis "µs per update"
  bar [1.1 , 2.5 , 6.3]

Each suspect gives itself away:

  • Sorted array: binary search finds a rank quickly, but an update moves on average half the array. At a million players that’s 3.4 ms per update, about 300 updates a second on one core. Even building the board one insert at a time was too slow to finish (quadratic), so the demo sorts it once. A convincing alibi for reads, and a damning one for writes.
  • Map + scan: updates are nearly free, but a rank means counting everyone with a higher score, and a top 10 means sorting everyone. The opposite crime.
  • Skip list: never the fastest at any single operation, never slow at any. For a leaderboard, that steady all-rounder quality is exactly what you want.

A balanced tree with subtree sizes (an order-statistic tree) gives the same O(log n) bounds, so it’s not a wrong answer, just a harder one; skip lists are popular because they’re far simpler to write and to make concurrent.

Where else the same solution turns up #

Situation What I’d use
Game or app leaderboard, many updates per second Redis sorted set: ZADD board 1520 player:42, ZREVRANK board player:42, ZRANGE board 0 9 REV WITHSCORES
Sales ranking in a B2B dashboard, updated a few times a minute A database index on (score DESC, id); rank with count(*) WHERE score > $1 or a window function on a small table
Priority queue whose priorities change (job scheduling, retry ordering) A sorted set or skip list, since a heap can’t cheaply change an item’s priority by key (compare the heap in the delayed-retries post)
Time-ordered index of keys The memtables of many LSM storage engines are skip lists (LevelDB; RocksDB by default), because they support concurrent reads during inserts and ordered iteration (B-tree vs LSM post)

Traps that bite #

  • Ties: decide the tie-break (earlier time, then ID) and make it part of the sort key, or ranks will flicker between equal scores.
  • Huge boards: one sorted set with hundreds of millions of members is one hot key on one Redis node; shard by region or season, and keep a separate small set of the global top.
  • Rank of the middle: “you’re 1,284,113th” is rarely useful; show percentiles or nearby players instead (ZRANGE … REV around the player’s rank; ZREVRANGE still works but is deprecated since Redis 6.2).

Remember this

  • A leaderboard needs cheap updates and cheap ranks at once.
  • Skip lists balance themselves with coin tosses, and spans give the rank.
  • Use Redis sorted sets, with tie-breaks in the key.

Case closed: what I’d do by default #

  1. Redis sorted sets for live leaderboards.
  2. A database index when updates are modest and data must be durable.
  3. Tie-breaks in the key, and sharding before one set gets huge.
  4. Never re-sort on read at scale.

It still surprises me that a structure built on coin flips sits under so many live leaderboards. The next time you see your rank update instantly, there’s a little stack of express lanes and span counts doing the arithmetic for you.

Measured Nov 2026 with Go 1.27 on a laptop (Intel i7-8550U); nanoseconds depend on the machine, the relationships don’t. Code and full results: systems-data-structures.

Like (sign in first) 0 likes 💬 0

Written by

Vishal Kumar

Associate Director at CapGrid, running product and engineering in a CPTO capacity. 10+ years building distributed, multi-tenant SaaS and AI platforms. AWS Certified Solutions Architect - Professional. Based in Gurugram.

Comments

No comments yet.

Sign in to comment. We'll email you a link; no password needed.