Count-min sketch
A small table of counters that estimates how often each item appears in a stream; it may overcount but never undercounts.
A count-min sketch keeps a few rows of counters, each row with its own hash function. Every time an item arrives, one counter per row goes up. To estimate an item’s count, you read its counters and take the smallest, since collisions only ever add. Graham Cormode and S. Muthukrishnan published it in 2005.
It answers “how often” where a HyperLogLog answers “how many distinct”. Typical uses are top search terms, the most active IP addresses for rate limiting and abuse detection, and hot keys in a cache, all over streams too large to count exactly. Combined with a heap it keeps a running top-k list. Redis offers it as a built-in type.
Rare items get the worst relative error, because a few collisions can dwarf their true count. The sketch is reliable for the heavy hitters, not for the long tail.
Sources
- Journal of Algorithms: Cormode, Muthukrishnan, An improved data stream summary: the count-min sketch (2005) doi.org
- Redis docs: Count-min sketch redis.io
Related terms
- HyperLogLog: A tiny sketch that estimates how many distinct items a stream has, such as unique visitors, with a small known error and fixed memory.
- Bloom filter: A tiny bit array that answers “definitely not in the set” or “probably in the set”, used to skip expensive lookups.
- Heap (priority queue): A tree kept just ordered enough that the smallest or most urgent item is always on top, with cheap inserts and removals.
- Rate limiting: Capping how many requests a user, key or IP can make in a time window, and answering HTTP 429 above it.
