Real-Time Leaderboard (Gaming / Contests)
Problem statement
Maintain global ranking with frequent score updates, top N queries, and user rank around me with low latency during live events.
How it works
- Redis Sorted Set
ZINCRBY leaderboard {delta} {userId}— O(log N) updates. - Periodic snapshot to S3/warehouse for historical analytics; sharding by contest id.
Analogy: Live sports scoreboard — digits flip fast (increment), fans ask “what place is my team” (rank query).
High-level design
Rendering diagram…
Components explained — this design
| Component | What it is | Why we use it here |
|---|---|---|
| Game servers | Authoritative score mutations in trusted server. | Never trust client-reported scores in competitive modes. |
| Kafka score events | Durable stream of score changes. | Optional audit + offline analytics + replay for disputes. |
| Redis ZSET per contest | Sorted set for O(log N) rank updates. | Industry standard for live leaderboards under high write QPS. |
| Leaderboard API | Reads rank, top-N, neighbors around user. | Composes multiple Redis calls (ZRANK, ZREVRANGE) into stable API contract. |
| S3 Parquet snapshots | Periodic historical leaderboard dumps. | Warehouse analytics without hammering Redis for big scans. |
Shared definitions: 00-glossary-common-services.md
Low-level design
Sharding
- Key per contest:
ZSET contest:123:lbavoids single planet-scale set. - Global all-time may still be huge — approximate top using sketches or tiered boards.
Tie-breaking
- Composite score in Redis as float loses precision — encode as 64-bit integer
(score << 32) | (MAX_TIMESTAMP - time)for same score earlier submission ranks higher.
ZRANK cost
- O(log N) acceptable to millions; beyond that shard + merge approximate top-K.
E2E: increment score
Rendering diagram…
Tricky parts
| Problem | Solution |
|---|---|
| Hot contest key | Redis Cluster hash tag {contest9} slot |
| Cheat engine | Server authoritative scoring only |
| Cross-region | Active contest pinned region or CRDT approx |
Caveats
- Redis durability — enable AOF every second; accept tiny loss vs full sync disk latency tradeoff.
- Fair play: rollback tool for exploit-discovered scores batch adjust.
Azure
- Azure Cache for Redis Enterprise RediSearch optional richer queries.