← All articles

Leaderboards with sorted sets

Updated Nov 10, 2026 · 4 min read

A leaderboard answers three questions all day long. Who is on top? Where am I? Who is just above and below me? Scores change every few seconds as matches end, and millions of players ask at once. The first two are easy at small scale and surprisingly slow at large scale, which is why most leaderboards end up in a sorted set.

Why SQL struggles with rank

The top 100 is fine in SQL. With an index on score, ORDER BY score DESC LIMIT 100 reads 100 index entries. A player’s rank is the problem. The query is SELECT COUNT(*) FROM scores WHERE score > :mine, and the index can find where your score sits but then has to walk every entry above it to count them. For a player in the middle of 50 million, that’s 25 million entries, on every request.

What a sorted set is

A Redis sorted set holds members, each with a score, kept in score order. Under the hood it’s a hash table, which finds a player’s score directly, plus a skip list, which keeps everyone in order. Each link in the skip list also records how many entries it jumps over, so walking down from the top and adding up the jumps gives a rank in about log₂ N steps. For 50 million players that’s about 26.

You wantCommandCost
Add points after a matchZINCRBY board 50 playerO(log N)
A player’s rankZREVRANK board playerO(log N)
The top 100ZREVRANGE board 0 99 WITHSCORESO(log N + 100)
A player’s scoreZSCORE board playerO(1)
How many between two scoresZCOUNT board min maxO(log N)

The REV versions count from the highest score, which is what a leaderboard wants. Ranks start at 0, so the leader is rank 0 and you show it as 1st.

The players around you

Showing your neighbours takes two calls. First ZREVRANK finds your position, say 48,211. Then ZREVRANGE board 48206 48216 reads the five players above you, you, and the five below. Both are log time, so this is as cheap for the player in 40 millionth place as for the leader. Clamp the start at 0 for players near the top.

Seasons and regions

Give every board its own key, named by what it covers, like lb:s12:eu for season 12 in Europe and lb:s12:all for the global board. A finished match updates both of the player’s boards, two cheap writes. A sorted set can’t filter by region, so keeping one big board and filtering on read would mean walking it until you’d seen enough European players.

A new season is just new keys. Don’t reuse one key and wipe it, because you lose the final standings and deleting tens of millions of entries at once blocks Redis. Copy the old boards to a database for history, then let the keys expire.

Match ends+50 pointsScore serviceEU boardlb:s12:euGlobal boardlb:s12:all
One finished match updates the player’s region board and the global board for the current season.

Breaking ties

When two players have the same score, Redis orders them by member name, which usually means by player id. If your rules say whoever got there first wins, fold the time into the score. Use points × 10¹⁰ + (a far-future time − the time they scored), with times in seconds. The points still decide the order, and among equal points an earlier time leaves a bigger remainder and ranks higher. Scores are 64-bit floats, exact for whole numbers up to about 9 × 10¹⁵, so this leaves room for up to 900,000 points.

Memory

Plan on about 100 bytes per entry, counting the hash table, the skip list and the member id. Ten million players on two boards each is 20 million entries, about 2 GB. Even 80 million players fit in 16 GB on one node. Add a replica so a failed node doesn’t lose the board, and keep a durable copy of scores in a database so you can rebuild it.

Reads that can and can’t be cached

The top 100 is the same for everyone, so you can cache it for a second and answer most views without touching Redis. Your own rank and the players around you are different for every player and change with every match, so they can’t be cached. At peak, those personal reads are what you size for, and read replicas of the board are the usual way to spread them.