Difficulty: Advanced
How does Redis optimize memory for small collections internally, and how do you reduce memory usage in a real deployment?
Redis stores small hashes, sets, sorted sets and lists in compact, contiguous encodings (listpack and intset) instead of full hash tables and skiplists. Once a collection crosses a configured size or element length, Redis converts it to the general-purpose structure. Compact encodings can use several times less memory for small objects, at the cost of O(N) scans inside each object, which is fine when N is small.
| Type | Compact encoding | Converts to | Threshold settings (defaults) |
|---|---|---|---|
| Hash | listpack | hashtable | hash-max-listpack-entries 128, hash-max-listpack-value 64 |
| Sorted set | listpack | skiplist + hashtable | zset-max-listpack-entries 128, zset-max-listpack-value 64 |
| Set of integers | intset | hashtable | set-max-intset-entries 512 |
| Small set of strings | listpack (7.2+) | hashtable | set-max-listpack-entries 128, set-max-listpack-value 64 |
| List | listpack, then quicklist of listpacks | quicklist | list-max-listpack-size -2 (8 KB per node) |
| String | int, embstr (up to 44 bytes), raw | n/a | n/a |
Redis 7.0 replaced the older ziplist with listpack everywhere. Listpack fixes ziplist's cascading-update problem, where growing one entry could force every following entry's header to be rewritten. Old config names like hash-max-ziplist-entries still work as aliases, which is why you will see both in blog posts.
You can check any key with OBJECT ENCODING key. Conversion is one way: once a hash becomes a hashtable it does not shrink back to a listpack if you delete fields.
A hashtable needs a bucket array, a separate allocation per entry, and pointers for each field and value. A listpack is one contiguous block of bytes with tiny length prefixes. For a user profile with 10 short fields, that removes dozens of allocations and pointer overhead. Intsets go further by storing integers in the smallest width that fits (16, 32 or 64 bits).
user:12345:lastseen strings, store HSET lastseen:123 45 <ts>, grouping 100 users per hash so each stays a listpack. This is the classic trick that can shrink memory dramatically for tiny values.-value threshold; one long value converts the whole hash.Redis uses jemalloc. After lots of deletes and updates, freed memory can be scattered, so the OS-level RSS stays high. Watch mem_fragmentation_ratio (used_memory_rss / used_memory) in INFO memory:
activedefrag yes to let Redis defragment online.MEMORY USAGE key gives the bytes used by a key, and MEMORY DOCTOR gives a human-readable diagnosis.
redis-cli --bigkeys and --memkeys find the keys worth optimising first.redis-cli HSET user:1 name Asha city Pune
redis-cli OBJECT ENCODING user:1 # listpack
redis-cli SADD ids 1 2 3
redis-cli OBJECT ENCODING ids # intset
redis-cli SADD ids hello
redis-cli OBJECT ENCODING ids # listpack (7.2+), was hashtable before
redis-cli SET counter 100
redis-cli OBJECT ENCODING counter # int
# Bytes used by a single key
redis-cli MEMORY USAGE user:1
# Fragmentation and totals
redis-cli INFO memory | grep -E 'used_memory_human|mem_fragmentation_ratio'
OBJECT ENCODING reveals which internal representation Redis picked. Exact MEMORY USAGE numbers vary by version and allocator.
// Instead of one string key per user:
// SET user:1234567:lastseen 1718000000
// group 100 users per hash so each hash stays a listpack.
function bucketKey(userId) {
const bucket = Math.floor(userId / 100);
return { key: `lastseen:${bucket}`, field: String(userId % 100) };
}
async function setLastSeen(redis, userId, ts) {
const { key, field } = bucketKey(userId);
await redis.hset(key, field, ts);
}
async function getLastSeen(redis, userId) {
const { key, field } = bucketKey(userId);
return redis.hget(key, field);
}
Each hash holds at most 100 small fields, below the 128-entry listpack threshold, which avoids per-key overhead for millions of tiny values. The trade-off is that TTLs apply per hash, not per user (unless you use hash field expiry in 7.4+).
Listpack, Intset, Skiplist, OBJECT ENCODING, Memory Fragmentation