How HyperLogLog Estimates Distinct Values
HyperLogLog (HLL) estimates how many distinct values appear in a stream without storing the values themselves. It hashes each value, records a small summary of the hash in one of many registers, and turns those summaries into a cardinality estimate.
The problem: counting without remembering
The exact way to count unique users, request IDs, device addresses, or search terms is to keep a set of every value seen. That works until the set contains millions or billions of entries. Memory grows with the number of distinct values, and distributing or merging those sets can be expensive.
HLL trades exactness for a predictable small error. A typical configuration uses thousands of registers, each only a few bits wide. Its memory use depends mainly on the desired accuracy, not on the number of values observed.
This is why the technique appears behind metrics such as “unique visitors,” database approximate-count-distinct functions, and telemetry systems that must aggregate data from many machines.
Hashing turns values into observations
HLL needs a hash function with an approximately uniform output: every bit pattern should be about as likely as any other, and similar input values should not produce visibly similar hash patterns. The algorithm does not learn useful structure from the original value; it relies on the hash making each input look random.
Suppose the hash produces a binary string. Split it into two parts:
- The first
pbits select one ofm = 2^pregisters. - The remaining bits are inspected for their first
1bit.
The position of that first 1 is called the rank (often written rho). If the remaining bits begin with 001..., the rank is 3: two zeroes precede the first one. A hash beginning with many zeroes is unlikely, and therefore provides evidence that the input population is large.
For each value, HLL updates the selected register with the largest rank seen there:
register[index] = max(register[index], rank)
Duplicates naturally have little effect. They hash to the same result and cannot usually raise a register beyond the value already recorded. Different values can also collide, but a sufficiently wide hash makes harmful collisions rare for normal operating ranges.
Why the registers reveal cardinality
Imagine that a register has observed n effectively random hash values. The chance that at least one of them has a long run of zeroes increases with n. A rank of 1 is common; a rank of 10 is much less common and suggests that many values were tried.
HLL does not rely on one unusually lucky register. It spreads observations across many registers and combines their ranks. If the input contains few distinct values, most registers remain low or empty. As the number of distinct values grows, the typical register value rises.
The standard estimator uses a harmonic-style average of the register values:
raw_estimate = alpha * m^2 / sum(2^-register[i])
Here, m is the number of registers and alpha is a correction constant that depends slightly on m. The term 2^-register[i] gives more influence to low registers and prevents a few unusually large ranks from dominating the result.
The result is probabilistic: repeated runs with independent hash behavior will vary. With a suitable number of registers, the relative standard error is approximately:
1.04 / sqrt(m)
For example, 16,384 registers have an expected error of roughly 0.8 percent. Increasing memory reduces error, but with diminishing returns because the improvement follows a square-root relationship.
Corrections matter at the edges
The raw formula is most useful in the middle of HLL’s operating range. Implementations commonly adjust it in two important regions.
Small cardinalities
With a small input, many registers are still zero. The raw estimator can be biased because it assumes the register distribution has reached a more representative shape. HLL can instead count the number of empty registers, V, and use linear counting:
small_estimate = m * ln(m / V)
This works because the probability that a register remains empty falls predictably as values are distributed among registers.
Large cardinalities
At the other extreme, a finite-width hash has only a finite number of possible outputs. If the estimated population approaches that space, hash collisions become significant and the estimate must be adjusted or bounded. Modern implementations also use empirically measured bias corrections for the middle range, often called HLL++ techniques. These corrections are based on how the estimator behaves for known cardinalities and make practical results closer to the intended value.
The exact correction policy varies by implementation. That is why two systems can both advertise HyperLogLog yet produce slightly different answers, especially for small sets.
Merging is the scaling advantage
Two HLL summaries built with the same register count, hash function, and configuration can be merged without replaying their input. Merge each register by taking the maximum at that position:
merged[i] = max(left[i], right[i])
This works because each register is already the maximum observation from its input stream. Taking the maximum across summaries produces the maximum over the combined stream. The same operation supports aggregation across servers, time windows, partitions, and retry paths.
What can go wrong
HLL’s assumptions are important. A biased or poorly distributed hash function can make some registers receive too many values or make long zero runs too common, producing systematic error rather than harmless random variation. All producers must also use compatible hashing and register settings before their summaries can be merged.
The output is an estimate, not a promise that the answer is within a particular bound on every run. Still, its compact memory use, mergeability, and well-characterized error make HLL a practical choice when an exact set would be too large or too expensive.