Redis Tutorial 0/42 lessons ~6 min read Lesson 13

    Bitmaps

    Redis bitmaps are bit arrays implemented on string values — SETBIT and GETBIT treat a string as 2^32 bits.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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

    1. Bitmap is a string value under the hood.
    2. SETBIT sets bit via SDS; sparse regions may not allocate all bytes until touched.
    3. BITCOUNT scans bytes — can be slow on huge sparse keys.
    4. BITOP creates new key from operations on source keys.
    5. 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.
    bash
    SETBIT dau:2026-07-01 10042 1
    GETBIT dau:2026-07-01 10042
    BITCOUNT dau:2026-07-01
    BITOP AND cohort:ab dau:day1 dau:day2

    Informative example

    Track daily feature usage per user offset in Spring:

    java
    @Service
    public 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?
    String treated as bit array; SETBIT/GETBIT at offsets.
    Q2BeginnerMemory for 100M users as bitmap?
    Roughly 12MB (100M bits) vs gigabytes of per-user keys.
    Q3IntermediateBITOP use case?
    Combine daily DAU bitmaps for cohort AND/OR analysis.
    Q4IntermediateBitmap vs HyperLogLog?
    Bitmap exact per-user flags; HLL approximate cardinality in fixed space.
    Q5AdvancedDesign presence for 2B users?
    Shard bitmaps by userId range; Pub/Sub for change notify; no persistence on ephemeral presence.

    Summary

    Bitmaps = dense boolean flags at offsets.

    Ready to mark this lesson complete?Track your journey across the entire course.