Bitmaps
Redis bitmaps are bit arrays implemented on string values — SETBIT and GETBIT treat a string as 2^32 bits.
Introduction
Redis bitmaps are bit arrays implemented on string values — SETBIT and GETBIT treat a string as 2^32 bits. WhatsApp-scale presence and DAU tracking use ~12MB for 100M users instead of gigabytes of SET keys.
BITCOUNT and BITOP (AND/OR/XOR) run server-side analytics without exporting raw data. Bitmaps assume numeric offsets — map userId to offset via internal ID, not raw UUID hash.
Use when you need dense boolean flags or daily active user counts with minimal memory.
Understanding the topic
Key concepts
- SETBIT key offset 1 — set bit at offset; auto-expands string.
- GETBIT — read single bit; O(1).
- BITCOUNT — count set bits; optional start/end byte range.
- BITOP AND/OR/XOR/NOT — combine bitmaps for cohort analysis.
- Offset is 0-based bit index, not ASCII character.
- One key can represent millions of flags compactly.
Step-by-step explanation
- Bitmap is a string value under the hood.
- SETBIT sets bit via SDS; sparse regions may not allocate all bytes until touched.
- BITCOUNT scans bytes — can be slow on huge sparse keys.
- BITOP creates new key from operations on source keys.
- Same TTL and persistence rules as strings apply.
Syntax reference
Common commands
- Offset = your stable numeric user ID.
- BITCOUNT on 100M-bit key — run off-peak or use HyperLogLog for approximate DAU.
- BITOP — retention cohort intersection.
SETBIT dau:2026-07-01 10042 1GETBIT dau:2026-07-01 10042BITCOUNT dau:2026-07-01BITOP AND cohort:ab dau:day1 dau:day2
Informative example
Track daily feature usage per user offset in Spring:
@Servicepublic class FeatureBitmap {private final StringRedisTemplate redis;public FeatureBitmap(StringRedisTemplate redis) {this.redis = redis;}public void markUsed(long userOffset, LocalDate day) {String key = "feature:dau:" + day;redis.opsForValue().setBit(key, userOffset, true);redis.expire(key, Duration.ofDays(3));}}
Maintain userId→offset mapping in DB. Never use hashCode of UUID as offset — collisions corrupt analytics.
Real-world use
Real-world use cases
- Daily/monthly active users — one bitmap per day.
- Online presence bit per user shard (WhatsApp pattern).
- A/B test enrollment flags.
- Permission bloom-style checks at fixed offsets.
- Retention analysis via BITOP AND across days.
Best practices
- Use stable dense numeric offsets.
- Set TTL on daily bitmap keys.
- Prefer HyperLogLog when ~1% error OK for UV.
- Run BITCOUNT during low traffic windows.
- Document offset assignment in team wiki.
- Monitor key memory with MEMORY USAGE.
Common mistakes
- Using random hash as offset — collisions.
- BITCOUNT on multi-GB key in hot path.
- Forgetting TTL — bitmaps accumulate forever.
- Expecting named fields — bitmaps are positional only.
Advanced interview questions
Q1BeginnerWhat is a Redis bitmap?
Q2BeginnerMemory for 100M users as bitmap?
Q3IntermediateBITOP use case?
Q4IntermediateBitmap vs HyperLogLog?
Q5AdvancedDesign presence for 2B users?
Summary
Bitmaps = dense boolean flags at offsets.