An Interactive Guide to HyperLogLog
Learn to estimate large unique counts efficiently using HyperLogLog. Dive into the algorithm, its applications, and play with an interactive app

The Problem
Imagine you’re running a large-scale online store. Thousands of users visit your website every second, and you want to know how many unique users visit each day. This sounds straightforward: just track each user by their IP address or login ID. But here’s the catch: keeping a set of every unique identifier requires huge amounts of memory, especially as the number of users grows into the billions.
How do you solve this problem without drowning in memory usage? This is where HyperLogLog, a probabilistic cardinality estimation algorithm, comes into play.
The Solution
HyperLogLog (HLL) is a clever algorithm that provides an approximate count of unique items (even billions of items) while using a tiny fraction of the memory (typically around ~1.5 kB) required by exact hash sets. It achieves this by trading off a tiny amount of precision (usually error) for massive space savings.
Play with HyperLogLog
I have created an interactive app that lets you experiment with HyperLogLog in real-time. Here is how the app works:
-
Input IP Address:
-
Click "Random IP" to generate an IP address automatically.
-
Add the entered IP to the HyperLogLog by clicking "Add to HLL".
-
-
Adjust Bucket Count:
-
Use the slider to adjust the number of buckets ().
-
Modifying this resets the HyperLogLog and clears previous data.
-
-
Add Multiple Random IPs:
- Use the preset buttons to insert 1K, 5K, 10K, 50K, or 100K random IP addresses into the sketch.
-
View Metrics:
- Compare Actual Count, Estimated Count, Difference, Margin of Error, and Actual Error in the metrics cards.
-
Inspect Buckets:
- Scroll through individual buckets to observe how HyperLogLog distributes hashed items and tracks maximum leading zero runs.
Things to Notice
-
Margin of error decreases as buckets increase: A single bucket might get unlucky with an unusually long run of leading zeros early on, but spreading observations across hundreds or thousands of buckets smooths out the variance. (Similar to how risk pooling works in insurance).
-
The error never reaches absolute zero: Because HyperLogLog is a probabilistic data structure, small statistical variance is inherent.
-
Skimping on buckets increases error: Notice what happens to the variance and error margin if you reduce the bucket count to a very small number.
How HyperLogLog Works
1. Hash Functions and Uniform Distribution
To estimate cardinality, we first hash incoming items. A good hash function is deterministic and distributes outputs uniformly across the entire bit space.
2. Leading Zeros and Cardinality
The core insight is based on coin flipping and probabilities:
-
When hashing data to a 64-bit integer, each bit is equally likely to be
0or1. -
The probability of a hash starting with consecutive leading zeros is . For example:
- Starting with
0...(): probability - Starting with
00...(): probability - Starting with
000...(): probability
- Starting with
-
If the maximum run of leading zeros observed is , we can roughly estimate that we have seen on the order of distinct elements.
3. Bucketing and Variance Reduction
A single estimate has high variance. To reduce error, HyperLogLog divides the hash space into independent buckets:
-
The first bits of the hash value determine the bucket index.
-
The remaining bits are used to count the number of leading zeros.
-
Each bucket maintains the maximum leading zero count () observed so far.
4. Combining Estimates with the Harmonic Mean
Instead of using a simple arithmetic mean (which is heavily skewed by outliers), HyperLogLog uses the harmonic mean of the estimates across all buckets:
Where:
-
: Total number of buckets.
-
: Maximum leading zeros observed in the -th bucket.
-
: A bias-correction constant derived empirically based on .
5. Bias Correction for Edge Cases
The raw estimate can exhibit bias at the extreme ends:
-
Small Range Correction (Linear Counting): When and there are empty buckets (), hash collisions are rare. HyperLogLog falls back to Linear Counting:
-
Large Range Correction: When approaches (on 32-bit hashes), corrections are applied to account for 32-bit hash space saturation.
6. Error and Memory Efficiency
The theoretical standard error of HyperLogLog is approximately:
-
With buckets (each requiring only 5 or 6 bits), the standard error is roughly , using just 1.5 KB of memory!
-
Space complexity scales as , making HyperLogLog remarkably memory-efficient.
Real-World Applications
-
Web Analytics: Platforms like Google Analytics and YouTube use HyperLogLog variants to estimate unique daily/monthly active users and video views.
-
Databases & Caches: Redis provides built-in
PFADD/PFCOUNTcommands taking ~12 KB for up to items; TimescaleDB uses it for fast time-series rollups. -
Big Data & Query Engines: Apache Druid and Presto / Trino use HyperLogLog for ultra-fast
COUNT(DISTINCT ...)queries across petabytes of data.
Limitations
-
Approximation Only: HyperLogLog produces an estimate, not an exact count. If 100% exact precision is required (such as in billing or financial transactions), exact sets must be used instead.
-
Hash Function Dependency: Accuracy relies heavily on a uniform, high-quality hash function (e.g., MurmurHash3 or HighwayHash).
-
Cannot Retrieve Elements: HyperLogLog only counts unique items; it does not store or allow retrieval of the actual elements.