Scrivio Interview library
ScenarioAdvanced6 min read · updated 13 November 2026

A global game expects 400,000 score updates per second and 300 million ranked players next season, more than one sorted set on one Redis primary can handle in CPU and memory.

How do you shard a leaderboard while still answering top-100 and what-is-my-rank queries, and which accuracy trade-offs would you accept?

Shard by player: hash each player id into one of, say, 64 sorted sets spread across the cluster, so writes spread evenly and each player's score lives in exactly one shard. The top 100 is built by a job once per second that reads the top 100 of every shard and merges them. An exact rank needs ZCOUNT with an exclusive lower bound on all 64 shards plus the position among equal scores, so it is offered sparingly or cached, while most players get an approximate rank from a score histogram. Sharding by score range is the alternative, cheaper for top-N but with hot shards and moving players, and a durable event log lets us rebuild any shard.

Why this question is asked

At 400,000 updates per second and 300 million players, a leaderboard no longer fits the 'one sorted set' recipe: one primary's thread and memory cannot hold it. Interviewers ask this to see whether we can split a structure whose main queries, top N and rank, are global by nature, and whether we are comfortable trading exactness for scale where players will not notice. In this answer, we will size the problem, shard by player, answer top 100 and exact rank on the sharded data, add an approximate rank for the long tail, compare sharding by score range, and plan for rebuilds.

1Why One Sorted Set Is Not Enough

A sorted set lives on one primary. Three hundred million members at roughly 100 bytes each plus ids is around 30 to 40 GB in one key, more than is comfortable for one Redis process to hold, replicate and snapshot. And 400,000 ZADD calls per second, each O(log N) in a skiplist of 300 million, all on one main thread, is beyond what one node can sustain with headroom. So the board must be split into many keys on many primaries.

2Shard by Player

We pick a fixed number of shards, for example 64, and send each player to shard hash(playerId) mod 64. Each shard is a sorted set, lb:s:{17}, and the braces make the shard number the hash tag, so Redis Cluster spreads the 64 keys over its primaries. Writes spread evenly because player ids hash uniformly, and each player's score lives in exactly one shard, so updates stay simple: one ZADD to one key.

Java
int shard = Math.floorMod(murmur3(playerId), 64); // 17
redis.opsForZSet().add("lb:s:{" + shard + "}", playerId, score); // ZADD lb:s:{17} 1450 p42

Notice that the number of shards is fixed up front. Changing it later means moving players between shards, so we choose a number with room to grow and let the cluster move whole shards between nodes as it grows.

3Top 100 by Merging Shards

The global top 100 must be among the top 100 of the shards combined, since a player outside their shard's top 100 has at least 100 players above them. So a job reads ZRANGE 0 99 REV WITHSCORES from each of the 64 shards, which is 6,400 entries, merges them in memory and keeps the best 100. That is 64 cheap reads, done once per second and published to the API servers, not on every request.

Step throughBuilding the global top 100 each second
  1. Read top 100 from each of 64 shardsPipelined, 64 small replies
  2. Merge 6,400 entriesSort by score, then time
  3. Keep the best 100Global top 100
  4. Publish to API serversServed from memory

4 steps in order. Play them, or step through them one at a time.

4Exact Rank: Count Above, Across Every Shard

A player's exact global rank is the number of players with a higher score, across all shards, plus their position among players with the same score. ZCOUNT counts members in a score range in O(log N), and putting ( before the bound makes it exclusive.

