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.
The encodings
| Type | Compact encoding | Converts to | Threshold (Redis 8 defaults) |
|---|---|---|---|
| String | int, embstr | raw | Integer; ~35 bytes (key + value must fit one 64-byte allocation) |
| Hash | listpack | hashtable | 512 fields or a 64-byte value |
| List | listpack | quicklist | ~8 KB (list-max-listpack-size -2) |
| Set | intset / listpack | hashtable | 512 integers / 128 members or a 64-byte value |
| Sorted set | listpack | skiplist (+ 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
- Group small values into hashes rather than one key each, so they stay in listpack. This is the bucketing trick from Hashes.
- Prefer integers where possible:
intstrings andintsetsets are the cheapest encodings there are. - Short key names matter only at very large key counts. Don't obfuscate for a 10,000-key dataset.
- Compress big values in the client (gzip, zstd) if they're over a few KB. Redis doesn't compress values.
- Don't raise the listpack thresholds blindly. Larger listpacks
save memory but make every operation on those keys slower (O(N)
scans). Measure with
--latencyand the slowlog after changing them.
INFO memory reports used_memory (what Redis allocated) and
used_memory_rss (what the OS sees). Their ratio,
mem_fragmentation_ratio, should be around 1.0-1.5. Much higher means
fragmentation: freed memory that jemalloc can't hand back. Redis can
defragment live with activedefrag yes. Below 1.0 means the OS has
swapped some of Redis out, which is a latency emergency.
See Memory Optimization.
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