Difficulty: Advanced
How do you check the internal encoding Redis uses for a key, and why does the encoding matter?
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.
| Type | Compact encoding | Large encoding |
|---|---|---|
| String | int (integers), embstr (up to 44 bytes) | raw |
| Hash | listpack | hashtable |
| List | listpack (7.2+ for small lists) | quicklist |
| Set | intset (integers only), listpack (7.2+) | hashtable |
| Sorted set | listpack | skiplist (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.
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 64set-max-intset-entries 512 and set-max-listpack-entries 128list-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.
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.
OBJECT ENCODING alongside MEMORY USAGE <key> to see actual bytes.hashtable, look for one oversized field value.OBJECT IDLETIME (LRU policies), OBJECT FREQ (LFU policies) and OBJECT REFCOUNT.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.
OBJECT ENCODING, listpack, intset, embstr, skiplist, Memory Optimization