All Levels75 min readยท Topic 12.5

Real-time and location systems

Live leaderboard, stock ticker, Google Maps, nearby friends, food delivery

๐Ÿ“Key Takeaways

  • 1
    Live leaderboards: sorted set in Redis (ZADD, ZRANGE) handles millions of score updates + rank queries efficiently
  • 2
    Nearby friends: geospatial index (Redis GEO, PostGIS) + pub/sub for real-time location updates
  • 3
    Food delivery: three-sided marketplace (customer, restaurant, driver) with real-time ETA and order tracking
  • 4
    Geohashing: 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?