Object Encoding

Difficulty: Advanced

Question

How do you check the internal encoding Redis uses for a key, and why does the encoding matter?

Answer

Run OBJECT ENCODING <key>. It returns the internal representation Redis chose for that value, for example int, embstr or raw for strings, listpack for a small hash, and hashtable once the hash grows past a threshold. The encoding matters because compact encodings can use several times less memory than the general-purpose ones, which directly affects how many keys fit in your RAM budget.

The encodings you will see in Redis 7 and later

TypeCompact encodingLarge encoding
Stringint (integers), embstr (up to 44 bytes)raw
Hashlistpackhashtable
Listlistpack (7.2+ for small lists)quicklist
Setintset (integers only), listpack (7.2+)hashtable
Sorted setlistpackskiplist (skiplist plus hash table)

Older guides and interview answers say ziplist. Redis 7.0 replaced ziplist with listpack, a similar flat byte array that fixed ziplist's cascading-update problem. If you say "ziplist" in 2026, add "now listpack" and you will sound current.

How Redis decides when to convert

A key starts in the compact encoding and is converted when it crosses a size or element-length threshold set in redis.conf:

  • hash-max-listpack-entries 128 and hash-max-listpack-value 64 (bytes)
  • zset-max-listpack-entries 128 and zset-max-listpack-value 64
  • set-max-intset-entries 512 and set-max-listpack-entries 128
  • list-max-listpack-size -2 (each quicklist node up to 8 KB)

So a hash with 100 small fields stays a listpack, but adding a single field whose value is 65 bytes converts the whole hash to a hashtable. Do not count on keys converting back to compact form when they shrink.

Why compact encodings save memory and what they cost

A listpack is one contiguous block: no per-element pointers, no hash buckets, no separate allocations. Lookups inside it are linear scans, which is fast for 128 small entries because the data sits in CPU cache. A hashtable gives O(1) lookups but stores a pointer-heavy entry per field. The trade-off is that raising the thresholds too high (say 10,000 entries) makes every HGET an O(N) scan, so CPU goes up while memory goes down.

Using it in practice

  • Check OBJECT ENCODING alongside MEMORY USAGE <key> to see actual bytes.
  • When designing a cache for something like per-user feature flags at a large consumer app, keep each hash under the listpack thresholds so millions of users fit comfortably.
  • If a key unexpectedly shows hashtable, look for one oversized field value.
  • Related introspection commands: OBJECT IDLETIME (LRU policies), OBJECT FREQ (LFU policies) and OBJECT REFCOUNT.
How a hash changes encoding as it grows. Redis starts compact and switches to a hash table once a threshold is crossed.
  1. HSET creates the hash: It starts as a listpack.
  2. Fields are added: Stays listpack while under 128 fields of 64 bytes or less.
  3. A threshold is crossed: Field 129 or a value over 64 bytes triggers conversion.
  4. Converted to hashtable: O(1) lookups but noticeably more memory per field.

Code examples

Watching encodings change

redis-cli SET counter 42
redis-cli OBJECT ENCODING counter          # int

redis-cli SET city "Bengaluru"
redis-cli OBJECT ENCODING city             # embstr (44 bytes or less)

redis-cli HSET user:1 name Asha plan pro
redis-cli OBJECT ENCODING user:1           # listpack
redis-cli MEMORY USAGE user:1

# One long value (over 64 bytes) converts the whole hash
redis-cli HSET user:1 bio "$(printf 'x%.0s' {1..80})"
redis-cli OBJECT ENCODING user:1           # hashtable
redis-cli MEMORY USAGE user:1

redis-cli SADD ids 1 2 3
redis-cli OBJECT ENCODING ids              # intset

The same hash jumps from listpack to hashtable because of one 80-byte field, and MEMORY USAGE shows the memory cost of that switch. Exact byte counts vary by version and allocator.

Key points

  • OBJECT ENCODING <key> shows the internal representation of one key.
  • Redis 7.0 replaced ziplist with listpack; old config names like hash-max-ziplist-entries are kept as aliases.
  • Strings: int, embstr (44 bytes or less) or raw.
  • Thresholds like hash-max-listpack-entries 128 and value 64 bytes trigger conversion.
  • Compact encodings save memory at the cost of O(N) scans inside the key.

Common follow-up questions

  • Why did Redis replace ziplist with listpack?
  • What happens if you set hash-max-listpack-entries to 100000?
  • How does a sorted set use both a skiplist and a hash table?
  • How would you estimate memory for 10 million user hashes?

Interview tips

  • Mention listpack replacing ziplist in Redis 7; it shows you are up to date.
  • Connect encoding to cost: compact encodings mean more keys per GB and a cheaper cluster.
  • Explain the trade-off (memory vs CPU) instead of claiming compact is always better.

Concepts covered

OBJECT ENCODING, listpack, intset, embstr, skiplist, Memory Optimization

More Redis & Caching questions