Redis Bitmaps
A Bitmap stores a long row of single bits, where each bit holds either 0 or 1. Redis builds Bitmaps on top of the String type, so no new data type exists to learn. Bitmaps track yes-or-no facts for millions of items while using very little memory.
The Light Switch Analogy
Imagine a wall with thousands of light switches in a straight line. Each switch has a position number and sits either off (0) or on (1). Redis lets you flip any switch by number, check any switch, and count how many switches are on.
Key: "login:2026-06-10"
Position: 0 1 2 3 4 5 6 7 8 9 ...
Bit: 0 1 1 0 0 1 0 0 0 1 ...
▲ ▲ ▲ ▲
Users 1, 2, 5 and 9 logged in that day
SETBIT and GETBIT
SETBIT turns a bit on or off at a given position. GETBIT reads it.
127.0.0.1:6379> SETBIT login:2026-06-10 1 1 (integer) 0 127.0.0.1:6379> SETBIT login:2026-06-10 2 1 (integer) 0 127.0.0.1:6379> GETBIT login:2026-06-10 2 (integer) 1 127.0.0.1:6379> GETBIT login:2026-06-10 3 (integer) 0
SETBIT returns the old value of the bit. Positions never touched read as 0.
BITCOUNT – Counting the Ones
127.0.0.1:6379> BITCOUNT login:2026-06-10 (integer) 2
The result tells you how many users logged in on that day. Redis counts the bits without scanning your application data.
BITOP – Combining Bitmaps
BITOP runs AND, OR, XOR, or NOT across several Bitmaps and saves the result under a new key.
Monday: 1 1 0 1 0
Tuesday: 1 0 0 1 1
─────────
AND: 1 0 0 1 0 → users active on both days
OR: 1 1 0 1 1 → users active on at least one day
127.0.0.1:6379> BITOP AND both:days login:mon login:tue (integer) 1 127.0.0.1:6379> BITCOUNT both:days (integer) 2
BITPOS – Finding the First Bit
127.0.0.1:6379> BITPOS login:2026-06-10 1 (integer) 1
BITPOS returns the position of the first bit that matches the value you request. Here it finds the first user who logged in.
How Little Memory Bitmaps Use
Users tracked Memory needed ───────────────── ────────────── 1,000 about 125 bytes 1 million about 125 KB 100 million about 12.5 MB
Eight bits fit in one byte, so one million users need only 125,000 bytes. Redis grows the String as you set higher positions.
Practical Example – Daily Active Users
Each user has a numeric ID. The app uses that ID as the bit position. User 1042 logs in on June 10: SETBIT dau:2026-06-10 1042 1 Dashboard asks, "How many users came today?" BITCOUNT dau:2026-06-10 Dashboard asks, "Who came all three days?" BITOP AND streak dau:06-08 dau:06-09 dau:06-10 BITCOUNT streak
This design answers analytics questions in milliseconds and needs no database query.
Limits to Remember
- Bit positions must be whole numbers, so every item needs a numeric ID.
- A very high position on an empty key allocates a large String at once. Setting position 4 billion creates a 512 MB value.
- Bitmaps answer yes-or-no questions only. Use Hashes or Sorted Sets for richer data.
Key Points
- A Bitmap is a String treated as a row of bits.
- SETBIT and GETBIT write and read one bit.
- BITCOUNT counts the bits set to 1.
- BITOP combines Bitmaps with AND, OR, XOR, and NOT.
- One million flags need about 125 KB of memory.
