Databases 21 min read

Redis Bitmaps Tutorial: Concepts, Commands, and Real‑World Applications

This tutorial explains Redis Bitmaps, covering their underlying concept, core commands such as SETBIT, GETBIT, BITCOUNT, BITOP, BITPOS, BITFIELD, and demonstrates practical uses like online‑status tracking, daily active‑user counting, Bloom filters, marketing segmentation, collaborative drawing, and handling non‑sequential IDs.

政采云技术
政采云技术
政采云技术
Redis Bitmaps Tutorial: Concepts, Commands, and Real‑World Applications

Preface

This article is a Redis Bitmaps tutorial that introduces the concept, operations, and common applications of Bitmaps. Redis version: 6.2.6.

1. Simple Introduction to Bitmaps

Bitmaps are not a separate data type; they are a set of bit‑oriented operations defined on the String type. Since strings are binary‑safe blobs up to 512 MB, they can hold up to 2³² bits.

Example: store the character "a" in a key and inspect its binary representation (0110 0001). Using GETBIT key offset we can read individual bits, and SETBIT key offset value can modify a specific bit, turning the stored character into "c" when bit 6 is set to 1.

Bitmaps allow a string to be treated as a binary array, enabling use cases such as recording the online status of billions of users with a single bit per user, saving massive space compared to traditional MySQL tables.

127.0.0.1:6379> set k1 a
OK
127.0.0.1:6379> get k1
"a"
127.0.0.1:6379> getbit k1 0
(integer) 0
127.0.0.1:6379> getbit k1 1
(integer) 1
... (other bits) ...
127.0.0.1:6379> setbit k1 6 1   // set bit 6 to 1
(integer) 0
127.0.0.1:6379> get k1
"c"

2. Bitmap Operations

SETBIT

Time complexity: O(1). Updates the bit at the specified offset (0‑based) to 0 or 1 and returns the previous value.

SETBIT key offset value

GETBIT

Time complexity: O(1). Returns the bit value at the given offset; if the key does not exist or the offset is out of range, 0 is returned.

GETBIT key offset

BITCOUNT

Time complexity: O(n). Counts the number of bits set to 1 in a string (optionally within a range).

BITCOUNT key [start end [BYTE|BIT]]

Example:

127.0.0.1:6379> set k1 a
OK
127.0.0.1:6379> BITCOUNT k1
(integer) 3

BITOP

Time complexity: O(n). Performs bitwise operations (AND, OR, XOR, NOT) on multiple keys and stores the result in a destination key.

BITOP operation destkey key [key ...]

BITPOS

Time complexity: O(N). Returns the position of the first bit set to 0 or 1.

BITPOS key bit [start [end [BYTE|BIT]]]

BITFIELD

Time complexity: O(n). Treats a Redis string as an array of arbitrary‑width integers, allowing GET, SET, INCRBY operations with optional overflow handling (WRAP, SAT, FAIL).

BITFIELD key [GET encoding offset] [SET encoding offset value] [INCRBY encoding offset increment] [OVERFLOW WRAP|SAT|FAIL]

Examples of GET, SET, INCRBY and overflow control are provided in the original text.

BITFIELD_RO

Read‑only variant of BITFIELD; only accepts GET sub‑commands and can be safely used on replica nodes.

Bitmap Applications

Key characteristics of Bitmaps: only two states (0/1), minimal memory usage, and O(1) set/get speed. These make them suitable for:

Tracking user online status.

Daily active‑user (DAU) statistics using BITCOUNT and BITOP.

Implementing Bloom filters (hash + bitmap).

Marketing segmentation: storing user attributes (gender, profession, spend level) as separate Bitmaps and combining them with BITOP AND for precise targeting.

Recording user actions such as ad clicks, page views, etc., by setting bits per user ID.

Collaborative drawing (e.g., Reddit r/place) where each pixel’s color is stored in a bitmap using BITFIELD with 4‑bit fields.

Handling non‑sequential IDs (e.g., Snowflake, UUID) by mapping IDs to a bounded range, similar to Bloom‑filter techniques.

Persisting bitmaps efficiently with compression algorithms like Run‑Length Encoding (RLE).

References

https://redis.io/

https://redditblog.com/2017/04/13/how-we-built-rplace/

Recruitment

The article concludes with a recruitment notice for the Zero technology team in Hangzhou, describing the team’s size, technical focus areas, and invitation to apply via email.

PerformanceDatabaseRedisBitfieldBitmap OperationsBitmaps
政采云技术
Written by

政采云技术

ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.