SQL
-- p42 has score 1450
ZCOUNT lb:s:{0} (1450 +inf -- (integer) 18211
ZCOUNT lb:s:{1} (1450 +inf -- (integer) 18054
-- ... 62 more shards, pipelined
-- sum = 1,163,900 players strictly above p42
-- plus position among players with exactly 1450 (tie-break, e.g. ZCOUNT 1450 1450 with time encoding)
-- rank = 1,163,900 + tie position + 1

That is 64 calls per rank query. Pipelined to the cluster it is fast for one query, but 64 times the query rate in Redis operations, so we offer exact rank sparingly: on a profile page, for the top tens of thousands, or cached per player for a few seconds. If the score already encodes the time as a tie-break, the exclusive count across shards gives the exact position directly.

5Approximate Rank for the Long Tail

A player ranked 147,203,881st does not care about the exact number; 'top 49 percent' is more meaningful. We keep a histogram: a hash where each field is a score bucket, for example buckets of 10 points, and the value is the number of players in it. Each score update moves the player from the old bucket to the new one with two HINCRBY calls. To estimate rank, we sum the buckets above the player's score, a few thousand numbers that the API can cache for a second or two, and turn it into a percentile.

This gives a rank good to within the bucket width at the cost of one small hash, and it is what we show to everyone outside the top few thousand.

6The Alternative: Shard by Score Range

We could instead put scores 0 to 999 in one shard, 1,000 to 1,999 in the next, and so on. Top 100 then reads only the highest shard, and rank is the sizes of the higher shards plus a rank within one shard, both cheap. But players move between shards as their scores change, which turns an update into a remove and an add across nodes, and the distribution is rarely even: the top shard is small but hot, the middle ones are huge. Range boundaries need rebalancing as the season progresses.

Choosing a sharding key
AspectShard by playerShard by score range
Write spreadEvenUneven, moves between shards
Top 100Merge 64 shards, once per secondRead one shard
Exact rank64 ZCOUNTsShard sizes plus one ZREVRANK
Hot spotsNoneTop shard, crowded middle
RebalancingFixed shard countBoundaries move during a season
Choose by query mix: write-heavy boards usually shard by player.

7Follow-up questions

How would you handle a season reset at midnight for 300 million players?

Do not delete anything at midnight. Put the season in the key names, lb:season:12:s:{17}, so writes after midnight go to new, empty keys and the reset is instant. The old season's keys stay readable for final standings and are removed later with UNLINK, which frees memory in a background thread, spread over time so the cluster is not hit by 64 large deletions at once. The final standings are also computed from the durable event log for prizes.

Why keep a durable event log if Redis replicates?

Replication protects against a node failure, not against a bug that writes wrong scores, an accidental FLUSHALL, or a change of sharding scheme. An event log of score changes lets us rebuild any shard or the whole board from scratch, and settle disputes about rankings.

Can the top 100 miss someone because of the per-shard cut?

No. If a player is in the global top 100, at most 99 players anywhere beat them, so at most 99 in their own shard do, which puts them in their shard's top 100. Merging the per-shard top 100 lists is therefore exact.

8Conclusion

In this answer, we saw that 300 million players and 400,000 updates per second need the board split across many keys. Sharding by player hash spreads writes evenly; the global top 100 is an exact merge of each shard's top 100, done once per second; exact rank is a sum of exclusive ZCOUNTs over all shards plus the tie position, offered sparingly; and most players get an approximate rank from a score histogram. Sharding by score range makes top-N cheap but creates hot shards and moving players, and a durable event log lets us rebuild any shard.

What to say in the interviewAbout 90 seconds aloud

Open with

One sorted set cannot take 300 million members and 400,000 writes a second on one primary, so I would shard by player: hash the player id into, say, 64 sorted sets with hash tags so the cluster spreads them over its primaries.

Then, if they want more

Writes spread evenly and each player lives in exactly one shard. For the top 100, a job reads the top 100 of every shard once per second, merges the 6,400 entries and publishes the best 100; that is exact, because anyone in the global top 100 must be in their shard's top 100.

For an exact rank, I sum ZCOUNT with an exclusive lower bound, the open bracket before the score, up to plus infinity across all 64 shards, then add the position among equal scores. That is 64 calls per query, so I would offer it sparingly or cache it per player.

For the long tail I would keep a histogram of players per score bucket and show something like top 3 percent, which is what those players care about. The alternative is sharding by score range, which makes top N cheap but moves players between shards and makes the top shard hot; I would choose by the query mix.

And I would keep a durable event log so any shard can be rebuilt.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you shard a leaderboard while still answering top-100 and what-is-my-rank...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/0bf74922ea/