Strings & Bitmaps in Redis

7 questions found

What is the Redis string data type, and what kinds of values can it actually store?

Beginner
A Redis string can store text, numbers, or even binary data up to five hundred twelve megabytes in size, making it the most basic and flexible data type in Redis, commonly used for simple values like a session token, a cached page, a counter, or a serialized object.
SET greeting 'Hello, World!'
SET user_count 100
GET greeting
Real-world example A simple page view counter stores its current count as a Redis string, incrementing it every time a visitor loads the page.

Common follow-ups: What is the maximum size limit for a single Redis string value?;Can a Redis string store binary data like an image?

Data Types;Redis CLI & Basic Commands

How do you atomically increment and decrement a numeric value stored as a Redis string?

Beginner
You use INCR to atomically increase a numeric string value by one, DECR to atomically decrease it by one, or INCRBY and DECRBY to change it by a specific custom amount, all guaranteed to be safe even if many clients are trying to update the same counter simultaneously.
SET page_views 0
INCR page_views
INCRBY page_views 5
Real-world example A website safely tracks its total page view count using INCR, ensuring the counter remains accurate even when many visitors are loading pages at the exact same moment.

Common follow-ups: What happens if you try to INCR a string that does not hold a valid integer?;Is there an equivalent command for incrementing a floating point number?

Data Types;Rate Limiting with Redis

What are Redis bitmaps, and how do they let you efficiently work with individual bits within a string?

Intermediate
A Redis bitmap is not actually a separate data type but rather a way of treating a regular string as a sequence of individual bits, letting you use commands like SETBIT and GETBIT to set or read a specific bit's value, which is extremely memory efficient for tracking simple binary states, such as whether a specific user was active on a specific day.
SETBIT user_active:2026-09-07 1001 1
GETBIT user_active:2026-09-07 1001
Real-world example A user activity tracking system sets a single bit for each user who was active on a specific day, using an extremely compact bitmap representation instead of a much larger data structure to track the same information.

Common follow-ups: How much memory does tracking a million users' daily activity actually require using a bitmap?;What does the specific bit position typically represent in a use case like this?

Data Types;Redis Memory Optimization

How do you count the number of set bits within a bitmap, and how is this useful for analytics purposes?

Intermediate
You use the BITCOUNT command, which returns the total number of bits set to one within the specified range of a string, letting you efficiently calculate things like how many unique users were active on a specific day, since each active user's bit was set to one within that day's bitmap.
BITCOUNT user_active:2026-09-07
Real-world example An analytics dashboard displays the total number of active users for each day by running BITCOUNT against that day's bitmap, getting an accurate count without needing to store or iterate through an actual list of user identifiers.

Common follow-ups: How does BITCOUNT performance scale with the size of the bitmap?;Can you count bits within just a specific portion of a bitmap rather than the entire thing?

HyperLogLog in Redis;Redis Memory Optimization

How would you use bitmap operations like BITOP to combine multiple daily activity bitmaps and answer questions like how many users were active on both of two specific days?

Advanced
You use BITOP with the AND operation to combine two bitmaps into a new one where only bits set in both original bitmaps remain set, and then run BITCOUNT on that resulting combined bitmap to get the count of users active on both days, giving you powerful analytical capabilities using very simple, efficient bitwise operations.
BITOP AND result_key user_active:2026-09-06 user_active:2026-09-07
BITCOUNT result_key
Real-world example A product analytics team calculates how many users were active on both Saturday and Sunday by combining the two days' bitmaps with a bitwise AND operation, then counting the resulting set bits, all using extremely memory efficient bitmap operations.

Common follow-ups: What other bitwise operations besides AND does BITOP support?;How does this approach scale for calculating activity across many different days at once?

HyperLogLog in Redis;Data Types

What are practical real world applications where using bitmaps provides a significant memory advantage over alternative approaches like sets?

Advanced
Bitmaps excel at tracking simple binary states across a large number of sequential numeric identifiers, such as daily active users, feature flag enablement per user, or attendance tracking, where using a set to store the same information would require far more memory per tracked item, since a bitmap uses just a single bit per item rather than the overhead of a full set member entry.
-- Bitmap: 1 bit per user
SETBIT feature_enabled:new_ui 12345 1

-- Set: significantly more overhead per entry
SADD feature_enabled_users 'user12345'
Real-world example A feature flag system tracks which users have a new feature enabled using a bitmap indexed by user id, achieving dramatically lower memory usage compared to storing the same information as a set of individual user identifiers.

Common follow-ups: At what point does a bitmap's memory advantage over a set become less significant?;What are the limitations of using sequential numeric identifiers as bit positions?

Redis Memory Optimization;HyperLogLog in Redis

How do you use the SETRANGE and GETRANGE commands to work with a specific portion of a Redis string without affecting or retrieving the rest of it?

Intermediate
SETRANGE lets you overwrite a specific portion of a string starting at a given offset with new content, and GETRANGE lets you retrieve just a specific substring based on a starting and ending position, both letting you work efficiently with just a portion of a potentially large string value.
SET mystring 'Hello World'
SETRANGE mystring 6 'Redis'
GETRANGE mystring 0 4
Real-world example A logging system that stores fixed format binary data as a string uses SETRANGE to efficiently update just a specific field within that structure, without needing to retrieve, modify, and rewrite the entire value.

Common follow-ups: What happens if SETRANGE is used with an offset beyond the current length of the string?;How efficient is GETRANGE compared to retrieving the entire string and processing it in the application?

Data Types;Redis Memory Optimization