Count how often things appear in a huge stream, using a tiny fixed amount of memory.
A Count-Min Sketch answers "how many times did this item appear?" for a huge stream of events, using a small, fixed amount of memory. The price is that the answer can be a little too high, but never too low.
Imagine you must count how often each search word appears, across billions of searches. A normal hash map (word → count) needs one entry per different word. If there are hundreds of millions of different words, that is a lot of memory, and it keeps growing.
A Count-Min Sketch uses a table with d rows and w columns, all starting at 0. Each row has a different hash function that turns an item into a column number.
Why the minimum? A cell can hold the real count plus counts from other items that landed in the same cell. So every cell is at least the true count. The row where the item had the fewest "neighbours" is closest to the truth, so the smallest cell is the best guess. The sketch can only over-count, never under-count.
| You choose | Symbol | Rule | Meaning |
|---|---|---|---|
| Error size | ε (epsilon) | width w = ceil(e / ε) | The over-count is at most ε × (total events) most of the time |
| Failure chance | δ (delta) | depth d = ceil(ln(1 / δ)) | The chance the error is bigger than that is at most δ |
| Example | ε = 0.001, δ = 0.01 | w = 2719, d = 5 | About 13,600 counters for any number of different items |
| Structure | Answers | Error | Memory |
|---|---|---|---|
| Hash map | Exact count of every item | None | Grows with the number of different items |
| Count-Min Sketch | How often did this item appear? | Over-counts only | Fixed (d × w counters) |
| Bloom filter | Was this item seen at all? | False positives only | Fixed (bit array) |
| HyperLogLog | How many different items were seen? | About ±1 to 2% | Fixed (a few KB) |
| Count Sketch (a cousin) | How often? Allows deletes | Can be too high or too low | Fixed |
Rule of thumb for these settings: with width 8 the extra count is usually at most about e/width × events = 204, and that holds with roughly 95% confidence with 3 rows. The table is tiny on purpose, so collisions are easy to see. Real sketches use thousands of columns.
| Term | Meaning in easy words |
|---|---|
| Sketch | A small summary of a big data stream that can answer some questions approximately |
| Count-Min Sketch | A d × w table of counters used to estimate how often each item appeared |
| Width (w) | The number of columns. Wider means fewer collisions and smaller error |
| Depth (d) | The number of rows. More rows make a bad estimate less likely |
| Hash function | A function that turns an item into a number. Each row uses a different one |
| Collision | Two different items landing in the same cell |
| Over-count | An estimate that is higher than the truth. This is the only kind of error |
| Heavy hitter | An item that appears much more often than most |
| ε (epsilon) | The error size you accept, relative to the total number of events |
| δ (delta) | The chance you allow for the error to be bigger than ε |
| Streaming algorithm | An algorithm that sees each item once and keeps only a small summary |
| Choice | We gain | We lose |
|---|---|---|
| Wider table | Fewer collisions, smaller error | More memory |
| More rows | Higher confidence in the answer | More hashing per operation, more memory |
| Sketch instead of a hash map | Fixed, small memory | Exactness, and the list of items |
| Conservative update | Less over-counting | No support for deletes or merging by simple addition |
| Small counters | Even less memory | Counters can saturate on very popular items |
| One sketch per time window | Recent data stays relevant | More sketches to store and combine |