1. What is a Redis Bitmap?
A Redis Bitmap is a set of bit-oriented operations on Redis Strings (internally implemented using Simple Dynamic Strings, or SDS). Since strings are binary-safe and can contain up to 512 MB, you can work with bits (4,294,967,296 bits or ~536 million bytes).
Key characteristics.
- Each bit can be 0 or 1
- Bits are addressed by offset (position), starting from 0
- Extremely memory-efficient for boolean tracking
- Example: Track 1 million users' daily login status = ~122 KB (1,000,000 bits / 8 bits per byte)
Common use cases.
- User login/activity tracking
- Feature flags for users
- Real-time analytics
- Online/offline status
- Daily active users (DAU) calculation
2. Basic Operations: SETBIT and GETBIT
SETBIT and GETBIT2.1.SETBIT
SETBIT
2.1.1.Syntax
Syntax
It sets the bit at a given offset to 0 or 1:
Parameters:
key: The bitmap key nameoffset: Bit position (0-based index)value: 0 or 1
Returns: Previous bit value at that offset (0 or 1)
2.1.2.Example
Example
Set bit at position 0 to 1:
Set bit at position 5 to 1:
Setting bit at large offset automatically allocates memory
-
Redis allocates enough bytes to hold bit at position 1,000,000
-
Memory used: = 125,001 bytes
2.2.GETBIT
GETBIT
2.2.1.Syntax
Syntax
It returns the bit value at a given offset:
2.2.2.Example
Example
Set some bits:
Read bits:
Returns: 1
Returns: 1
Unassigned bits default to 0:
Returns: 0
Reading beyond allocated memory returns 0:
Returns: 0
2.3.DEL - Delete Bitmap Key
DEL - Delete Bitmap Key
2.3.1.Syntax
Syntax
Removes the entire bitmap key from Redis:
Parameters:
key: One or more keys to delete
Returns: Number of keys deleted
2.3.2.Example
Example
Delete a single bitmap:
Returns: 1 (key deleted)
Delete multiple bitmaps at once:
Returns: 3 (three keys deleted)
After deletion, the key no longer exists:
Returns: 0 (key doesn't exist, all bits are 0)
Returns: 0 (key doesn't exist)
3. Use Cases
3.1.User Login Status Tracking
User Login Status Tracking
3.1.1.Bitmap Approach
Bitmap Approach
Track which users logged in on a specific date. Each user ID maps to a bit offset.
Remark. This is for staistic purpose, the real login status is much more complicated because a user may log into a system using multiple tabs, the actual status involves listening to socket connection.
Date: 1st March, 2026, users of userId: 100, 250, 1000, 5000 logged in:
Check if user 100 logged in:
Returns: 1 (yes)
Check if user 500 logged in:
Returns: 0 (no)
Check if user 5000 logged in:
Returns: 1 (yes)
Python implementation:
3.1.2.Memory Efficiency Comparison to Set Approach
Memory Efficiency Comparison to Set Approach
Traditional Approach. Store user IDs in a Set
Returns: ~500 bytes (overhead per element)
Bitmap Approach. Store as bits
Returns: ~650 bytes (625 bytes for 5000 bits + overhead)
Comparison. For users with 50% login rate (500,000 active users):
Set with integer user IDs.
- Small Set (≤512 elements): ~4-8 bytes/element = ~3 KB
- Large Set (>512 elements):
dictEntryoverhead: ~24 bytes (pointers for key, value, next in hash chain, with value pointing toNULLinSETstructure)- Integer stored as SDS: ~20-30 bytes (object header + SDS + data)
Total: ~44-54 bytes/element 24 MB for 500,000 elements
Set with UUIDs (strings).
dictEntry: ~24 bytes- String object: ~60 bytes (object header + SDS + 36-char UUID)
Total: ~84 bytes/element 40 MB for 500,000 elements
Bitmap with integer user IDs: 1,000,000 bits / 8 = 125 KB
Key limitation. Bitmaps require contiguous integer user IDs as bit offsets. For UUID/email based systems, we need to assign each user an integer for bitmap-oriented statistic.
3.2.User Statistics - Daily Active Users (DAU)
User Statistics - Daily Active Users (DAU)
Track daily active users across multiple days and calculate statistics.
3.2.1.Data Setup
Data Setup
Login data of 1-5th March, 2026:
3.2.2.Calculation via BITCOUNT and BITOP (Bitvalue Operation)
Calculation via BITCOUNT and BITOP (Bitvalue Operation)
3.2.2.1.Count total active users per day
Count total active users per day
Returns: 3 (users 100, 200, 300)
Returns: 3 (users 100, 300, 400)
Returns: 3 (users 200, 300, 500)
3.2.2.2.Find users active on both March 1 AND March 2
Find users active on both March 1 AND March 2
Returns: 2 (users 100 and 300)
3.2.2.3.Find users active on March 1 OR March 2
Find users active on March 1 OR March 2
Returns: 4 (users 100, 200, 300, 400)
3.2.2.4.Find users active in March 1-5 (union)
Find users active in March 1-5 (union)
Returns: 5 (users 100, 200, 300, 400, 500)
3.2.2.5.Find users active ALL 5 days (intersection)
Find users active ALL 5 days (intersection)
Returns: 1 (only user 300 logged in all 5 days)
Python implementation for analytics:
4. BITPOS - Find First Set Bit
BITPOS - Find First Set BitReturns the position of the first bit set to 0 or 1.
4.1.Syntax
Syntax
Parameters:
key: Bitmap keybit: 0 or 1 (which bit value to find)start: Optional start offsetend: Optional end offsetBYTE|BIT: Unit for start/end (default: BYTE)
Returns: Position of first bit, or -1 if not found
4.2.Examples
Examples
4.2.1.Find first occurence
Find first occurence
Create bitmap:
Find first bit set to 1:
Returns: 2 (first 1 is at position 2)
Find first bit set to 0:
Returns: 0 (first 0 is at position 0)
Create another bitmap:
Find first 0 bit:
Returns: 3 (positions 0,1,2 are 1, position 3 is 0)
Find first 1 starting from byte 0:
Returns: 0
4.2.2.Range search
Range search
Set bits at various positions:
Find first 1 in byte , where (bits 0-7):
Returns: 0 (first 1 in byte 0)
Find first 1 in byte , where (bits 8-15):
Returns: 8 (first 1 in byte 1)
Find first 1 in byte , where (bits 16-31):
Returns: 16 (first 1 in byte 2, 3)
4.2.3.Find first available slot
Find first available slot
5. BITFIELD - Advanced Bitmap Operations
BITFIELD - Advanced Bitmap OperationsBITFIELD allows you to treat a bitmap as an array of integers of arbitrary bit width. You can set, get, and increment integer values at specific bit offsets.
5.1.Syntax
Syntax
5.2.Data Types: u\<N\> and i\<N\>
Data Types: u\<N\> and i\<N\>
BITFIELD works with integer types encoded in N bits:
-
u<N>: Unsigned integer with N bitsu1: 0 to 1 (1 bit)u8: 0 to 255 (8 bits)u16: 0 to 65535 (16 bits)u32: 0 to 4,294,967,295 (32 bits)- Maximum:
u63(unsigned 63-bit)
-
i<N>: Signed integer with N bits (two's complement)i1: -1 to 0 (1 bit)i8: -128 to 127 (8 bits)i16: -32768 to 32767 (16 bits)i32: -2,147,483,648 to 2,147,483,647 (32 bits)- Maximum:
i64(signed 64-bit)
N represents the number of bits used to encode the integer. For example:
u8uses 8 bits and can store values 0-255i8uses 8 bits and can store values -128 to 127u3uses 3 bits and can store values 0-7
5.3.Subcommand: SET
Subcommand: SET
Sets an integer value at a specific bit offset.
5.3.1.Syntax
Syntax
Returns: Previous value at that offset
5.3.2.Example
Example
Using different bit widths:
5.4.Subcommand: GET
Subcommand: GET
Retrieves an integer value from a specific bit offset.
5.4.1.Syntax
Syntax
Returns: Value at that offset
5.4.2.Example
Example
5.5.Subcommand: INCRBY
Subcommand: INCRBY
Increments an integer value at a specific bit offset.
5.5.1.Syntax
Syntax
Returns: New value after increment
5.5.2.Example
Example
Overflow behavior with unsigned integers:
5.6.OVERFLOW Handling: WRAP, SAT, FAIL
OVERFLOW Handling: WRAP, SAT, FAIL
Controls what happens when an operation would cause overflow or underflow.
5.6.1.Syntax
Syntax
Modes:
-
WRAP: Wrap around on overflow (default behavior)- Unsigned: 255 + 1 = 0, 0 - 1 = 255
- Signed: 127 + 1 = -128, -128 - 1 = 127
-
SAT: Saturate at min/max value- Unsigned: 255 + 1 = 255, 0 - 1 = 0
- Signed: 127 + 1 = 127, -128 - 1 = -128
-
FAIL: Return nil and don't modify value- Returns null when overflow would occur
5.6.2.Examples
Examples
Signed integer overflow:
Multiple operations with different overflow modes:
5.7.Interesting Use Case: 31-Day User Activity Statistics
Interesting Use Case: 31-Day User Activity Statistics
Track whether a user was active each day of the month using a single 31-bit bitmap per user. Each bit represents one day.
5.7.1.Data Setup
Data Setup
5.7.2.Check activity (total amount in a month)
Check activity (total amount in a month)
5.7.3.Check daily activity using BITFIELD for efficient storage and retrieval
Check daily activity using BITFIELD for efficient storage and retrieval
Activities in a month are condensed into 31 bits allocated to the key
activity:march:1000
we retrieve it by:
which results in 1431633920, a simple javascript (or counterpart in any language) yields
6. Summary
Redis Bitmaps provide memory-efficient boolean tracking through bit-level operations:
Core commands:
SETBIT: Set individual bitsGETBIT: Read individual bitsBITCOUNT: Count set bitsBITPOS: Find first 0 or 1 bitBITOP: Perform AND, OR, XOR, NOT operationsBITFIELD: Work with multi-bit integers
Key advantages:
- Extreme memory efficiency (1 bit per boolean)
- Fast bitwise operations
- Track millions of users with minimal memory
- Atomic operations for concurrent access
Common patterns:
- User activity tracking (daily, weekly, monthly)
- Feature flags and permissions
- Real-time analytics (DAU, MAU, retention)
- Resource allocation (parking slots, seats)
- A/B testing cohorts
BITFIELD use cases:
- Compact integer arrays
- Activity counters with overflow protection
- Multi-day statistics in single key
- Space-efficient time series data





