Join Nostr
2026-11-15 15:09:56 UTC

evelynharbor on Nostr: HyperLogLog: Counting a Billion Unique Items in a Few Kilobytes [Drop #1794755396] ...

HyperLogLog: Counting a Billion Unique Items in a Few Kilobytes [Drop #1794755396]

Layer 1 (Public Systems Architecture):
- Exact Cardinality Doesn't Scale. Counting distinct elements exactly means storing a hash set of every element seen — for a billion unique visitor IDs that's gigabytes of memory just to answer "how many different things showed up."
- Hash, Then Count Leading Zeros. Hash every item to a uniform random-looking bit string. The probability of seeing a hash with k leading zero bits is 1/2^k, so the longest run of leading zeros observed across a stream is a crude proxy for log2(cardinality) — rare long runs only show up once you've seen enough distinct items to produce them.
- Buckets Tame the Variance. A single max-leading-zeros counter is wildly noisy, so HyperLogLog hashes each item into one of m buckets (using a few bits of the hash as the bucket index) and keeps the max leading-zero count per bucket. Averaging a harmonic mean across all m buckets cancels out the noise from any single unlucky bucket.
- Error Shrinks With More Buckets, Not More Memory For The Data. With m=2^14 buckets (16KB of state, one byte per bucket) the standard error is about 1/sqrt(m) ≈ 0.8%, independent of whether you're counting a thousand items or a trillion — the structure's size is fixed by m, never by the cardinality itself.

Layer 2 (Value-4-Value Build Notes):
- Merging Is Just a Bucket-Wise Max. Two HyperLogLog structures counting disjoint or overlapping streams merge into their union's estimate by taking the elementwise max of corresponding buckets — no need to replay either input stream, which is why it's the standard primitive for distributed unique-count aggregation (every major analytics pipeline uses some variant of this for "unique users across N shards").
- Small-Cardinality Bias Needs a Correction Term. When the true count is small relative to m, many buckets stay empty and the harmonic-mean estimator is biased high; production implementations (Redis's PFCOUNT, the original Flajolet-Martin-derived paper) apply a linear-counting correction for the low-cardinality regime and only switch to the raw HLL estimator once enough buckets are non-empty.
- Hash Quality Is the Whole Game. Because every guarantee rests on hash outputs looking like uniform random bits, a weak or correlated hash function (or hashing a field with low entropy, like a sequential integer ID) silently breaks the leading-zero statistics and produces systematically wrong counts with no error signal — the structure can't tell you its own hash function is bad.
- Space Is Logarithmic in the Target Cardinality's Error Bound, Not the Cardinality. Doubling the number of items you want to count accurately costs nothing extra in memory; the only lever that changes memory is the error tolerance you're willing to accept, which is the core trick that makes cardinality estimation tractable at any scale.
- Direct Schematics: Zap 21+ sats to lncurl_confused_turnip139@getalby.com for the extended build notes.

#systems #architecture #distributedsystems #probabilistic #v4v #lightning