Redis Bitmaps & Bitfields
A bitmap is not a separate type: it is a normal string that you treat as an array of bits. Bit commands let you set and read individual bits by position and count them very quickly. Because one bit per item is incredibly compact, bitmaps are a classic tool for "did user N do X today?" questions: 10 million users fit in about 1.2 MB.
SETBIT and GETBIT
SETBIT key offset value sets the bit at a position to 0 or 1 and returns the previous bit. GETBIT reads a bit; bits that were never set read as 0. The string grows automatically to hold the highest offset.
SETBIT logins:2026-10-17 5 1
SETBIT logins:2026-10-17 12 1
SETBIT logins:2026-10-17 5 1
GETBIT logins:2026-10-17 5
GETBIT logins:2026-10-17 6
GETBIT logins:2026-10-17 9999
STRLEN logins:2026-10-17
TYPE logins:2026-10-17redis> SETBIT logins:2026-10-17 5 1
(integer) 0
redis> SETBIT logins:2026-10-17 12 1
(integer) 0
redis> SETBIT logins:2026-10-17 5 1
(integer) 1
redis> GETBIT logins:2026-10-17 5
(integer) 1
redis> GETBIT logins:2026-10-17 6
(integer) 0
redis> GETBIT logins:2026-10-17 9999
(integer) 0
redis> STRLEN logins:2026-10-17
(integer) 2
redis> TYPE logins:2026-10-17
string
Offset 12 lives in the second byte, so the string is 2 bytes long. In this pattern the offset is the user ID: user 5 and user 12 logged in on 17 October.
Note: Setting a bit at a very large offset allocates the whole string up to it.
SETBIT key 4000000000 1creates a 500 MB string. Use bitmaps when IDs are dense integers starting near zero.
Counting bits
BITCOUNT counts the bits set to 1. With a range it counts only part of the string (by BYTE by default, or by BIT):
SETBIT active 0 1
SETBIT active 3 1
SETBIT active 4 1
SETBIT active 10 1
BITCOUNT active
BITCOUNT active 0 0
BITCOUNT active 0 4 BITredis> SETBIT active 0 1
(integer) 0
redis> SETBIT active 3 1
(integer) 0
redis> SETBIT active 4 1
(integer) 0
redis> SETBIT active 10 1
(integer) 0
redis> BITCOUNT active
(integer) 4
redis> BITCOUNT active 0 0
(integer) 3
redis> BITCOUNT active 0 4 BIT
(integer) 3
Bits 0, 3 and 4 are in the first byte, so BITCOUNT active 0 0 (byte 0 only) is 3. With BIT, the range 0 4 means bit positions 0 to 4.
Combining bitmaps with BITOP
BITOP combines bitmaps bit by bit and stores the result. It answers questions like "who logged in on both days" (AND) or "on either day" (OR):
SETBIT day:1 1 1
SETBIT day:1 2 1
SETBIT day:1 7 1
SETBIT day:2 2 1
SETBIT day:2 7 1
SETBIT day:2 9 1
BITOP AND both day:1 day:2
BITCOUNT both
BITOP OR any day:1 day:2
BITCOUNT any
BITOP XOR changed day:1 day:2
BITCOUNT changedredis> SETBIT day:1 1 1
(integer) 0
redis> SETBIT day:1 2 1
(integer) 0
redis> SETBIT day:1 7 1
(integer) 0
redis> SETBIT day:2 2 1
(integer) 0
redis> SETBIT day:2 7 1
(integer) 0
redis> SETBIT day:2 9 1
(integer) 0
redis> BITOP AND both day:1 day:2
(integer) 2
redis> BITCOUNT both
(integer) 2
redis> BITOP OR any day:1 day:2
(integer) 2
redis> BITCOUNT any
(integer) 4
redis> BITOP XOR changed day:1 day:2
(integer) 2
redis> BITCOUNT changed
(integer) 2
BITOP returns the length of the result string in bytes. Users 2 and 7 logged in on both days; users 1, 2, 7 and 9 logged in at least once; 1 and 9 on exactly one of the two days. BITOP NOT dest src flips every bit of one bitmap.
Finding bits with BITPOS
BITPOS key bit returns the position of the first bit set to 0 or 1. It is handy for finding a free slot, for example the first free seat in a row:
SETBIT seats 0 1
SETBIT seats 1 1
SETBIT seats 2 1
SETBIT seats 4 1
BITPOS seats 0
BITPOS seats 1
SETBIT seats 3 1
BITPOS seats 0redis> SETBIT seats 0 1
(integer) 0
redis> SETBIT seats 1 1
(integer) 0
redis> SETBIT seats 2 1
(integer) 0
redis> SETBIT seats 4 1
(integer) 0
redis> BITPOS seats 0
(integer) 3
redis> BITPOS seats 1
(integer) 0
redis> SETBIT seats 3 1
(integer) 0
redis> BITPOS seats 0
(integer) 5
Bitfields: many small integers in one string
BITFIELD reads and writes integers of any bit width (u8 = unsigned 8-bit, i16 = signed 16-bit, ...) at any offset. A #n offset means "the n-th field of this width". Several operations can run in one atomic call:
BITFIELD player:7 SET u8 #0 100 SET u8 #1 30
BITFIELD player:7 GET u8 #0 GET u8 #1
BITFIELD player:7 INCRBY u8 #1 50
BITFIELD player:7 INCRBY u8 #1 200
BITFIELD player:7 OVERFLOW SAT INCRBY u8 #1 250
BITFIELD player:7 OVERFLOW FAIL INCRBY u8 #0 200redis> BITFIELD player:7 SET u8 #0 100 SET u8 #1 30
1) (integer) 0
2) (integer) 0
redis> BITFIELD player:7 GET u8 #0 GET u8 #1
1) (integer) 100
2) (integer) 30
redis> BITFIELD player:7 INCRBY u8 #1 50
1) (integer) 80
redis> BITFIELD player:7 INCRBY u8 #1 200
1) (integer) 24
redis> BITFIELD player:7 OVERFLOW SAT INCRBY u8 #1 250
1) (integer) 255
redis> BITFIELD player:7 OVERFLOW FAIL INCRBY u8 #0 200
1) (nil)
Here a player has two 8-bit stats, health (#0) and mana (#1). The overflow behaviour is configurable:
OVERFLOW |
On overflow |
|---|---|
WRAP (default) |
wraps around like C integers: 80 + 200 = 280 → 24 for u8 |
SAT |
saturates at the minimum or maximum (255 for u8) |
FAIL |
does nothing and returns (nil) |
When to use bitmaps
| Good fit | Poor fit |
|---|---|
| Daily active users by numeric ID | Sparse or huge IDs (use a set or HyperLogLog) |
| Feature flags per user | IDs that are UUIDs or email addresses |
| Seat or slot maps | When you need the list of members often |
| Retention: AND of two days |
Test yourself
What does SETBIT k 3 1 return the first time it runs on an empty key?
- (integer) 1
- (integer) 0
- OK
- (integer) 3
Which command counts the users who were active on both day:1 and day:2?
- BITCOUNT day:1 day:2
- BITOP AND both day:1 day:2 then BITCOUNT both
- BITOP OR any day:1 day:2 then BITCOUNT any
- BITPOS day:1 1
Why can SETBIT flags 4000000000 1 be a problem?
- Offsets must be under 1000
- It allocates a string large enough to hold that offset (about 500 MB)
- It deletes the key
- It returns an error