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 with options replaces older commands: SETNX, SETEX, and
GETSET still exist but SET ... NX, SET ... EX, and SET ... GET are
the modern forms.
A plain SET removes any existing TTL. Overwriting a cache entry
with SET key value (no EX) turns it into a key that never expires.
Under volatile-* eviction policies it can then never be evicted
either. Always pass EX/PX or KEEPTTL when updating keys that
should expire.
Counters
If a string holds an integer, Redis can increment it atomically:
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 60That'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
Bitmaps
A string is also an array of bits you can address individually. That makes very compact boolean sets:
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.
SETBIT key 4000000000 1 allocates a ~500 MB string, because the
bitmap must extend to that offset. Bitmaps are compact only when IDs
are dense integers starting near zero. Sparse or random IDs (UUIDs,
hashes) belong in a set or a HyperLogLog.
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 300The 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