HyperLogLog explained in plain terms means understanding how a system can count billions of distinct values using only a few kilobytes of memory. The central question is how to answer how many different users visited a site without storing every user, and the answer is a sketch: a small summary that estimates the count instead of counting exactly.
HyperLogLog is a probabilistic algorithm that estimates the number of distinct elements in a data set, using a fixed amount of memory that does not grow with the data. It trades a small, known error, typically around two percent, for the ability to count streams far too large to store.
The idea is simple: hash every value, watch the pattern of the hashes, and infer how many distinct values must have passed through. This article explains what HyperLogLog is, how it works, and where it is used.
Key takeaways
- HyperLogLog estimates how many distinct values a data set contains.
- It uses a fixed, tiny amount of memory, whatever the size of the data.
- The estimate is usually within about two percent of the true count.
- It works by hashing values and watching patterns in the hashes.
- It suits counting users, queries, and events at massive scale.
What HyperLogLog is
A HyperLogLog sketch is an array of small registers, typically a few thousand of them, each holding a small number. Every value that arrives is hashed, part of the hash chooses a register, and the rest of the hash decides what number to store in it. The registers together summarise everything the sketch has ever seen.
The estimate comes from the leading zeros in the hashes. A hash is a random-looking string of bits, and the chance that it starts with a run of zeros falls by half with each extra zero. If the longest run seen is long, many distinct values must have passed through, because only a large set is likely to produce such a run.
The algorithm was published by Philippe Flajolet and colleagues in 2007, as an improvement on the earlier LogLog algorithm. The name is a joke about the double logarithm in its mathematics, and the method is now built into Redis, BigQuery, and many other systems. Our guide to big data explains the scale of the problem it solves, and our guide to data mining explains the analysis that uses its answers.
How HyperLogLog works
Each value is hashed to a fixed-length string of bits. The first few bits select one of the registers, and the remaining bits are examined for the position of the first one bit, which gives a count of leading zeros. The register keeps the largest such count it has ever seen.
A single register is a noisy witness, because one hash tells little about the whole set. HyperLogLog therefore uses many registers, typically 16,384 of them, and combines their values with a harmonic mean, which smooths out the noise and produces a stable estimate.
The memory cost is tiny. Each register holds a number up to about 32, which fits in a single byte, so a sketch with 16,384 registers uses 16 kilobytes, whether it has seen a thousand values or ten billion. The memory never grows, which is the point of the design.
Sketches can be merged. Two sketches built from different parts of the data can be combined by taking the larger value in each register, and the result is the sketch of the union. This makes HyperLogLog easy to distribute: each machine builds its own sketch, and they are merged at the end.
The error is bounded and known. With the standard number of registers, the estimate is within about two percent of the true count, and the error shrinks as more registers are used. For most counting questions, two percent is close enough, and the savings in memory and time are enormous.

Comparing HyperLogLog with other counting methods
HyperLogLog is one of several ways to count distinct values, and each was built with a different emphasis. An exact set stores every value and never errs, a bitmap works when the values are small numbers, and a sample counts only part of the data, but each approach trades accuracy for memory in a different way.
The table below sets out the main contrasts between the leading approaches.
| Aspect | HyperLogLog | Exact set | Bitmap |
|---|---|---|---|
| Memory | Fixed, about 16 KB | Grows with every value | One bit per possible value |
| Accuracy | About 2 percent error | Exact | Exact |
| Merging sketches | Easy, take the maximum | Expensive, union the sets | Easy, OR the bits |
| Works for | Any hashable value | Any value | Small integer ranges only |
| Best for | Huge streams, approximate counts | Small sets, exact answers | Dense integer sets |
The practical result is that HyperLogLog is the right choice for counting distinct values in huge streams, an exact set suits small data where the answer must be perfect, and a bitmap fits dense ranges of small integers.
Where HyperLogLog is used
Web analytics uses HyperLogLog to count unique visitors. A site with millions of daily visitors cannot store every visitor identifier, but a sketch a few kilobytes in size can estimate the daily count to within a couple of percent, which is enough for reporting.
Databases use HyperLogLog to estimate the size of a query result. A query planner needs to know how many distinct values a column holds before it chooses a plan, and a sketch gives that estimate without scanning the whole table.
Advertising platforms count unique reach with sketches. An advertiser wants to know how many different people saw a campaign, and a sketch built from the stream of impressions answers that without storing a record for every person.
Network monitoring uses HyperLogLog to count distinct addresses. A router that sees millions of flows can estimate how many different hosts it talked to, and spot a sudden change, using a sketch that fits in a few kilobytes of memory.
Search engines use sketches to estimate how many documents match a term. The estimate guides ranking and caching decisions, and it is far cheaper to maintain than an exact count that must be updated on every change.
Strengths and limits
The greatest strength of HyperLogLog is that its memory never grows. A sketch that counts a billion distinct values uses the same few kilobytes as one that counts a thousand, which makes it possible to count streams that could never be stored.
It is also a strength that sketches merge cleanly. Work can be split across any number of machines, each building its own sketch, and the merged result is the same as if one sketch had seen all the data.
The limits are real, though. The answer is an estimate, not a count, and it cannot list which values were seen or answer how often a particular value appeared. When the exact set matters, a sketch is the wrong tool.
HyperLogLog checklist
- Use HyperLogLog when you need a distinct count, not the distinct values.
- Accept the error; a two percent estimate is the price of fixed memory.
- Use the standard 16,384 registers unless you have a reason to change it.
- Hash consistently; the same value must always hash the same way.
- Merge sketches by taking the maximum in each register.
- Do not use it to count values you must later list.
- Test the error on your own data before trusting the estimate.
- Combine it with a Count-Min Sketch when you also need frequencies.
- Remember that the sketch forgets nothing about the count, but nothing about the values.
Frequently asked questions
What is HyperLogLog in simple terms?
It is an algorithm that estimates how many different values a data set contains, using a small fixed amount of memory, by watching patterns in the hashes of the values.
Why is it called HyperLogLog?
After the double logarithm in its mathematics, and as a joke on the earlier LogLog algorithm it improved upon.
How accurate is HyperLogLog?
With the standard number of registers, usually within about two percent of the true count.
Can HyperLogLog tell me which values it saw?
No. It keeps only a summary of the hashes, so it can estimate the count but cannot list the values.
Can two HyperLogLog sketches be combined?
Yes, by taking the larger value in each register, which gives the sketch of the union of the two data sets.
When should I not use HyperLogLog?
When the count must be exact, when the data set is small enough to store, or when you need to know which values appeared.
The bottom line
HyperLogLog explained in one line is this: hash every value, watch the patterns, and estimate how many distinct ones there were.
It is the sketch that lets a system count billions in kilobytes, and the reason approximate answers are often the right answers.
This article is general information about how HyperLogLog works, not advice about choosing counting methods for a particular system.







