HYPERLOGLOG

Count distinct items in kilobytes, whatever the cardinality

L318 MINALGORITHMS
PREDICT

HyperLogLog keeps m = 64 registers, each holding the largest rank (position of the first 1 bit) it has seen in item hashes. All start at 0. You add 10 distinct items and apply the raw formula α·m²/Σ2^(−reg) with α = 0.709. About what does it estimate?

HyperLogLog hashes every item. The first p bits pick one of m = 2^p registers. In the remaining bits it finds the first 1: seeing a long run of zeros first is rare, so a large rank means many distinct items. Each register keeps the largest rank it has seen.

The estimate is α·m² divided by Σ2^(−reg), a harmonic mean across registers. Its standard error is about 1.04/√m. When most registers are still 0, that formula is biased high, so for small counts HyperLogLog switches to linear counting: m·ln(m/zeros).

▶ SIMULATION
REGISTERS (m = 64)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
zeros: 64 of 64
LAST ITEM
—
ESTIMATE
true distinct 0 · items added 0
estimate (linear counting) —
error — · allowed 39%
raw α·m²/Σ2^(−reg)0.709 · 64² / 64.000 = 45.4
linear counting m·ln(m/zeros)64 · ln(64/64) = 0.0
✓ relative error ≤ 3 × 1.04/√m
SCHEDULE
01[HLL]configure: always use α·m²/Σ2^(−reg)
02[ADD]add user-0 … user-9 (10 new items)
03[ADD]add user-10 … user-39 (30 new items)
04[ADD]add user-40 … user-199 (160 new items)
05[ADD]add user-200 … user-999 (800 new items)
06[ADD]add user-0 … user-999 again (1000 repeats, 0 new)
07[ADD]add user-1000 … user-4999 (4000 new items)
08[REPORT]report the worst relative error seen
Start of raw estimate only scenario. 8 steps.
HYPERLOGLOG  (m = 2^p registers)

ADD(x):
  h    = hash32(x)
  idx  = top p bits of h
  rank = position of the first 1 in the other 32 − p bits (1-based)
  reg[idx] = max(reg[idx], rank)

ESTIMATE():
  Z = Σ 2^(−reg[j])
  E = α_m · m² / Z          α_16 = 0.673, α_32 = 0.697, α_64 = 0.709,
                            α_m = 0.7213 / (1 + 1.079/m) for m ≥ 128
  if E ≤ 2.5m and V = (registers equal to 0) > 0:
    E = m · ln(m / V)       linear counting (small-range correction)
  if E > 2^32 / 30:  E = −2^32 · ln(1 − E / 2^32)   (32-bit hash only)

Standard error ≈ 1.04 / √m.  p = 14: m = 16384, 6-bit registers,
12 KB, about 0.81%. Repeats never change a register: it counts distinct.
Merge two sketches by taking the max of each register.

Watch ESTIMATE and the invariant strip. RAW ESTIMATE ONLY reports 50 for 10 items and fails the 3 × 1.04/√m bound until enough registers fill. The fix uses the same registers and the same items and stays inside the bound from 10 to 5000. The repeat batch changes no register in either. Drag REGISTERS down to 4 or 8 and the bound breaks at 5000 items even with the fix: with so few registers the estimate has high variance, and the paper’s constants assume m ≥ 16.

TUNE IT NOT YET

Each register is 6 bits and your budget is 384 bytes (at most 512 registers). Find a size within budget whose worst error over the whole run, from 10 to 5000 distinct items, stays under 10%. The error shrinks like 1/√m, so halving it costs four times the memory.

GOAL: worst error < 10% with at most 512 registers

FIX IT NOT YET

Implement add(item) and estimate() for m = 2^p registers. hash32(item) returns an unsigned 32-bit integer; alpha is α_m. The grader checks small and large cardinalities at p = 8 (allowed error 3 × 1.04/√256 ≈ 19.5%) and that repeats do not change the estimate.

Mental model:

each register runs a tiny experiment: “how long a run of zeros have I seen?” One experiment is noise; m of them, combined by a harmonic mean, give an error of 1.04/√m. Repeats cost nothing because a register only keeps a max. At the low end, count the empty registers instead.

IN PRODUCTION

Redis PFADD/PFCOUNT (16384 registers, 12 KB), approximate distinct-count functions in BigQuery, Presto/Trino, Spark and Druid, and unique-visitor or unique-IP dashboards. The symptom of a missing small-range correction is small counts that are off by multiples while large ones look fine.

IN INTERVIEWS

“Count unique visitors per page per day for a billion pages.” Explain register index and rank, the harmonic-mean estimate and its 1.04/√m error, why repeats are free, the small-range switch to linear counting, and that sketches merge by register-wise max so you can roll days up into weeks.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: COUNT-MIN SKETCH →