Bitmaps and Cardinality
Counting enormous things in very little space.
Two structures for questions about how many, both built on the string type, and both trading something away to stay small.
Bits in a string
SETBIT addresses one bit by offset, and GETBIT reads it back:
SETBIT flags 5 1
GETBIT flags 5
GETBIT flags 0
SETBIT flags 5 0
GETBIT flags 5The reply is the bit’s previous value, not the new one. And there is no bitmap type — the key is an ordinary string:
SETBIT b 7 1
TYPE b
STRLEN b
GET bWhich means a high offset allocates everything below it:
SETBIT sparse 1000 1
STRLEN sparseBITCOUNT
It counts the set bits, optionally within a range:
SETBIT u 0 1
SETBIT u 3 1
SETBIT u 9 1
BITCOUNT u
BITCOUNT u 0 0
BITCOUNT u 0 0 BITDaily active users
One bit per user id, one key per day:
SETBIT dau:2026-11-27 7 1
SETBIT dau:2026-11-27 42 1
SETBIT dau:2026-11-27 99 1
BITCOUNT dau:2026-11-27
GETBIT dau:2026-11-27 42
GETBIT dau:2026-11-27 43BITOP
It combines bitmaps into a new one, which is how you answer questions across days:
SETBIT mon 1 1
SETBIT mon 2 1
SETBIT tue 2 1
SETBIT tue 3 1
BITOP AND both mon tue
BITCOUNT both
BITOP OR either mon tue
BITCOUNT eitherThe result keeps the identities, not just the count:
SETBIT mon 1 1
SETBIT mon 2 1
SETBIT tue 2 1
BITOP AND both mon tue
GETBIT both 2
GETBIT both 1HyperLogLog
When you want a count and nothing else, a HyperLogLog trades exactness for size:
PFADD hll a b c
PFCOUNT hll
PFADD hll a
PFCOUNT hllPFADD returns 1 when the estimate moved, 0 when it did not — duplicates are free. It is
a string too, growing only until it switches to a dense form:
redis-cli STRLEN hMerging estimates
PFMERGE unions HLLs into one, and PFCOUNT over several does it without storing a result:
PFADD mon a b c
PFADD tue c d
PFMERGE week mon tue
PFCOUNT week
PFCOUNT mon tueFour distinct members across two keys, c counted once — but unlike BITOP you can never
ask which members were involved.
Exact or approximate
The error is real and small. Measured on this version:
redis-cli PFCOUNT h| Memory | Exact? | Can you ask about one member? | |
|---|---|---|---|
| Set | grows per member | yes | yes, SISMEMBER |
| Bitmap | one bit per id | yes | yes, GETBIT |
| HyperLogLog | 12 KB maximum | no, ~1% | no |