Count the number of different items in a huge set with just a few kilobytes.
HyperLogLog (HLL) estimates how many different items are in a huge set (the "cardinality"), using only a few kilobytes, with an error of about one or two percent.
Suppose you flip a fair coin and write down the longest run of heads in a row. If the longest run you ever saw was 3, you probably flipped only a few times. If you saw a run of 20 heads, you must have flipped millions of times, because a run that long is very rare. The length of the longest run tells you roughly how many tries there were.
HyperLogLog does exactly this with hash bits. A good hash turns every item into a random-looking string of bits, and the same item always gives the same bits. The chance that a hash starts with k zeros is 1 in 2k. So if the longest run of leading zeros you have seen is k, you have probably seen around 2k different items.
One lucky hash could fool you, so HLL uses many registers (say 1,024) to even out the luck:
| Bits p | Registers m = 2^p | Typical error (1.04 / โm) | Memory at 6 bits per register |
|---|---|---|---|
| 4 | 16 | About 26% | 12 bytes |
| 8 | 256 | About 6.5% | 192 bytes |
| 10 | 1,024 | About 3.3% | 768 bytes |
| 12 | 4,096 | About 1.6% | 3 KB |
| 14 | 16,384 | About 0.8% | 12 KB |
| Method | Accuracy | Memory | Can merge? |
|---|---|---|---|
| Hash set of all items | Exact | Grows with every different item | Yes, but expensive |
| Bitmap / bit array (one bit per ID) | Exact | Grows with the ID range | Yes (OR) |
| HyperLogLog | About 1 to 3% | Fixed, a few KB | Yes (max per register) |
| Bloom filter | Not a count | Fixed | Yes (OR) |
| Count-Min Sketch | Counts each item, not distinct | Fixed | Yes (add) |
Repeat visits do not change the registers at all: the same user always gives the same hash, so the estimate is the same for 1, 5 or 20 visits each. A plain counter would say 1,000. Memory assumes 6 bits per register and 8 bytes per user ID in the exact list. Typical error is 1.04 / โregisters.
| Term | Meaning in easy words |
|---|---|
| Cardinality | The number of different items in a set (not the total number of events) |
| Distinct count | Another name for cardinality, as in COUNT(DISTINCT user_id) |
| HyperLogLog (HLL) | A sketch that estimates cardinality using a few KB of registers |
| Register | A small counter that stores the largest rank seen for its share of the items |
| Rank | The number of leading zero bits in the hash remainder, plus 1 |
| Precision (p) | The number of hash bits used to choose a register. There are 2^p registers |
| Harmonic mean | A way of averaging that is not thrown off by one very big value |
| Standard error | The typical size of the error. For HLL it is about 1.04 divided by โ(number of registers) |
| Linear counting | A better formula for small sets, based on how many registers are still 0 |
| HyperLogLog++ | An improved version with a 64-bit hash, bias correction and a compact form for small sets |
| Merge (union) | Combine two sketches by taking the maximum of each register pair |
| Choice | We gain | We lose |
|---|---|---|
| More registers (higher p) | Smaller error | More memory per sketch |
| Fewer registers | Tiny sketches, so you can keep millions of them | A bigger error |
| HyperLogLog instead of a set | Fixed, small memory | Exactness and the ability to list items |
| Sketch per time window | Easy โlast N daysโ questions by merging | More sketches to store |
| 64-bit hash | Works for very large counts | Slightly more work per item |
| Sparse form for small sets | Very small memory when few items | More complex code and a switch to the dense form later |