Unlock Redis Power: Master Bitmaps, Commands, and Real-World Use Cases
This tutorial explains Redis bitmap operations, covering SETBIT, GETBIT, BITCOUNT, and BITOP commands, their syntax, underlying SDS structure, time‑complexity analysis, memory‑size calculations, and practical scenarios such as user sign‑in tracking and online‑user statistics.
Redis Bitmap Operations Overview
This article introduces Redis’s advanced bitmap feature, covering the four bitmap commands (SETBIT, GETBIT, BITCOUNT, BITOP), their syntax and usage examples, the underlying SDS data structure, time‑complexity analysis, memory‑size calculation, and typical application scenarios such as user sign‑in tracking, active‑user counting, online‑user statistics, and notification red‑dot indicators.
Environment
Operating System: macOS 64‑bit Redis version: 5.0.7 64‑bit Running mode: standalone
Bitmap Commands
SETBIT
Syntax: SETBIT key offset value – writes a binary value (0 or 1) at the specified offset (starting at 0). Non‑binary values cause the command to fail.
GETBIT
Syntax: GETBIT key offset – retrieves the bit value at the given offset.
BITCOUNT
Syntax: BITCOUNT key – returns the number of bits set to 1 in the bitmap.
BITOP
Syntax: BITOP operation destkey key [key...] – performs bitwise AND, OR, XOR, or NOT on one or more bitmaps and stores the result in destkey.
Underlying Data Structure (SDS)
Redis stores strings using Simple Dynamic String (SDS), a binary‑safe structure that provides O(1) length access, prevents buffer overflows, reduces reallocations, offers binary‑safe APIs, and is compatible with many C string functions.
Time Complexity
GETBIT – O(1)
SETBIT – O(1)
BITCOUNT – O(N)
BITOP – O(N) (or O(N²) for certain operations)
For SETBIT/GETBIT, the byte index is calculated as offset / 8 and the bit position as offset % 8, resulting in constant‑time operations.
Memory Calculation
Each bit occupies one position; 8 bits = 1 byte, 1 KB = 1024 bytes, etc. Example: 1 billion bits → 1 000 000 000 ÷ 8 ÷ 1024 ÷ 1024 ≈ 119.21 MB, which easily fits in modern Redis instances.
Typical Application Scenarios
User daily sign‑in tracking (key = date, offset = user ID)
Active‑user (DAU/MAU) counting and retention analysis
Online‑user presence and total online count
App notification red‑dot indicator for unread messages
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.
