Top-K / Space-Saving
Tracks the approximate most frequent items ("heavy hitters") in a stream using only k counters, no matter how many distinct items appear. Complements Count-Min Sketch: CMS answers "how many times has X appeared", Top-K answers "what are the most frequent items".
How it works
The Space-Saving algorithm keeps exactly k counters, each holding an item, a count, and an error bound. On add(item):
- If
itemalready owns a counter, just increment it (an exact count for that item). - Otherwise, if a counter is still free, give it to
itemwith count 1 and error 0. - Otherwise, all
kcounters are occupied by other items: evict whichever counter has the smallest count, and give it toitemwith count =min + 1and error =min. The error records that the new item might secretly have appeared up tomintimes before it stole this counter.
This guarantees count - error ≤ trueCount ≤ count for every tracked item, and -- the key "space-saving" property -- any item whose true frequency exceeds N / k (N = total items seen) is mathematically guaranteed to be tracked, so k counters is enough to never lose a real heavy hitter, even though it can't perfectly rank the long tail.
Interactive demo
Counters used0 / 5
Total items seen0
Guaranteed heavy hitter—
SpaceO(k)
No counters yet. Add an item below.
No operations yet. Try adding an item above.
API surface
| Operation | Description | Complexity |
|---|---|---|
| new TopK(k) | Allocate k counters for tracking the approximate top-k items. | O(k) space |
| add(item, amount?): TopKAddResult | Increment, insert, or evict-and-replace a counter for item. | O(k) time |
| top(n?): TopKEntry[] | Return up to n tracked entries sorted by count descending. | O(k log k) time |
| estimate(item): TopKEntry | undefined | Return the counter for item if it is currently tracked. | O(1) time |
| clear(): void | Reset all counters and totalCount. | O(k) time |
References
Original papers
- Efficient Computation of Frequent and Top-k Elements in Data StreamsAhmed Metwally, Divyakant Agrawal, Amr El Abbadi · 2005
Library implementations
- Godgryski/go-topk (Filtered Space-Saving)
- Javafzakaria/space-saving (StreamSummary)
- Javaapache/datasketches-java (ItemsSketch, frequent items)
- Pythonapache/datasketches-python (frequent_items_sketch)
- RustNewbieOrange/topk (Filtered Space-Saving)
- JS/TSCallidon/bloom-filters (Top-K)
- Redis moduleRedisBloom TOPK.*
Real-world use cases
- Redis/RedisBloom TOPK.ADD/TOPK.LIST for real-time heavy-hitter tracking (trending hashtags, top search terms, most-hit API endpoints)
- Apache DataSketches Frequent Items sketches for finding the heaviest keys/items in large-scale analytics pipelines
- Network traffic analysis: identifying the top-talking IPs/flows without keeping a counter per flow
- Finding the most popular products/pages/queries in a stream too large to count exactly in memory