Skip to main content
← PROBLEMS / SYSTEM-DESIGN
An end-to-end domain learning journey

Design a 100M-Player Distributed
Leaderboard at 200K Writes/Sec.

Shard 100M players across K=16 Redis ZSETs by userId % K (12.5k writes/s/shard), merge Top-100 via Min-Heap with 500ms caching, and serve O(1) player ranks via Score-Range Histograms.
Start the journey7 stages· Last modified Nov 24, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

Design a real-time distributed leaderboard for 100,000,000 active players sustaining 200,000 score updates/sec and serving getTopK(100) and getPlayerRank(userId) in < 20 ms p99 with deterministic tie-breaking and zero-downtime season resets.

From patterns to decisions.

Shard 100M players across K=16 Redis ZSETs by userId % K (12.5k writes/s/shard), merge Top-100 via Min-Heap with 500ms caching, and serve O(1) player ranks via Score-Range Histograms.

Full walkthrough · 7 stages
01
Stage 1 · Foundationfoundation

Capacity Sizing: 100M Entries & 200K Writes/Sec.

At ~140 bytes per ZSET entry, 100M players require ~14 GB RAM. With a 50% safety margin per Redis instance (target <= 70k ops/s and <= 2 GB/shard), K = 16 shards gives 12,500 writes/s and ~875 MB data per shard.

Capacity Math

Sizing K = 16 Shards for 100M Users & 200K ZADD/s

100M Users | 200K/ssourceDivide by K = 16process8x Write HeadroomstoreStoreLinear scaling

1.Memory Budget: Keeping each shard under 1–2 GB prevents multi-second RDB fork pauses.

2.Burst Headroom: 12.5k/s per shard leaves ample CPU for parallel ZREVRANGE reads.

Caption: Sharding across 16 Redis nodes drops per-shard load to 12.5k writes/sec and ~875 MB RAM.

02
Stage 2 · Foundationfoundation

API & Write Pipeline: Kafka WAL + Idempotent ZADD.

Score updates (POST /v1/scores) publish to a userId-partitioned Kafka topic and return 202/200 quickly. Consumer workers batch updates into Redis via pipelined ZADD (using absolute scores rather than non-idempotent ZINCRBY where possible).

Write Path

Partitioned Kafka WAL -> Pipelined Redis ZADD

Score API (200K/s)sourceKafka (16+Partitions)processRedis Shard istoreStoreOrdered per-user ZADD

1.Ordering Guarantee: Partitioning Kafka by userId preserves per-player score monotonicity.

2.Pipelining: Batching 100 ZADDs per pipeline cuts network syscalls by 100x.

Caption: Partitioning Kafka by userId preserves per-player score order while feeding pipelined Redis ZADDs.

03
Stage 3 · Intermediateintermediate

Global Top-100 Scatter-Gather + 500ms Read Cache.

A background refresher (or singleflight cache-aside) runs ZREVRANGE 0 99 WITHSCORES across all 16 shards every 500 ms, merges via Min-Heap in O(16 * 100 log 16), and stores the Top-100 JSON in edge/Redis cache.

Top-K Serving

Singleflight 500ms Cache + Min-Heap Merge

100K Top-100Reads/ssource500ms Top-100 Cachestore16-Shard Min-HeapprocessStoreBackground update

1.Zero Stampede: Background refresh or singleflight ensures only 1 scatter-gather runs per 500ms.

2.Bandwidth: 16 shards x 100 entries is tiny (~50 KB total per refresh).

Caption: Caching the Min-Heap merged Top-100 for 500ms shields all 16 shards from read storms.

04
Stage 4 · Intermediateintermediate

Exact Top-10K vs O(1) Histogram Rank for 100M Players.

Players in the Top 10,000 care about exact rank (#42 vs #43), which is served from a periodically merged Top-10K snapshot. For the remaining 99.99M players, a Score-Range Prefix Histogram returns global rank in O(1) with <0.1% variance.

Tiered Ranking

Exact Top-10K Snapshot + O(1) Histogram Tail

getRank(userId)sourceScore >= Top-10KCutoff?storeElse HistogramPrefixprocessStoreNo (99.99% players)

1.Best of Both Worlds: Exact precision where competition is fiercest; O(1) scalability for 100M casual players.

2.Bucket Updates: Kafka consumers update histogram counters alongside ZADD.

Caption: Tiering exact Top-10K lookups with O(1) histogram prefix sums serves all 100M players cleanly.

05
Stage 5 · Intermediateintermediate

Deterministic Tie-Breaking & Anti-Cheat Score Guards.

Pack (score << 20) | ((MAX_TS - ts) & 0xFFFFF) into the 53-bit exact integer range of Redis doubles so earlier achievers rank higher on ties. Enforce signed server-side score deltas and anomaly bounds in the Score API so compromised clients cannot inject 999,999,999.

Integrity

Server-Signed Validation + 53-Bit Tie-Breaker

Validate Game Tokensource52-Bit CompositeScoreprocessDeterministic ZADDstoreStoreStore exact float64

1.53-Bit Safety: Never exceed 2^53 - 1 in composite score encoding.

2.Quarantine: Flagged cheaters can be ZREM'd from their single userId % 16 shard in O(log(N/K)).

Caption: Server-side delta validation and 53-bit composite tie-breaking keep rankings fair and deterministic.

06
Stage 6 · Advancedadvanced

Zero-Downtime Season Cutovers & Async UNLINK Cleanup.

Namespace keys by season (lb:s43:shard_0..15). At midnight UTC, clients atomically switch to s43 while old s42 keys are archived to S3/Iceberg and freed asynchronously via Redis UNLINK.

Lifecycle

Versioned Season Cutover + Non-Blocking UNLINK

lb:s42:shard_0..15sourceMidnight UTC Cutoverprocesslb:s43:shard_0..15storeStoreInstant activation

1.Never Use DEL: Deleting a 6.25M-node ZSET with DEL freezes Redis for seconds; UNLINK frees in background.

2.Pre-Warming: Pre-create empty s43 keys and histogram buckets 5 minutes before midnight.

Caption: Versioned season keys and background UNLINK prevent midnight reset outages.

07
Stage 7 · Advancedadvanced

Shard Failover & Stale-While-Revalidate Degradation.

Bound each shard RPC in getTopK with a 10 ms hedged timeout. If any shard times out during Sentinel/Cluster failover, serve the last good cached Top-100 with a stale telemetry metric while the Kafka consumer replays missed writes onto the promoted replica.

Resilience

10ms Shard Timeout + Stale-While-Revalidate Fallback

16-Shard Fan-OutsourceShard 7 TimeoutwarningServe Stale Top-100storeStoreGraceful degrade

1.No Cascading Retries: Never retry timed-out scatter-gather calls synchronously inside the user request path.

2.Replica Reads: Route read-heavy ZREVRANGE calls to per-shard read replicas to isolate write primaries.

Caption: Per-shard 10ms timeouts and stale-while-revalidate protect Top-K p99 latency during shard failovers.

Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

At 100M players (~14 GB total ZSET RAM) and 200k ZADD/sec, why is K = 16 sharding (`userId % 16`) effective?

Recommended Learning Flow