MongoDB
Designing a Real-Time Leaderboard
Build a gaming leaderboard that handles millions of score updates per second using Redis sorted sets and efficient ranking algorithms.
S
srikanthtelkalapally888@gmail.com
Designing a Real-Time Leaderboard
A leaderboard ranks players by score, updating in real-time as scores change.
Requirements
- Update scores in real-time
- Get top 100 players
- Get a player's rank
- Handle 1M+ concurrent users
- Multiple time windows (daily, weekly, all-time)
Why Redis Sorted Sets?
Redis sorted set (ZSET) stores members with scores and maintains sorted order.
ZADD leaderboard 5000 "player:alice"
ZADD leaderboard 3500 "player:bob"
ZADD leaderboard 4200 "player:charlie"
→ Automatically sorted: alice(5000) > charlie(4200) > bob(3500)
Key Operations
# Update score
ZINCRBY leaderboard 500 "player:alice"
# Get top 10
ZREVRANGE leaderboard 0 9 WITHSCORES
# Get player rank (0-indexed)
ZREVRANK leaderboard "player:alice"
# Get players around user (±5)
ZREVRANGEBYSCORE leaderboard +inf -inf LIMIT offset 11
Time-Based Windows
leaderboard:all_time
leaderboard:weekly:2026-W06
leaderboard:daily:2026-02-15
Use TTL to auto-expire old windows.
Persistence
Redis ZSET is primary for rankings; write-through to PostgreSQL for durability.
Sharding for Scale
For 100M+ players, shard by player_id range:
- ZSET 1: players A-F
- ZSET 2: players G-M
Merge top-K from each shard for global leaderboard.
Conclusion
Redis sorted sets are the perfect data structure for leaderboards — O(log N) updates, O(log N + K) top-K retrieval.