HyperLogLog
A tiny sketch that estimates how many distinct items a stream has, such as unique visitors, with a small known error and fixed memory.
Counting unique visitors exactly means remembering every visitor id. HyperLogLog hashes each id and keeps only, per bucket, the longest run of leading zeros seen; rare long runs imply many distinct ids. From these few numbers it estimates the count. Philippe Flajolet and colleagues published it in 2007.
The memory stays fixed however many items arrive: the Redis implementation uses up to 12 KB with a standard error of 0.81%. Sketches also merge, so daily sketches combine into a weekly unique count without reprocessing raw events. Analytics databases and warehouses use it behind their approximate distinct-count functions, which is why dashboards can show unique users over billions of events quickly.
The price is that the number is an estimate. It is fine for dashboards and retention trends, not for billing or anything a contract counts exactly.
Sources
- DMTCS: Flajolet et al., HyperLogLog: the analysis of a near-optimal cardinality estimation algorithm (2007) dmtcs.episciences.org
- Redis docs: HyperLogLog redis.io
Related terms
- Count-min sketch: A small table of counters that estimates how often each item appears in a stream; it may overcount but never undercounts.
- Bloom filter: A tiny bit array that answers “definitely not in the set” or “probably in the set”, used to skip expensive lookups.
- Retention: The share of users who come back and use a product again in later weeks or months, usually shown per sign-up cohort.
- Columnar storage (Parquet, ClickHouse): Storing a table column by column instead of row by row, so analytics that read a few columns over many rows are fast and compress well.
- Hash table: A structure that finds a value by its key in about constant time by hashing the key into a slot; fast lookups, no order.
