Learn Labs
Data Structures

Encodings & Memory Layout

listpack, intset, quicklist, skiplist, and what a key really costs

Each Redis type has more than one concrete in-memory representation, its encoding. Small values use compact encodings that are cheap in memory; Redis switches to general-purpose encodings as they grow. Knowing the thresholds is how you keep a large dataset small.

> 512 fields or > 64 B> 512 ints / non-integer> 128 members or > 64 B> ~8 KBhash: listpackhashtableset: intsetlistpack / hashtablezset: listpackskiplist + hashlist: listpackquicklistmeasured on Redis 8.8 (CONFIG GET *-max-listpack-*); conversions never reverse

The encodings

TypeCompact encodingConverts toThreshold (Redis 8 defaults)
Stringint, embstrrawInteger; ~35 bytes (key + value must fit one 64-byte allocation)
Hashlistpackhashtable512 fields or a 64-byte value
Listlistpackquicklist~8 KB (list-max-listpack-size -2)
Setintset / listpackhashtable512 integers / 128 members or a 64-byte value
Sorted setlistpackskiplist (+ hash table)128 members or a 64-byte value
CONFIG GET *-max-listpack-*
CONFIG GET set-max-intset-entries
OBJECT ENCODING <key>

listpack is a single contiguous block of length-prefixed entries. No pointers, no per-entry allocations. Lookups are linear scans, which is why it's used only up to a few hundred entries: below that, a scan over contiguous memory is as fast as hashing and much more CPU-cache-friendly.

intset is a sorted array of 16-, 32-, or 64-bit integers, upgraded to the wider width as needed. Membership is a binary search.

Conversions are one-way. A hash that once exceeded the threshold stays a hashtable even if you delete fields back down.

What a key really costs

MEMORY USAGE of a tiny string reports 24-32 bytes, not the 2 you stored. Every key carries:

  • an entry in the main dictionary (the key → value hash table)
  • the key itself, as an SDS string with a header
  • a value object (robj: type, encoding, LRU/LFU info, refcount; in Redis 8 the key is embedded in it)
  • the value's own representation
  • allocator overhead: jemalloc rounds allocations up to size classes
  • an entry in the expires dictionary, if it has a TTL
SET a 1
MEMORY USAGE a            # 24 bytes for a 1-character key and an integer value

# a million such keys
used_memory grows by tens of MB, almost entirely overhead
(the dictionary entries and allocator rounding aren't all counted by MEMORY USAGE)

Practical memory rules

  1. Group small values into hashes rather than one key each, so they stay in listpack. This is the bucketing trick from Hashes.
  2. Prefer integers where possible: int strings and intset sets are the cheapest encodings there are.
  3. Short key names matter only at very large key counts. Don't obfuscate for a 10,000-key dataset.
  4. Compress big values in the client (gzip, zstd) if they're over a few KB. Redis doesn't compress values.
  5. Don't raise the listpack thresholds blindly. Larger listpacks save memory but make every operation on those keys slower (O(N) scans). Measure with --latency and the slowlog after changing them.

Lab

# 1. strings
SET app:e1 12345        OBJECT ENCODING app:e1     # int
SET app:e2 hello        OBJECT ENCODING app:e2     # embstr
SET app:e3 <50 chars>   OBJECT ENCODING app:e3     # raw

# 2. a set that crosses the intset limit (run from the shell)
for i in $(seq 1 513); do echo "SADD app:ints $i"; done | \
docker exec -i redis redis-cli --user app --pass app123 > /dev/null
docker exec redis redis-cli --user app --pass app123 OBJECT ENCODING app:ints   # hashtable

# 3. compare memory: 1000 keys vs one hash with 1000 fields
INFO memory   # note used_memory, then create each, then compare

On this page