Redis Bitmaps

Difficulty: Advanced

Question

What are Redis Bitmaps, and what is a good real-world use case for them?

Answer

Redis Bitmaps are not a separate type; they are bit-level operations on a regular string value, where each bit at an offset represents a yes/no fact about something with a numeric ID. The classic use case is tracking daily active users: set bit N to 1 when user N is active today, then count the ones. With 10 million users, one day's bitmap is only about 1.25 MB.

How bitmaps work

  • SETBIT key offset 1 sets the bit at offset. Redis grows the string automatically, padding with zeros.
  • GETBIT key offset returns 0 or 1.
  • BITCOUNT key counts set bits. Since Redis 7.0 you can pass a range with a BYTE or BIT unit.
  • BITOP AND|OR|XOR|NOT destkey key1 key2 ... combines bitmaps into a new key.
  • BITPOS key 1 finds the first set bit, and BITFIELD reads or writes multi-bit integers at arbitrary offsets.
  • Because a string can be up to 512 MB, a bitmap can address 2^32 bits.

Daily and weekly active users

Say a Swiggy-style app has numeric user IDs from 1 to 10 million.

  1. On every app open: SETBIT dau:2026-10-12 <userId> 1. Setting a bit that is already 1 does nothing, so duplicates are free.
  2. DAU is BITCOUNT dau:2026-10-12.
  3. Weekly active users: BITOP OR wau:2026-w41 dau:2026-10-06 ... dau:2026-10-12, then BITCOUNT wau:2026-w41.
  4. Users active every day of the week: BITOP AND instead of OR.
  5. Did user 42 open the app today? GETBIT dau:2026-10-12 42.

The same idea works for attendance, feature flag rollouts, "has this user seen the onboarding", or a per-day login streak.

When bitmaps beat sets, and when they do not

AspectBitmapSet of user IDs
Memory for 10M users, all activeabout 1.25 MBhundreds of MB
Memory for 1,000 active users with IDs near 10Mstill about 1.25 MBa few KB
Membership checkGETBIT, O(1)SISMEMBER, O(1)
List the actual membersHard, scan bitsSMEMBERS
Union/intersectionBITOP, very fastSUNION, SINTER

Bitmaps shine when IDs are dense and the set is a large fraction of all IDs. If IDs are sparse or huge (UUIDs, or numeric IDs starting at 9,000,000,000), the bitmap wastes space and you should use a set or HyperLogLog.

Gotchas

  • Allocation spikes: the first SETBIT at offset 4 billion allocates a 512 MB string at once and can block the server. Keep offsets bounded and map IDs to a dense range.
  • BITCOUNT and BITOP are O(N) in string length. For very large bitmaps, run them off-peak or on a replica.
  • You need integer IDs. If users have UUIDs, maintain a mapping to a sequential ID.
  • Set a TTL on daily keys so old days are cleaned up, for example 90 days.
  • In Redis Cluster, BITOP needs all keys in the same slot, so use hash tags like dau:{2026-10}:12.
Counting weekly active users with bitmaps. Each day is one bitmap; OR them together and count the ones.
  1. User opens app: SETBIT dau:<date> <userId> 1
  2. Daily count: BITCOUNT dau:<date> gives DAU.
  3. Combine seven days: BITOP OR wau dau:day1 ... dau:day7
  4. Weekly count: BITCOUNT wau gives WAU.
  5. Expire old days: EXPIRE each daily key after 90 days.

Code examples

DAU and WAU with redis-cli

# Users 5, 42 and 1001 were active on Monday
redis-cli SETBIT dau:2026-10-06 5 1
redis-cli SETBIT dau:2026-10-06 42 1
redis-cli SETBIT dau:2026-10-06 1001 1

# Users 42 and 77 were active on Tuesday
redis-cli SETBIT dau:2026-10-07 42 1
redis-cli SETBIT dau:2026-10-07 77 1

redis-cli BITCOUNT dau:2026-10-06            # DAU Monday

# Active on any day, and on both days
redis-cli BITOP OR  active:any  dau:2026-10-06 dau:2026-10-07
redis-cli BITOP AND active:both dau:2026-10-06 dau:2026-10-07
redis-cli BITCOUNT active:any
redis-cli BITCOUNT active:both

redis-cli GETBIT dau:2026-10-07 5            # was user 5 active Tuesday?

Outputs of SETBIT and BITOP are omitted. Four users were active on at least one day (5, 42, 77, 1001), and only user 42 was active on both.

Key points

  • Bitmaps are bit operations on string values, up to 2^32 bits per key.
  • Perfect for dense integer IDs and yes/no facts like daily activity.
  • BITOP AND/OR/XOR/NOT combines bitmaps for cohort and retention analysis.
  • Sparse or very large IDs waste memory; use sets or HyperLogLog instead.
  • A SETBIT at a huge offset allocates the whole string at once and can block Redis.

Common follow-up questions

  • How would you track DAU if user IDs are UUIDs?
  • When would you use HyperLogLog instead of a bitmap?
  • How would you compute 7-day retention with bitmaps?
  • What does BITFIELD let you do that SETBIT does not?

Interview tips

  • Do the memory math out loud: 10 million bits is about 1.25 MB.
  • Mention the sparse ID problem; it shows you know when not to use bitmaps.
  • Compare with sets and HyperLogLog to show you can pick the right structure.

Concepts covered

Bitmaps, SETBIT and GETBIT, BITCOUNT, BITOP, Daily Active Users

More Redis & Caching questions