Quick revision
The short answer
PFADD adds items to a HyperLogLog and returns 1 if the estimate changed, PFCOUNT returns the estimated number of distinct items, and PFCOUNT over several keys or PFMERGE gives the distinct count of their union. A dense HyperLogLog takes about 12 KB whatever the cardinality, with a standard error of 0.81 percent, and small ones use a compact sparse encoding. In exchange we cannot list members, test whether an item was added, or remove one, so it suits approximate unique counts such as visitors, not counts that drive money.
What to remember
In this answer, we saw that a HyperLogLog estimates distinct counts from the longest runs of leading zeros in 16,384 registers. PFADD adds items, PFCOUNT estimates the count of one key or the union of several, and PFMERGE stores a union. A dense HyperLogLog uses about 12 KB with a 0.81 percent standard error, small ones stay sparse until 3000 bytes, and it is stored as a normal string. It cannot list, test, remove or reliably intersect, so we use it for approximate unique counts, not for money.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
PFADD adds items to a HyperLogLog and returns 1 if the estimate changed, PFCOUNT returns the estimated number of distinct items, and PFCOUNT over several keys or PFMERGE gives the distinct count of their union. A dense HyperLogLog takes about 12 KB whatever the cardinality, with a standard error of 0.81 percent, and small ones use a compact sparse encoding. In exchange we cannot list members, test whether an item was added, or remove one, so it suits approximate unique counts such as visitors, not counts that drive money.
Why this question is asked
Counting distinct things exactly is expensive: to know whether a visitor is new we must remember every visitor we have seen. For a site with 50 million daily visitors, a set of their ids costs gigabytes per day. HyperLogLog trades a small, known error for a fixed, tiny memory cost, and Redis has it built in. Interviewers ask about it to see whether we know the commands and, more importantly, the limits, so we choose it for the right problems. In this answer, we will see the idea behind the structure, use the three commands, look at memory and error, see how it is stored, and finish with what it cannot do and where it fits.
1The Idea Behind HyperLogLog
Imagine hashing every visitor id into a random-looking 64-bit number. In a stream of random numbers, about half start with a 1 bit, a quarter with 01, an eighth with 001, and so on. If the longest run of leading zeros we have ever seen is 20, we have probably seen about 2^20, a million, distinct values. Duplicates do not change this, because the same id always hashes to the same number.
One such estimate is very noisy, so HyperLogLog splits the hashes into 16,384 groups, called registers, using the first 14 bits of the hash, keeps the maximum run of zeros in each register, and combines all 16,384 with a harmonic mean plus corrections. Each register needs only 6 bits, because a run longer than 63 is practically impossible. That is the whole structure: 16,384 small counters.
2PFADD, PFCOUNT and PFMERGE
The commands are named after Philippe Flajolet, who invented the algorithm. They work like a set that can only answer 'how many'.
Notice that PFADD returns 1 only when at least one internal register changed, which is not quite 'a new item was added', since a new item can land on a register without raising it. PFCOUNT with several keys computes the union on the fly without storing it; PFMERGE stores the union in a destination key, which is better when we will ask the same question many times. Merging is exact in the sense that matters: the merged HyperLogLog is identical to one built by adding every item directly, because each register just takes the maximum of the inputs.
3Memory and Error
A dense HyperLogLog is 16,384 registers of 6 bits, which is 12,288 bytes, about 12 KB plus a small header. That is true for one thousand distinct items and for one billion. The standard error is 1.04 divided by the square root of the number of registers, which is 1.04 / 128 = 0.81 percent. So for a true count of 1,000,000, the estimate is usually within about 8,100 of it, and occasionally two or three times that.
Small HyperLogLogs would waste most of those 12 KB on empty registers, so Redis starts each one in a sparse encoding that stores runs of empty registers compactly. It converts to dense when the sparse form would exceed hll-sparse-max-bytes, 3000 bytes by default, which happens somewhere in the low thousands of distinct items. A HyperLogLog with a few hundred items takes only a few hundred bytes.
| Approach | Memory | Answer |
|---|---|---|
| SET of 20-byte ids | about 60 to 80 MB | exact, can list members |
| Bitmap (dense integer ids) | 125 KB per million ids in range | exact, ids must be integers |
HyperLogLog | about 12 KB | estimate, 0.81% standard error |
4It Is Just a String
Redis stores a HyperLogLog as an ordinary string value with a special header. So everything that works on keys works on it: EXPIRE gives it a TTL, COPY and DUMP/RESTORE copy it, and it replicates and persists like any other key. GET returns the raw bytes, which we can store elsewhere and later write back with SET. TYPE reports it as a string.
5What You Cannot Do With It
Because it keeps only register maxima, the items themselves are gone. We cannot list the members, cannot ask whether user:42 was added, and cannot remove an item, since we cannot know whether another item also contributed to that register. Intersections are only possible indirectly, by inclusion and exclusion: |A and B| = |A| + |B| - |A or B|. The errors of three estimates then fall on a small result, so the intersection of two sets of 10 million that share 50,000 members can be off by more than the answer itself.
So it fits where an approximate unique count is acceptable and the items do not need to be recalled: unique visitors per page, distinct search terms, distinct devices per day. It does not fit where exact counts drive money, such as billing per active user, or where we must answer 'was this user counted?'.
6Follow-up questions
Why is the union of daily HyperLogLogs correct but the sum of daily counts wrong?
A visitor who comes on Monday and Tuesday is one weekly visitor but appears in both daily counts, so adding daily counts counts them twice. Merging the HyperLogLogs takes the maximum of each register, which is exactly the state we would have reached by adding every visitor once to a weekly HyperLogLog, so PFCOUNT over the seven days or their PFMERGE estimates distinct visitors correctly, within the usual 0.81 percent.
Can I change the accuracy?
Not in Redis. The number of registers is fixed at 16,384, so the error is fixed at 0.81 percent. If we need more accuracy we need an exact structure, such as a set or a bitmap, or a different system that lets us choose the precision.
Does the hash function make results differ between Redis versions?
The format is part of Redis and stable across versions, so a HyperLogLog created on one version can be read and merged on another. We should avoid comparing estimates computed by different products, such as Redis and a data warehouse, item by item, because each uses its own hash and corrections.
7Conclusion
In this answer, we saw that a HyperLogLog estimates distinct counts from the longest runs of leading zeros in 16,384 registers. PFADD adds items, PFCOUNT estimates the count of one key or the union of several, and PFMERGE stores a union. A dense HyperLogLog uses about 12 KB with a 0.81 percent standard error, small ones stay sparse until 3000 bytes, and it is stored as a normal string. It cannot list, test, remove or reliably intersect, so we use it for approximate unique counts, not for money.
Open with
A HyperLogLog estimates how many distinct items I've added without storing the items. PFADD key item adds one or more items and returns 1 if any internal register changed, PFCOUNT key returns the estimated distinct count, and PFCOUNT with several keys, or PFMERGE into a destination key, gives the count of the union, so the same visitor across days is counted once.
Then, if they want more
Internally it hashes each item, uses 14 bits to pick one of 16,384 registers, and keeps the longest run of leading zeros in each, with 6 bits per register. That's about 12 KB for a dense HyperLogLog, no matter whether it counted a thousand or a billion items, and the standard error is 0.81 percent.
Small ones start in a sparse encoding and convert to dense when they pass hll-sparse-max-bytes, 3000 bytes by default. It's stored as a string, so TTLs, copying and replication work as usual.
What I can't do is list the members, check whether a specific item was added, or remove an item, and intersections are only possible through inclusion and exclusion, with large errors. So I'd use it for unique visitors or distinct search terms, where a small error is fine, and not for anything where an exact count drives money.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain PFADD, PFCOUNT and PFMERGE, the memory use and error of a Redis HyperLogLog, and...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/db60c5c184/