Design a system that returns the Top K items (e.g., videos/posts/products) by popularity.
Requirements
-
API to fetch:
GET /topk?window=10m&k=100
returning the top 100 items in the last 10 minutes.
-
Support multiple windows (e.g., 1m, 10m, 1h, 24h).
-
Near real-time updates (freshness target e.g. < 5 seconds).
-
Read QPS is high (e.g., 10k–100k QPS); write events can be much higher (e.g., millions/minute).
-
Popularity is driven by events (views/clicks/likes). Event stream may include duplicates and out-of-order delivery.
Non-functional
-
Low latency for reads (e.g., p99 < 100 ms).
-
Fault tolerant, scalable, and cost-aware.
-
Discuss exact vs approximate Top-K and trade-offs.
Produce an end-to-end design: ingestion, storage/compute, APIs, data model, and operations/monitoring.