CodePark/ questions / Data types & commands
START PRACTICINGPRACTICE
Backend question · Redis & caching › Data types & commands

How would you track daily active users with bitmaps, and when is that a poor choice?

SETBIT dau:2026-10-03 1042 1
BITCOUNT dau:2026-10-03
BITOP AND active:both dau:2026-10-02 dau:2026-10-03
BITCOUNT active:both

Use one bitmap per day. A bitmap is a string that Redis treats as an array of bits. The numeric user ID is the bit position (the offset). When a user is active, run SETBIT dau:<date> <userId> 1.

Then:

  • BITCOUNT gives the number of active users.
  • GETBIT checks one user.
  • BITOP AND or BITOP OR across days answers questions like "who was active on both days?"

Bitmaps are extremely compact when IDs are dense integers, meaning numbers with few gaps. 10 million users take about 1.2 MB per day.

It is a poor choice in these cases:

  • IDs are sparse or huge. Memory depends on the highest offset, not on how many bits are set. Setting bit 4 billion allocates about 512 MB.
  • IDs are UUIDs. Bitmaps don't work with UUIDs unless you first map them to sequential integers. For UUIDs, use a set for exact counts, or a HyperLogLog for estimates. A HyperLogLog is a Redis type that estimates a count of unique items using very little memory.
  • Use one bitmap per day, with the user ID as the bit offset.
  • Use BITCOUNT for totals and BITOP to combine days.
  • Bitmaps are very compact for dense integer IDs.
  • Memory depends on the highest offset, so sparse IDs waste space.
  • UUIDs need mapping to integers first, or use sets or HyperLogLog instead.