What is HyperLogLog in Redis and when would you use it?
Understand Redis HyperLogLog, how PFADD, PFCOUNT and PFMERGE estimate unique counts in ~12 KB, and when to use it for large-scale unique visitor analytics.
Expected Interview Answer
HyperLogLog is a probabilistic Redis structure that estimates the number of unique elements (cardinality) in a set using a fixed ~12 KB of memory, with a standard error of about 0.81%, regardless of how many items you add.
You add items with PFADD and read the estimated distinct count with PFCOUNT; PFMERGE combines several HyperLogLogs into one. Unlike a Set, it never stores the actual members, so memory stays constant whether you count a thousand or a billion uniques. The trade-off is that the answer is an approximation, not an exact count, which is acceptable for large-scale analytics like unique visitors.
- Constant ~12 KB memory no matter how many items
- Fast O(1) PFADD and PFCOUNT
- Mergeable across keys with PFMERGE
- Ideal for huge-scale unique counting
- Backed by a String, so it persists and replicates normally
AI Mentor Explanation
Picture estimating how many different fans attended a whole tournament without keeping every ticket stub. You just watch the pattern of the rarest jersey numbers you spot and infer the crowd size from that. HyperLogLog counts uniques the same way — it keeps a tiny statistical fingerprint, not the names, yet estimates the total closely.
Step-by-Step Explanation
Step 1
Create or reuse a key
A HyperLogLog is just a specially encoded String key; PFADD creates it on first use.
Step 2
Add elements
PFADD hll item1 item2 ... folds each element's hash into the register array.
Step 3
Read the estimate
PFCOUNT hll returns the approximate number of distinct items added.
Step 4
Merge sources
PFMERGE dest src1 src2 unions several HyperLogLogs, e.g. daily keys into a weekly total.
Step 5
Accept the error budget
Expect ~0.81% standard error; use it only where approximate uniques are acceptable.
What Interviewer Expects
- Understanding it estimates cardinality, not membership
- The constant ~12 KB memory and ~0.81% error
- Correct commands: PFADD, PFCOUNT, PFMERGE
- A use case like unique visitors at scale
- Knowing it cannot list or check individual members
Common Mistakes
- Expecting exact counts instead of estimates
- Trying to check membership like a Set (not supported)
- Assuming memory grows with the number of items
- Forgetting PFMERGE to combine per-period keys
- Using it for small sets where a Set would be exact and cheap
Best Answer (HR Friendly)
“HyperLogLog is a clever Redis feature that estimates how many unique things you have seen — like unique website visitors — using a tiny, fixed amount of memory. It trades a small margin of error for the ability to count billions of uniques without storing them all.”
Code Example
# Add page visitors
PFADD visitors:2026-07-21 user:1 user:2 user:1
# Approximate unique count
PFCOUNT visitors:2026-07-21 # => 2
# Combine a week of daily keys into one total
PFMERGE visitors:week visitors:2026-07-21 visitors:2026-07-20
PFCOUNT visitors:weekFollow-up Questions
- How does HyperLogLog achieve constant memory usage?
- What is the expected error rate and can it be tuned?
- When would you prefer a Set or a bitmap over HyperLogLog?
- How does PFMERGE let you build weekly or monthly totals?
- Why can't HyperLogLog tell you if a specific item was seen?
MCQ Practice
1. What does HyperLogLog estimate?
HyperLogLog approximates the number of distinct elements added, not their sum or membership.
2. Roughly how much memory does a Redis HyperLogLog use?
A HyperLogLog uses a constant ~12 KB regardless of how many items are added.
3. Which command combines multiple HyperLogLogs?
PFMERGE unions several HyperLogLogs into a destination key, useful for rolling up periods.
Flash Cards
What does PFCOUNT return? — An approximate count of distinct elements added to the HyperLogLog.
HyperLogLog memory footprint? — A constant ~12 KB per key, independent of item count.
Typical HyperLogLog error? — About 0.81% standard error.
Can HyperLogLog check membership? — No — it only estimates cardinality, it cannot confirm if a specific item was seen.