Home/Labs/ZSET Leaderboard Ranks
All 280 Labs
INTERACTIVE LAB🏆

ZSET Leaderboard Ranks Lab (Interactive)

Rank players in a Redis ZSET skip-list at log2(N) hops and break ties by finish order. Model a Redis sorted-set leaderboard: memory per member, skip-list rank cost, and top-K reads for concurrent readers.

Redis ZSET Leaderboard: Skip List Math at 50M Players

Balance score-update load and rank-read fan-out against one master + replicas, and see the DRAM bill for the sorted set.

Sorted-set DRAM6.4 GBreplicated 3×: 19.2 GB cluster
Skiplist hops / op26O(log₂ N) = ZRANK & ZADD
Write headroom50k of ~125k/smaster keeps up solo
Read capacity used30%100k ÷ 330k/s
COMMAND BOARD (season:2026-s9):

› ZADD season:2026-s9 9999.9999958 "player:99123" — 9,999 pts, finished slot 42 → fractional tail breaks score ties by recency.

› ZREVRANGE season:2026-s9 0 99 WITHSCORES → top-100 page served from the skip-list tail walk.

› ZREVRANK season:2026-s9 "player:99123" → "You are #1,402 of 50,000,000" without materializing ranks.

› Monthly rollover: archive s8 ZSET to PostgreSQL aggregates, players reseed from rank-based MMR — bounded cardinality forever.

A Sorted Set is a HashMap (member→score, O(1)) glued to a probabilistic skip list (score→rank, O(log N)) living entirely in DRAM — 50M players fit in ~6.4 GB, which is why Redis owns this problem. Reads scale horizontally with replicas; writes don't, so hot leagues shard into separate keys (per region, per game mode) and a Redis Cluster splits those keys across shards by hash slot.

How It Works Under the Hood

Game leaderboards are a sorted-set problem: Redis ZSET stores each player with a score, backed by a skip list whose rank and range queries cost O(log2 N) hops, so getting the top-K or a player's rank on tens of millions of members stays logarithmic and hot. Memory is the real bill — roughly 128 bytes per member — so a full leaderboard of 50M players is gigabytes, and you shard by game and region while keeping only the global top slice. Ties need a deterministic break, so encode secondary ordering into the fractional score as points plus an inverse finish order.

Core Architectural Principles

  • Skip-list operations cost log2(N) hops; ZREVRANGE 0 K-1 returns the top-K in one call.
  • RAM is roughly 128 bytes per member, so 50M players is about 6.4 GB before sharding.
  • Tie-break by embedding recency in the fractional score: points plus (1 - finishOrder / 1e7).
Interview Round Script

Name Redis ZSET as the standard leaderboard primitive and explain the skip-list complexity for rank and top-K. Discuss the memory ceiling and sharding by game, season, or region, plus keeping global top-N separately. Handle ties explicitly with timestamp or insertion order, and mention read patterns — polling hot keys, caching rank deltas — and why you might denormalize rank to avoid ZREVRANK storms.

Key Trade-Offs

ZSET gives O(log N) ranking in RAM but is memory-heavy at scale and single-key hot, forcing sharding and rank caching.

Related Curriculum Chapter

Design a Real-Time Gaming Leaderboard

Read Full Chapter Blueprint

Explore More Interactive Labs

View All 280 Labs