Difficulty: Advanced
What is HyperLogLog in Redis, how accurate is it, and when would you use it instead of a Set?
HyperLogLog (HLL) is a probabilistic data structure that estimates the number of unique elements (the cardinality) in a set using a fixed, tiny amount of memory. In Redis each HLL key uses at most about 12 KB, whether you add a thousand or a billion items, with a standard error of 0.81%. The trade-off is that it only gives you a count; you cannot list the elements or check whether a specific one was added.
PFADD key element [element ...] adds items. Returns 1 if the internal registers changed, 0 otherwise.PFCOUNT key [key ...] returns the estimated unique count. With several keys it estimates the size of their union without modifying them.PFMERGE destkey key1 key2 ... stores the union of several HLLs in one key.Because adding the same item twice produces the same hash, duplicates never change the estimate. That is why HLL counts unique items, not total events.
Redis uses a sparse representation for small cardinalities, which takes far less than 12 KB, and switches to the dense 12 KB form as the count grows (controlled by hll-sparse-max-bytes, default 3000). An HLL is stored as a string, so GET returns its raw bytes.
| Need | Set | Bitmap | HyperLogLog |
|---|---|---|---|
| Exact count | Yes | Yes | No, about 0.81% error |
| Memory for 100M uniques | Gigabytes | 12.5 MB if IDs are dense | About 12 KB |
| Check membership | Yes | Yes | No |
| List members | Yes | Hard | No |
| Works with string IDs | Yes | Needs integer IDs | Yes |
PFMERGE for weekly numbers.A common mistake is merging HLLs and then trying to compute an intersection by subtraction. Intersections via inclusion-exclusion amplify the error and can be very wrong when the overlap is small.
# Track unique device IDs per day
redis-cli PFADD uv:home:2026-10-11 dev-a dev-b dev-c
redis-cli PFADD uv:home:2026-10-12 dev-b dev-c dev-d dev-b
redis-cli PFCOUNT uv:home:2026-10-12 # dev-b counted once
# Union estimate without storing anything
redis-cli PFCOUNT uv:home:2026-10-11 uv:home:2026-10-12
# Store a weekly rollup
redis-cli PFMERGE uv:home:2026-w41 uv:home:2026-10-11 uv:home:2026-10-12
redis-cli PFCOUNT uv:home:2026-w41
# Memory stays tiny
redis-cli MEMORY USAGE uv:home:2026-w41
Only the PFCOUNT and PFMERGE outputs are shown. For small sets the estimate is usually exact. Errors become visible at larger cardinalities but stay around 0.81% on average.
HyperLogLog, Cardinality Estimation, PFADD and PFCOUNT, PFMERGE, Standard Error