Probabilistic sketch sizer
A sketch answers a counting question in a fixed amount of memory by accepting a small, known error. HyperLogLog estimates how many distinct items you have seen. A count-min sketch estimates how many times each item has appeared.
12.29 KB counts 10,000,000 distinct items to within ±1.63% about 95% of the time, against 80 MB or more for an exact set.
Inputs
The sketch has 2^p registers.
Used for the 95% band and the exact-set comparison.
Each step of p doubles the memory and cuts the error by a factor of √2. The standard error holds for large counts. For small counts (well under the number of registers) HyperLogLog++ switches to linear counting and is usually more accurate.
Redis stores 16,384 registers of 6 bits in 12,288 bytes, with a standard error of 0.81%. That is p = 14 here: the same as the figures above. Redis documentation.
What it shows
HyperLogLog hashes each item and records, in each of m registers, the longest run of leading zero bits it has seen. Long runs are rare, so they signal many distinct items. More registers mean a smaller error: 1.04 ÷ √m.
A count-min sketch is a grid of counters, d rows by w columns. Each row has its own hash, and an item adds one to one counter per row. A count is the smallest of its d counters, so it can be too high but never too low.
How to read it
- For HyperLogLog, set the precision p or a target standard error. The sketch has 2^p registers of 6 bits each.
- “Memory, dense” is the dense layout. Each step of p doubles memory and cuts error by √2, so halving the error costs four times the memory.
- “Likely range” is the 95% band, two standard errors either side of the true count. Expect that spread; it is not a hard limit.
- “Exact set, for comparison” assumes a fixed number of bytes per ID, before hash-table overhead.
- For count-min, ε sets the width and δ the depth. The “Error bound” is ε × N, where N is the total of all counts added, so it grows with the stream, not the item.
- Counter size sets memory per counter. The tool warns “Counters may overflow” if one count could pass what a counter holds.
Questions
Why does HyperLogLog use 6 bits per register?
Each register stores the position of the first 1 bit in a 64-bit hash, which never exceeds 64 and so fits in 6 bits. That is how Redis lays out its dense sketch: 16,384 registers of 6 bits, 12,288 bytes.
Can I merge two HyperLogLog sketches?
Yes, if they use the same precision and hash function. Take the larger value in each register, and the result is the sketch of the union. Count-min sketches merge by adding the two grids, again with matching size and hashes.
Why is a count-min estimate never too low?
Every item adds to its own counter in each row, so a counter holds at least the item’s true count plus whatever other items collided into it. The minimum across rows is the least inflated of those.
How accurate are these numbers?
The HyperLogLog error is the large-count figure from the paper, and the count-min bound holds for the sketch as described by Cormode and Muthukrishnan. Real libraries add bias correction for small counts, so their error there is usually lower. Heavy skew or a poor hash can make results worse.
Learn it properly
Sources
- Flajolet, P., Fusy, É., Gandouet, O. and Meunier, F. (2007). HyperLogLog: the analysis of a near-optimal cardinality estimation algorithm. AofA 2007.
- Heule, S., Nunkesser, M. and Hall, A. (2013). HyperLogLog in Practice: Algorithmic Engineering of a State of the Art Cardinality Estimation Algorithm. EDBT 2013.
- Cormode, G. and Muthukrishnan, S. (2005). An improved data stream summary: the count-min sketch and its applications. Journal of Algorithms 55(1).
- Redis documentation: HyperLogLog.
- Redis documentation: PFCOUNT.
Last updated
More tools
All tools- estimatorBack-of-envelope estimatorTurn daily users into requests per second, storage and bandwidth, with the working shown.Open tool
- simulatorConsistent hashing simulatorA hash ring with virtual nodes: key spread per node, and how many keys move when the cluster changes.Open tool
- estimatorQueue latency estimatorWhy latency explodes as servers get busy: utilisation, waiting time and Little’s law.Open tool