Learn Labs
Data Structures

Strings

Values, counters, SET options, bitmaps, and bitfields

A Redis string is a binary-safe byte sequence up to 512 MB: text, a number, serialised JSON, a protobuf, an image. It's the simplest type and the one you'll use most, as cache entries, counters, flags, and locks.

SET and GET

SET app:greeting "hello"create or overwriteGET app:greeting"hello"SET cache:page:/home "<html>..." EX 60with a TTL of 60 seconds (PX for ms)SET lock:report token123 NX PX 5000only if Not eXists; the basis of locksSET app:config:mode "v2" XXonly if it already eXistsSET app:counter 10 GETset and return the old valueSET app:k v KEEPTTLoverwrite but keep the existing TTLMSET a 1 b 2 c 3 / MGET a b cmany keys in one round tripGETEX cache:x EX 300read and refresh the TTLGETDEL app:tokenread and delete atomically

SET with options replaces older commands: SETNX, SETEX, and GETSET still exist but SET ... NX, SET ... EX, and SET ... GET are the modern forms.

Counters

If a string holds an integer, Redis can increment it atomically:

INCR app:stats:page_views+1, returns the new valueINCRBY app:stats:page_views 10+10DECR / DECRBYsubtractINCRBYFLOAT app:balance 2.50floating point

Atomic increment is one of Redis's most useful features. A hundred app servers can INCR the same key concurrently and never lose an update. In SQL that needs UPDATE ... SET n = n + 1 and row locks.

# a per-minute counter that cleans itself up
INCR rate:fixed:user-42:29863051
EXPIRE rate:fixed:user-42:29863051 60

That's the core of a fixed-window rate limiter.

Encodings

SET n 123                 -> OBJECT ENCODING n      "int"      (8 bytes, no string at all)
SET s hello               -> OBJECT ENCODING s      "embstr"   (short: stored in one allocation with the key)
SET l <60 bytes>          -> OBJECT ENCODING l      "raw"      (separate allocation)

The embstr cutoff depends on the key length too: in Redis 8 the key name is embedded in the same allocation, so a value of about 35 bytes or less usually qualifies (older versions used a fixed 44 bytes). You don't need to manage this, but it explains why MEMORY USAGE of SET k hello is 32 bytes, not 6: each key carries a dictionary entry, an object header, and allocator rounding.

Partial strings

APPEND app:log "line\n"append, returns new lengthSTRLEN app:logGETRANGE app:log 0 9substringSETRANGE app:log 0 "LINE"overwrite bytes at an offset

Bitmaps

A string is also an array of bits you can address individually. That makes very compact boolean sets:

SETBIT app:logins:user:42:2026-10 11 1user 42 logged in on day 12 (0-indexed)GETBIT app:logins:user:42:2026-10 111BITCOUNT app:logins:user:42:2026-10days logged in this monthBITPOS app:logins:user:42:2026-10 1first day with a loginBITOP AND active:both day:1 day:2users active on both days

Track daily active users by setting bit user_id in dau:2026-10-12: 10 million users is 1.25 MB per day, and BITOP AND across days gives retention cohorts in milliseconds.

Bitfields

BITFIELD packs several small integers into one string at arbitrary bit widths, with overflow control. For example, 4 counters of 16 bits each in 8 bytes:

BITFIELD app:player:42:stats SET u16 #0 100 INCRBY u16 #1 5 GET u16 #0
# u16 #0 = first unsigned 16-bit slot, #1 = second, ...

Storing objects as strings

Serialising a whole object to JSON in one string is common and fine for caches:

SET cache:product:1001 '{"name":"Mechanical keyboard","price":89}' EX 300

The trade-off: to change one field you read, deserialise, modify, and write the whole value, racing other writers. If you update fields individually, use a hash or the built-in JSON type.

Lab

SET app:visits 0
INCR app:visits
INCRBY app:visits 41
GET app:visits                       # "42"
OBJECT ENCODING app:visits           # "int"
SET cache:demo hello EX 30
SET cache:demo world                 # plain SET...
TTL cache:demo                       # -1: the TTL is gone
SETBIT app:bits 7 1
GET app:bits                         # "\x01"  — bit 7 is the lowest bit of byte 0
BITCOUNT app:logins:user:42:2026-10  # from seed.py

On this page