Skip to main content

What is Bitmap?

Bitmap in Redis is a way to store and manipulate sequences of bits (0s and 1s) on top of an ordinary string (String). It's not really a separate data type, it's a set of bit operations on a Redis String that let you use it as a bitmap.

The core idea

A Redis String can be thought of as an array of bits up to 512 MB long → that's up to 4 billion bits (4×10⁹ boolean values). Every bit can be set (1) or cleared (0), then read or counted.

Example

bash
SETBIT userlogins 5 1 GETBIT userlogins 5

Result:

javascript
1

Bit 5 is set to 1.

Main commands

CommandDescription
SETBIT key offset valueset a bit's value (0 or 1)
GETBIT key offsetget a bit's value
BITCOUNT key [start end]count how many bits are set to 1
BITOP operation destkey key [key ...]bitwise operations (AND, OR, XOR, NOT)
BITPOS key bit [start end]find the first bit with a given value

A counting example

bash
SETBIT active_users 1 1 SETBIT active_users 5 1 SETBIT active_users 7 1 BITCOUNT active_users

Result:

javascript
3

Three bits are set to 1.

Bitwise operations

bash
BITOP AND result key1 key2 BITOP OR result key1 key2 BITOP XOR result key1 key2 BITOP NOT result key

These let you intersect or union several bitmaps.

Internal structure

A Bitmap is an ordinary Redis String, where every byte is made of 8 bits. Redis doesn't preallocate memory: if you set a bit at a large offset, everything before it is filled with zeros.

Uses

  • Tracking daily user activity (1, active, 0, not).
  • Counting unique visits.
  • Storing boolean flags (did/didn't perform an action).
  • Simple analytics and statistics systems.
  • Compressing binary data or feature flags.

An activity-tracking example

bash
SETBIT logins:2025-10-21 12345 1 GETBIT logins:2025-10-21 12345 BITCOUNT logins:2025-10-21

User ID 12345 is marked active for the day, and the total number of active users can be read instantly.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.