Redis HyperLogLog
How many different people visited your site today? A set gives the exact answer, but it stores every visitor ID: tens of millions of IDs cost gigabytes. A HyperLogLog (HLL) answers the same question approximately, with a typical error of about 0.81 %, using at most 12 KB of memory, no matter whether you count a hundred items or a billion.
The trade-off: a HyperLogLog only counts. It cannot tell you which items it has seen or list them.
PFADD and PFCOUNT
PFADD key element [element ...] adds elements and returns 1 if the estimated count changed, 0 otherwise. PFCOUNT returns the estimate. (The PF prefix honours Philippe Flajolet, who invented the algorithm.)
PFADD visitors:home user:1 user:2 user:3
PFADD visitors:home user:2 user:3
PFADD visitors:home user:4
PFCOUNT visitors:home
TYPE visitors:homeredis> PFADD visitors:home user:1 user:2 user:3
(integer) 1
redis> PFADD visitors:home user:2 user:3
(integer) 0
redis> PFADD visitors:home user:4
(integer) 1
redis> PFCOUNT visitors:home
(integer) 4
redis> TYPE visitors:home
string
Adding elements that were already seen does not change the count, so the second PFADD returned 0. For small numbers of elements HyperLogLog is exact; the estimate only kicks in as the count grows.
Estimating large counts
Let's add 1,000 distinct users with a small Lua loop (scripts are explained in the Lua scripting lesson) and compare with an exact set:
EVAL "for i = 1, 1000 do redis.call('PFADD', KEYS[1], 'user:' .. i); redis.call('SADD', KEYS[2], 'user:' .. i) end" 2 hll:demo set:demo
PFCOUNT hll:demo
SCARD set:demo
MEMORY USAGE set:demo
STRLEN hll:demoredis> EVAL "for i = 1, 1000 do redis.call('PFADD', KEYS[1], 'user:' .. i); redis.call('SADD', KEYS[2], 'user:' .. i) end" 2 hll:demo set:demo
(nil)
redis> PFCOUNT hll:demo
(integer) 1007
redis> SCARD set:demo
(integer) 1000
redis> MEMORY USAGE set:demo
(integer) 42168
redis> STRLEN hll:demo
(integer) 1911
EVAL replies (nil) because the script returns nothing. The HyperLogLog estimates 1007 for 1000 real members, an error of 0.7 %. It is stored as a string of only 1911 bytes, because small HyperLogLogs use a compact sparse encoding; Redis switches to the fixed 12 KB dense encoding as they fill up. The set, by contrast, keeps every member and already needs about 41 KB for a thousand short IDs, growing linearly from there.
Merging counters
PFMERGE dest src [src ...] combines HyperLogLogs into one that counts the union of their elements. PFCOUNT with several keys does the same on the fly without storing the result:
PFADD visitors:mon ada linus grace
PFADD visitors:tue grace alan ada
PFADD visitors:wed alan barbara
PFCOUNT visitors:mon visitors:tue
PFMERGE visitors:week visitors:mon visitors:tue visitors:wed
PFCOUNT visitors:weekredis> PFADD visitors:mon ada linus grace
(integer) 1
redis> PFADD visitors:tue grace alan ada
(integer) 1
redis> PFADD visitors:wed alan barbara
(integer) 1
redis> PFCOUNT visitors:mon visitors:tue
(integer) 4
redis> PFMERGE visitors:week visitors:mon visitors:tue visitors:wed
OK
redis> PFCOUNT visitors:week
(integer) 5
Five different people visited during the week, even though there were eight visits in total. This is the kind of question HyperLogLog is great at: unique counts per day merged into unique counts per week or month.
Note: You can merge (union) HyperLogLogs, but you cannot intersect them reliably. For "who visited on Monday and Tuesday" you need sets or bitmaps.
Choosing a structure for unique counts
| Structure | Exact? | Memory for 10 million IDs | Can list members? |
|---|---|---|---|
| Set | yes | hundreds of MB | yes |
| Bitmap (numeric IDs) | yes | about 1.2 MB | yes, by scanning bits |
| HyperLogLog | ~0.81 % error | 12 KB | no |
A typical analytics design keeps one HyperLogLog per page per day (uv:page:/pricing:2026-10-17) with an expiry of a few months, and merges them on demand for weekly or monthly reports.
How it works (briefly)
Each element is hashed to a 64-bit number. The algorithm splits the hash into a bucket number (one of 16,384 registers) and a remaining part, and each register remembers the longest run of leading zeros it has seen. Long runs of zeros are rare, so the longest run says something about how many different hashes have arrived. Averaging over all registers (with a harmonic mean and some corrections) gives the estimate. Since the same element always hashes the same way, duplicates never change any register.
Test yourself
What is the main advantage of HyperLogLog over a set for counting unique visitors?
- It is exact
- It uses a small, fixed amount of memory
- It can list the visitors
- It supports intersections
What does PFADD hll x return when x was already added before?
- (integer) 1
- (integer) 0
- OK
- (error)
Which command gives the unique count across several daily HyperLogLogs?
- PFCOUNT day:1 day:2 day:3
- SUNION day:1 day:2 day:3
- BITOP OR
- PFADD day:1 day:2