Advanced Redis Bit Operations: Commands, Underlying Data Structures, Complexity, Storage Calculation, and Use Cases
This article explains Redis bitmap (bit) operations, covering command syntax for SETBIT, GETBIT, BITCOUNT, and BITOP, analyzing the underlying SDS data structure, detailing time‑complexity and storage calculations, and presenting practical application scenarios such as user sign‑in tracking and online status monitoring.
This article introduces Redis's advanced bitmap (bit) features, aiming to teach readers how to use Redis bit operations and understand their underlying implementation principles.
It covers the following topics:
Redis bit operation command examples
Underlying data structure analysis
Why the algorithms have O(1) time complexity
Storage space required for billions of bits
Suitable application scenarios for Redis bit operations
Redis Bit Operations
Redis provides four bitmap commands: SETBIT , GETBIT , BITCOUNT , and BITOP .
SETBIT
Syntax: SETBIT key offset value
The command writes a binary value (0 or 1) at the specified offset (starting from 0). Writing any value other than 0 or 1 fails.
GETBIT
Syntax: GETBIT key offset
The command retrieves the binary value stored at the given offset.
BITCOUNT
Syntax: BITCOUNT key
This command returns the number of bits set to 1 in the bitmap associated with the key.
BITOP
Syntax: BITOP operation destkey key [key...]
It performs bitwise operations (AND, OR, XOR) on one or more bitmaps and stores the result in destkey .
Underlying Data Structure Analysis
Redis stores strings using SDS (Simple Dynamic String), a binary‑safe structure defined as:
struct sdshdr {
int len; // number of used bytes (also the string length)
int free; // number of free bytes in the buffer
char buff[]; // actual byte array containing the string data
}SDS offers O(1) operations, prevents buffer overflows, reduces memory reallocations when modifying strings, provides binary‑safe APIs, and is compatible with many C string functions.
Bitmaps in Redis are stored as String objects that internally use the SDS structure.
Time Complexity
GETBIT – O(1)
SETBIT – O(1)
BITCOUNT – O(n) (where n is the number of bits, with internal optimizations)
BITOP – O(n) or O(n²) depending on the operation
For SETBIT/GETBIT, the target byte is calculated as offset / 8 and the specific bit within that byte as offset % 8 , resulting in constant‑time computation.
Storage Space Calculation
To store 1 billion bits, the required memory is approximately:
1000000000 ÷ 8 ÷ 1024 ÷ 1024 ≈ 119.21 MBThis fits comfortably within modern Redis instances that often have tens of gigabytes of memory. However, using excessively large offsets for small data sets can waste space.
Application Scenarios
Redis bitmaps are useful in many real‑world projects, such as:
User sign‑in tracking (key = date, offset = user ID)
Active user counting for daily/monthly active users and retention analysis
Online/offline status monitoring and total online user count
Global notification red‑dot indicators in mobile apps
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.