Design Top-K system (involving data pipelines, CountMinSketch, Heap, etc.)
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2019-07-24
Problem Statement
Design a Top-K system that continuously processes massive amounts of streaming data (e.g., search queries, clicked URLs, trending hashtags, songs played) and efficiently returns the Top-K most frequent items within a time window (real-time or near real-time).
This problem emphasizes distributed system design, streaming pipelines, and approximate algorithms (since exact counting at scale may be infeasible).
✅ Functional Requirements
1.Data Ingestion
•Ingest real-time event streams (e.g., queries, clicks, hashtags).
•Handle very high throughput (millions of events/sec).
2.Frequency Estimation
•Maintain approximate frequency counts of items using memory-efficient structures like Count-Min Sketch.
•Support per-time-window counting (sliding or tumbling windows).
3.Top-K Computation
•Maintain a Top-K list using Min-Heap or Priority Queue.
•Continuously update as new data arrives.
4.Query API
•Provide an API to fetch:
•Global Top-K (all time)
•Windowed Top-K (e.g., last 1 min / 1 hr)
5.Scalability & Reliability
•Must handle distributed ingestion & aggregation.
•Must support fault tolerance (no single point of failure).
❌ Non-Functional Requirements
•Low latency (< 1s lag for Top-K queries).
•High availability (99.99%).
•Scalable to billions of events per day.
•Memory efficient (avoid storing all raw events).
🧱 System Components
1.Data Pipeline
•Producers (user activity, clickstream, sensors).
•Message Queue (Kafka / Pulsar) → buffers events for consumers.
2.Stream Processing Layer
•Use frameworks like Flink / Spark Streaming / Storm.
•Each event updates a Count-Min Sketch (for frequency approximation).
•Maintain a Min-Heap of size K per partition for candidate Top-K items.
3.Aggregator Layer
•Periodically merges Top-K candidates from multiple partitions.
•Consolidates into global Top-K using another Min-Heap.
4.Storage
•Store historical Top-K results in NoSQL (Cassandra, DynamoDB, Redis).
•Optional: store raw sketches for replay/debugging.
5.Query API Layer
•REST/gRPC endpoints to fetch:
•Current Top-K items (real-time)
•Historical Top-K (time window based).
🔄 Example Flow
1.Event #WorldCup enters Kafka.
2.Stream processor updates Count-Min Sketch (count += 1).
3.Local Min-Heap updated (#WorldCup may enter heap).
4.Aggregator merges local heaps from all partitions → Global Top-K.
5.API returns Top-10 hashtags in the last 5 minutes.
⚖️ Trade-Offs / Design Choices
•Count-Min Sketch → saves memory, but may overestimate frequencies.
•Heap vs. Ordered Map → Heap is efficient (O(logK)), Ordered Map gives easier merges.
•Windowing → Sliding windows provide more accuracy but cost more memory & compute than tumbling windows.
•Exact vs. Approximate → Exact requires heavy storage & shuffle; Approximate trades accuracy for performance.
🧪 Open-Ended Discussion Points
•How to scale merges of local Top-K across thousands of partitions?
•How to ensure fault tolerance (checkpointing sketches/heaps)?
•How to support multi-tenant workloads (Top-K per user, region, category)?
•How to handle late arriving events in a streaming system?
👉 This problem tests HLD skills in distributed streaming pipelines + approximate algorithms + heap data structures.