Topic 15.2
Real-Time Leaderboards and Analytics
In one line
Real-time analytics combine Redis primitives: streams for ingest, counters and hashes for totals, sorted sets for rankings and time windows, HyperLogLog for uniques, bitmaps for activity, and time-bucketed keys with TTLs, with durable aggregates flushed to a warehouse.
Think of it like this
A stadium scoreboard. It shows live scores, the top players and attendance instantly, while the official statistics are compiled afterwards from the full record.
Key ideas
- 01
Time bucketing: one key per metric per minute or hour (
views:product:9:202609281205), with TTLs; roll up withZUNIONSTOREor sums over bucket keys for windows (last hour = 60 minute buckets). - 02
Ingest: events flow into a Redis Stream (or Kafka); aggregator workers update counters (
HINCRBY stats:{minute} product:9 1), sorted sets (ZINCRBY top:{minute} 1 product:9) and HLLs (PFADD uv:{hour} userId) in pipelines. - 03
Serve: dashboards read small, precomputed keys (top 10 for the last hour, cached for seconds); never compute heavy unions per request.
- 04
Durability: Redis holds the hot, recent window (hours to days); periodic jobs write aggregates to a warehouse or time-series database for long-term reporting.
- 05
Time series in Redis 8 (TS.ADD, TS.RANGE with aggregation, retention and downsampling rules) is an option for metric-like data; for large-scale analytics, dedicated OLAP stores (ClickHouse, Druid, BigQuery) handle history better.
Code & diagrams
# per-minute aggregates written by workers (pipelined)
HINCRBY stats:{202609281205} views 1
ZINCRBY top:{202609281205} 1 product:9
PFADD uv:{2026092812} user:42
EXPIRE top:{202609281205} 7200
# last 60 minutes top 10 (computed by a job every 10 s, cached)
ZUNIONSTORE top:lasthour 60 top:{202609281106} ... top:{202609281205}
ZRANGE top:lasthour 0 9 REV WITHSCORES
# Redis 8 time series for a metric
TS.CREATE ts:checkout:latency RETENTION 86400000 LABELS svc checkout
TS.ADD ts:checkout:latency * 183
TS.RANGE ts:checkout:latency - + AGGREGATION avg 60000Interview problem
The problem
Real-time analytics dashboard
Design a real-time dashboard for an e-commerce site: orders per minute, revenue, top products in the last hour, unique visitors today, and a live leaderboard of sellers by revenue this week. 20K events/sec.
Explain it without notes
Why use time-bucketed keys for analytics in Redis?
Practice
Implement "top 5 search terms in the last 15 minutes" with minute buckets.
Trade-offs
- ↔
Redis gives real-time freshness for recent windows; warehouses give cheap long history.
Done when you can
I can design real-time analytics with buckets, sorted sets, HLL and replayable ingest.