KatInterview

Design a Real-Time Top-K Heavy Hitters System

System DesignModel answer
HardModel answerSystem Designsystem-designstreamingdata-structures

Last updated

Design an architecture to identify top-k elements in high-throughput streams over rolling windows, covering data structures, exact vs. approximate models, and sharding.

Design a distributed system capable of reporting the top $K$ most frequent items (e.g., trending hashtags, top-viewed articles, or viral products) over streaming data across multiple time windows (e.g., 1 minute, 1 hour, 24 hours).

Expected Discussion Points

  1. Requirements & Scale Assumptions: Clarify functional/non-functional requirements, exact vs. approximate trade-offs, and back-of-the-envelope calculations.
  2. API & Data Models: Define consumer endpoints, event ingestion interfaces, and low-level data structures.
  3. Core Algorithmic Trade-offs: Compare streaming data structures (e.g., Min-Heap + Hash Map, Count-Min Sketch, Space-Saving) under sliding vs. tumbling windows.
  4. Architecture & Partitioning: Detail end-to-end streaming ingestion, parallel aggregations, key-partitioning mechanisms, and hot-key handling.
  5. Fault Tolerance & Queries: Explain caching strategies, data freshness guarantees, state recovery, and exact reconciliation via batch processing.

Hints

Hint 1

Consider how stream partitioning strategy (e.g., key-by item_id vs. random round-robin) affects the mathematical correctness of merging local top-K results into a global top-K list.

Hint 2

Think about how sliding time windows complicate data structures when counts can decrease—how do you handle elements falling out of the top-K bound?

Hint 3

Evaluate approximate data structures like Count-Min Sketch with Heavy Hitters heaps or the Space-Saving algorithm to constrain memory usage.