Master Redis Bitmaps: SETBIT, GETBIT, BITCOUNT, BITOP Explained
This article introduces Redis's advanced bitmap capabilities, detailing the SETBIT, GETBIT, BITCOUNT, and BITOP commands, their syntax, underlying SDS data structure, performance characteristics, storage calculations, and practical use cases such as user sign‑in tracking and online status monitoring.
This article shares Redis's advanced feature: bit operations (bitmap).
Operating System: macOS 64‑bit Redis version: 5.0.7 64‑bit Mode: standalone
Redis Bit Operations
Redis bit operations, also called bitmap, provide four commands: SETBIT, GETBIT, BITCOUNT, and BITOP.
SETBIT
Syntax: SETBIT key offset value (value must be 0 or 1). The command writes a binary value at the specified offset in the bitmap.
GETBIT
Syntax: GETBIT key offset. It retrieves the binary value stored at the given offset.
BITCOUNT
Syntax: BITCOUNT key. It returns the number of bits set to 1 in the bitmap.
BITOP
Syntax: BITOP operation destkey key [key…]. It performs AND, OR, XOR on multiple bitmaps and stores the result in destkey.
Underlying Data Structure Analysis
Redis stores strings using SDS (Simple Dynamic String), a binary‑safe structure.
struct sdshdr {
int len; // used bytes (string length)
int free; // unused bytes
char buff[]; // actual data
}The bitmap is stored as a Redis string, which internally uses SDS.
Time Complexity
GETBIT and SETBIT: O(1)
BITCOUNT: O(n)
BITOP: O(n) or O(n²) depending on operation.
Explanation: SETBIT calculates the target byte as offset / 8 and the bit position as offset % 8, both constant‑time operations.
Storage Space Calculation
Each bit occupies 1/8 byte. For 1 billion bits, memory needed is:
1 000 000 000 ÷ 8 ÷ 1024 ÷ 1024 ≈ 119.21 MBThus 1 billion entries require roughly 119 MB, well within modern Redis clusters.
Be careful not to use excessively large offsets for small data sets, as this wastes memory.
Use Cases
User Sign‑In Tracking
Store each day as a key; user ID maps to an offset. Setting the bit to 1 records a sign‑in.
Active User Counting
Daily keys record whether a user was active (bit = 1). Same approach works for monthly active users.
Online Status Monitoring
Map user IDs to bits; 1 indicates online, 0 offline. Allows quick online/offline queries and total online count.
App Notification Red Dots
Use a bitmap to flag users with unread messages, enabling efficient global notification checks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
