๐Key Takeaways
- 1Live leaderboards: sorted set in Redis (ZADD, ZRANGE) handles millions of score updates + rank queries efficiently
- 2Nearby friends: geospatial index (Redis GEO, PostGIS) + pub/sub for real-time location updates
- 3Food delivery: three-sided marketplace (customer, restaurant, driver) with real-time ETA and order tracking
- 4Geohashing: encode lat/long into a string prefix โ nearby locations share prefixes, enabling efficient range queries
Systems That Operate in Real-Time
Real-time systems require sub-second data freshness: leaderboards update instantly, location tracking shows live positions, stock tickers stream prices. These systems push the boundaries of eventual consistency toward real-time consistency.
System Designs
Data structure: Redis Sorted Set. ZADD to update scores (O(log N)), ZRANGE/ZREVRANGE to get top-K (O(log N + K)).
For millions of players: shard by region or game. Merge top-K from each shard for global leaderboard.
Real-time: WebSocket push leaderboard changes to connected clients. Throttle updates (every 1-5 seconds).
Advantages
- โขRedis Sorted Sets are purpose-built for leaderboards
- โขGeohashing enables efficient spatial queries
- โขWebSocket enables true real-time updates
Disadvantages
- โขReal-time systems require persistent connections (stateful)
- โขLocation tracking creates high write load
- โขThree-sided marketplaces have complex matching logic
๐งช Test Your Understanding
Knowledge Check1/1
What data structure is ideal for a live leaderboard?