What is a Redis bitmap and what use cases does it enable?
Learn what a Redis bitmap is, how SETBIT, BITCOUNT and BITOP work, and how to track active users and feature flags in just a few megabytes of memory.
Expected Interview Answer
A Redis bitmap is not a separate type but a set of bit-level commands (SETBIT, GETBIT, BITCOUNT, BITOP, BITPOS) that operate on a regular String, treating it as a compact array of bits addressed by offset.
Because each flag costs just one bit, a bitmap can track a boolean state for hundreds of millions of items in only a few megabytes. You address any bit by its numeric offset, set or clear it in O(1), and aggregate across the whole key with BITCOUNT or combine multiple keys with AND/OR/XOR/NOT using BITOP. This makes bitmaps ideal when your data can be reduced to a yes/no answer per integer-indexed subject.
- Extremely memory efficient — one bit per item
- O(1) set and get on any bit offset
- Fast population counts with BITCOUNT
- Cross-key logical aggregation via BITOP
- Backed by ordinary Strings, so it persists and replicates normally
AI Mentor Explanation
Picture a giant attendance board at a stadium with one tiny light per season-ticket seat. Flipping a seat's light on marks that fan present today; counting the lit lights gives the day's turnout instantly. A Redis bitmap works the same way — each user ID is a bit, SETBIT lights it, and BITCOUNT tallies the crowd in one sweep.
Step-by-Step Explanation
Step 1
Pick an integer key space
Map each subject (user, day, item) to a stable non-negative integer offset.
Step 2
Set bits
Use SETBIT key offset 1 to mark an event; Redis auto-extends the String to fit the offset.
Step 3
Read individual flags
GETBIT key offset returns 0 or 1 for a single subject in O(1).
Step 4
Aggregate
BITCOUNT key totals set bits; BITPOS finds the first 0 or 1.
Step 5
Combine keys
BITOP AND/OR/XOR/NOT merges multiple bitmaps, e.g. users active on both day A and day B.
What Interviewer Expects
- Knowing bitmaps are String operations, not a distinct type
- The memory advantage of one bit per item
- Correct command names (SETBIT, GETBIT, BITCOUNT, BITOP, BITPOS)
- A realistic use case like daily active users or feature flags
- Awareness that offsets should map to a dense integer range
Common Mistakes
- Thinking bitmap is a separate Redis data type
- Using sparse or huge random offsets, wasting memory on a giant String
- Forgetting BITCOUNT can take a byte range for partial counts
- Assuming BITOP is O(1) rather than proportional to key length
- Storing non-boolean data where a Hash or Set fits better
Best Answer (HR Friendly)
“A Redis bitmap is a super-compact way to store lots of yes/no facts, using a single bit per item. It is perfect for things like counting how many users were active today, because it uses very little memory and answers those counts almost instantly.”
Code Example
# Mark user 1023 active on 2026-07-21
SETBIT dau:2026-07-21 1023 1
# Was user 1023 active?
GETBIT dau:2026-07-21 1023 # => 1
# How many users were active that day?
BITCOUNT dau:2026-07-21
# Users active on BOTH days -> new key
BITOP AND dau:both dau:2026-07-21 dau:2026-07-20
BITCOUNT dau:bothFollow-up Questions
- How would you compute weekly active users from daily bitmaps?
- What are the memory implications of very sparse bit offsets?
- How does BITFIELD extend bitmaps to store small integers?
- When would a HyperLogLog be a better choice than a bitmap?
- How does BITCOUNT's optional byte range help partial aggregation?
MCQ Practice
1. Which underlying Redis type do bitmap commands operate on?
Bitmaps are not a distinct type; SETBIT/GETBIT/BITCOUNT operate on ordinary Redis Strings treated as bit arrays.
2. Which command counts the number of set bits in a bitmap?
BITCOUNT returns the population count (number of bits set to 1), optionally within a byte range.
3. What is the main memory advantage of a bitmap?
Each subject uses a single bit, so hundreds of millions of boolean flags fit in a few megabytes.
Flash Cards
Is a Redis bitmap a separate data type? — No — it is a set of bit commands (SETBIT/GETBIT/BITCOUNT/BITOP) over a normal String.
How much memory does a bitmap flag use? — One bit per item, so ~1M flags fit in about 128 KB.
Which command combines two bitmaps? — BITOP with AND, OR, XOR, or NOT, writing the result to a destination key.
Best offset choice for a bitmap? — A dense, non-negative integer range so the backing String stays compact.