What is HyperLogLog?
HyperLogLog (HLL) in Redis is a probabilistic data structure for counting the number of unique elements (cardinality) with minimal memory use.
In simple terms
If you need to find out:
- how many unique users visited a site,
- how many unique IPs / events / IDs there were,
- while not storing all the values,
→ HyperLogLog solves this problem very efficiently.
Key properties
| Property | Value |
|---|---|
| Type | A probabilistic algorithm |
| Purpose | Counting unique values |
| Memory | ~12 KB regardless of the number of elements |
| Accuracy | ≈ ±0.81% |
| Speed | Very high |
| Stores the values themselves? | No |
Important: HLL doesn't return the elements themselves, only an approximate count.
How it works (intuitively)
- Elements get hashed
- The distribution of bits gets analyzed
- A mathematical model estimates the number of unique values
Redis stores a compact statistical representation, not a list of values.
Redis commands for HyperLogLog
PFADD, add elements
redis
PFADD users 1 2 3 4PFCOUNT, get the unique count
redis
PFCOUNT usersPFMERGE, merge several HLLs
redis
PFMERGE all_users users_day1 users_day2A real-world example
Unique users per day
redis
PFADD visitors:2025-12-22 user_123
PFADD visitors:2025-12-22 user_456
PFCOUNT visitors:2025-12-22Even with 10 million users, memory usage stays at ~12 KB.
Comparison with other approaches
Set
redis
SADD users user1 user2 user3
SCARD users| Set | HyperLogLog |
|---|---|
| Exact | Approximate |
| Stores values | No |
| Memory grows | Memory is fixed |
| Fits analytics | Yes |
When to use HyperLogLog
A great fit for:
- DAU / MAU / WAU
- traffic analytics
- events, clicks, views
- large systems (millions / billions of IDs)
Not a fit if:
- 100% accuracy is required
- you need to iterate elements
- you need
exists,remove
Frequently asked questions
How large is the error margin?
Around 0.81% For example:
- the real value:
1,000,000 - the result:
~992,000 - 1,008,000
Can the error margin be reduced?
No, in Redis, the size and accuracy are fixed by the implementation.
Can elements be removed?
No. Only adding is supported.
The short rule of thumb
Need a unique count + a lot of data + minimal memory → HyperLogLog
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.