Sub-Millisecond Gaming Leaderboard: Redis Sorted Sets & DynamoDB
Handling 100,000 score updates per second with real-time global player ranking using ElastiCache Redis Sorted Sets and persistent DynamoDB archiving.
1. Business Problem & Context
A multiplayer mobile game with 10 million daily active users updates player scores every time a match finishes. Generating a real-time global leaderboard using relational SQL (SELECT player_id, score, RANK() OVER (ORDER BY score DESC)…) locked table indexes and took over 8 seconds per query.
2. Requirements & Constraints
- Sub-Millisecond Rank Retrieval: Fetch the global Top 100 or a player’s exact rank in < 2ms.
- Massive Write Ingestion: Process up to 100,000 score submissions/second during tournaments.
- Zero Data Loss: Ensure historical scores survive in-memory cache restarts.
3. Architecture Overview & Data Flow
Interactive Architecture Diagram (Use controls to zoom & pan)
4. AWS Services Used & Rationales
AWS Services Architecture Rationale
Concrete reasons why these specific services were chosen over alternatives
| Service | Category | Architectural Rationale ("Why this service?") |
|---|---|---|
| Amazon ElastiCache Redis (Sorted Sets) | Database | Provides skip-list backed Sorted Sets that calculate rank dynamically with O(log(N)) complexity. |
| Amazon DynamoDB | Database | Durable persistent store for all historical matches and player season stats. |
| Amazon Kinesis Data Streams | Analytics | Decouples in-memory cache updates from slower persistent database writes. |
5. Key Design Trade-offs
Architecture Decision & Trade-Off Matrix
Evaluating alternative approaches under real-world constraints
Relational SQL ORDER BY + Window Functions
- + Simple SQL table schema
- − O(N log N) full table scans
- − High disk lock contention
- − Unusable at scale
Redis Sorted Sets + Async DynamoDB (Chosen)
✓ Chosen Design- + 0.6ms response times
- + Pre-calculated real-time ranks
- + Asynchronous persistent durability
- − Requires dual write architecture
6. Implementation Highlights
Redis Commands Redis Sorted Set Commands for Ranking
# Add or update player score atomically:
ZADD leaderboard:season_5 98450 "player_u9921"
# Retrieve top 10 players instantly (Rank 1 to 10):
ZREVRANGE leaderboard:season_5 0 9 WITHSCORES
# Fetch specific player's exact rank:
ZREVRANK leaderboard:season_5 "player_u9921" 7. Results & Key Metrics
- Query Latency: Dropped from 8,400ms down to 0.6ms.
- Throughput: Succeeded in handling 115,000 score submissions/sec during global championship tournament.
8. Key Architectural Takeaways
Algorithm Architecture: When data requires real-time sorted ranking, use data structures built for sorting (Skip Lists in Redis Sorted Sets) rather than forcing relational databases to sort millions of rows on demand.