Skip to content
Sign inGet all access
Chapter 7·12 minFree

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 5
Output(integer) 0(integer) 1(integer) 0(integer) 1(integer) 0

The 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 b
Output(integer) 0string(integer) 1"\x01"

Which means a high offset allocates everything below it:

SETBIT sparse 1000 1
STRLEN sparse
Output(integer) 0(integer) 126

BITCOUNT

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 BIT
Output(integer) 0(integer) 0(integer) 0(integer) 3(integer) 2(integer) 1

Daily 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 43
Output(integer) 0(integer) 0(integer) 0(integer) 3(integer) 1(integer) 0

BITOP

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 either
Output(integer) 0(integer) 0(integer) 0(integer) 0(integer) 1(integer) 1(integer) 1(integer) 3

The 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 1
Output(integer) 0(integer) 0(integer) 0(integer) 1(integer) 1(integer) 0

HyperLogLog

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 hll
Output(integer) 1(integer) 3(integer) 0(integer) 3

PFADD 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:

terminal
redis-cli STRLEN h
Output$ # after PFADD of N distinct items, STRLEN of the key 10 items -> 47 bytes 100 items -> 277 bytes 1000 items -> 1913 bytes 10000 items -> 12304 bytes 100000 items -> 12304 bytes

Merging 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 tue
Output(integer) 1(integer) 1OK(integer) 4(integer) 4

Four 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:

terminal
redis-cli PFCOUNT h
Output$ # PFCOUNT against the true distinct count 1000 actual -> 1002 estimated (0.20% off) 10000 actual -> 9878 estimated (1.22% off) 100000 actual -> 98488 estimated (1.51% off)
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