Video in progress
How do you count daily active users (DAU) and "users who visited both yesterday and today" with a Redis Bitmap, and when does this approach not work?
One Bitmap per day: the user ID is the bit position, you mark it with SETBIT, BITCOUNT gives the DAU, and BITOP AND gives the intersection of two days. It's very cheap when the IDs are dense sequential numbers; with sparse IDs or UUIDs it doesn't work.
📖 Answer
SETBIT active:2026-11-02 1042 1
SETBIT active:2026-11-03 1042 1
BITCOUNT active:2026-11-03 # DAU
BITOP AND retained active:2026-11-02 active:2026-11-03
BITCOUNT retained # visited on both days
The math: 10 million users, IDs from 1 to 10,000,000, so one day is 10,000,000 / 8 ≈ 1.25 MB. A year is about 450 MB, but you get an exact count and can also answer "who visited" (GETBIT). With a Set this would be several times more.
redis.opsForValue().setBit("active:" + LocalDate.now(), userId, true);
Long dau = redis.execute((RedisCallback<Long>) c ->
c.stringCommands().bitCount(("active:" + LocalDate.now()).getBytes()));
When it doesn't work:
- Sparse IDs: a Bitmap's size is set by its largest offset. If one user has the ID 4,000,000,000, the key grows to about 500 MB, and Redis blocks for a moment while
SETBITallocates that memory. The offset limit is 2^32 - 1. - UUID or string IDs: you need a separate mapping to turn them into bit positions, which adds complexity.
- A big
BITOP: it's O(N), and over 30 keys for monthly retention it occupies the main thread. Compute it on a replica or at night. - Cluster: all the keys in a
BITOPmust be in one slot, with a hash tag like{active}:2026-11-03.
If you only need an approximate count and the IDs are sparse, use a HyperLogLog. For an exact count with dense IDs, use a Bitmap.
Sign up to mark studied questions and save your progress.