SD System Design In Depth
0%
lesson system-designprobabilistic-structures

Top-k heavy hitters

Module 4.3: Probabilistic Sketches & Stream Analytics Updated Sep 17, 2026
📄 Cheat Sheet
System Design Interview - Top K Problem (Heavy Hitters)
System Design Interview System Design Interview - Top K Problem (Heavy Hitters) Watch on YouTube ↗

Still the clearest end-to-end treatment: exact vs Count-Min plus heap, fast and slow paths, and distributed merging.

0:00 / --:--

Finding the Heavy Hitters (Top-K)

Finding the most frequent items in a massive data stream (e.g., "Trending Hashtags", "Top 10 IP addresses DDOSing us", "Most viewed videos today") is a classic system design problem known as the Top-K or Heavy Hitters problem.

The Exact Approach

The exact way is to maintain a HashMap of frequencies, and a Min-Heap (Priority Queue) of size K. For every event, update the hash map, then update the heap. If the stream is infinite and the cardinality is high, the HashMap will exhaust memory.

The Probabilistic Approach (Count-Min Sketch + Heap)

A Count-Min Sketch can estimate a supplied key's frequency, but it does not remember every key and therefore cannot discover candidates by itself. Pair it with a bounded candidate algorithm such as Space-Saving, or maintain an explicitly evaluated candidate set. A heap entry also needs updates when its estimate changes; simply inserting every estimate above the current minimum can retain stale entries and duplicates.

Distributed Top-K

Merging only each worker's local K is approximate and can miss the global winner: one key can rank just below K on every shard yet have the largest combined count. Use a mergeable heavy-hitters sketch with a stated error bound, send an oversampled candidate set and obtain global estimates for its union, or compute exact counts when correctness requires it. Partitioning by key makes exact merging easier but can create a hot owner.

Worked example: why local top-K merging fails

Three shards each report their local top 2.

KeyShard 1Shard 2Shard 3Global total
A1,00010101,020
B101,000101,020
C10101,0001,020
X900, ranked 2nd900, ranked 2nd900, ranked 2nd2,700

Here X happens to be second on every shard, so it survives, but with local top 1 it would vanish entirely, even though it is the global winner by a wide margin. The fixes: send an oversampled candidate list, for example top 10 times K from each shard, then ask every shard for the exact counts of the union of candidates; or partition the stream by key so each key's total lives on one node.

Worked example: Space-Saving guarantee

An hour contains N = 1 billion hashtag uses. With k = 10,000 counters, every counter's overestimate is at most N divided by k = 100,000. Any hashtag used more than 100,000 times is guaranteed to be in the summary. Memory: 10,000 entries of key plus two integers, well under 1 MB.

Approaches compared

ApproachMemoryAccuracyNotes
Exact hash map plus min-heapGrows with distinct keysExactFine when cardinality is millions, not billions
Count-Min plus heap of K candidatesSketch plus K entriesOverestimates by at most epsilon x NCandidate discovery happens as items stream past
Space-Saving, Misra-Griesk countersDeterministic bound N divided by kMergeable with additive error
Batch MapReduce or SQLDistributedExactMinutes to hours late; used to correct the fast path

Time windows: keep one summary per minute and merge the last 60 for an hourly view, or use exponential decay, multiplying all counts by a factor such as 0.99 per minute, so trending reflects recent activity. Twitter and similar platforms also normalise against a baseline, because a hashtag that is always popular is not trending.

Check yourself Question 1 of 3
Count-Min alone is not top-K recall
From: Finding the Heavy Hitters (Top-K)

Why can a Count-Min sketch not produce the top 10 hashtags on its own?

Count-Min Sketch (d = 3 rows)

Each item increments one counter per row. The estimate is the minimum across its rows, so it never under-counts; collisions only push it up. Narrow grids collide more.

Add items or stream a skewed workload.

Key Takeaways

The exact Top-K approach requires too much memory for high-cardinality streams.
Frequency estimation and candidate discovery are separate jobs; combine compatible sketches with a bounded candidate algorithm.
Merging only local K can miss a global heavy hitter; oversample candidates or use a mergeable algorithm with explicit guarantees.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue