The hard query
A leaderboard must answer two questions fast: the top N players and a given player rank. With millions of players changing scores constantly, scanning and sorting on every request is far too slow.
Sorted set structures
- An in memory sorted set, as offered by Redis, keeps members ordered by score and supports rank and range queries in logarithmic time.
- Updating a score is a single operation that repositions the member.
- Reading the top slice or a player surrounding ranks is then cheap.
Sharding and approximation
A single node has limits. For global scale you shard by score range or region, then merge shards to answer global top N. For a player exact global rank, exact counting across shards is expensive, so many systems serve an approximate rank using bucketed score histograms, reserving exact rank for the top tier where it matters most.
Time windows
Daily and seasonal boards need bounded windows. Writing scores into per window sorted sets that expire keeps memory bounded and makes resets free rather than a giant delete.
Key idea
Leaderboards use sorted sets for fast rank queries, shard for global scale with approximate ranks, and use expiring per window sets for seasonal boards.