🔴 Redis Advanced

What is memory optimization in Redis (ziplist, listpack, and encoding thresholds)?

Answer

Redis uses compact internal encodings for small data structures to minimize memory overhead. The listpack (replacing the older ziplist in Redis 7+) is a contiguous memory block storing elements back-to-back with length prefixes. Lists, Hashes, Sets, and Sorted Sets use listpack when they are small (controlled by configuration thresholds like hash-max-listpack-entries 128 and hash-max-listpack-value 64). Once a structure exceeds the threshold, it is converted to a more memory-hungry but faster-access structure (hashtable, skiplist). The skiplist powers Sorted Sets at larger sizes — it provides O(log N) operations with a linked-list structure that allows range queries, unlike a hashtable. Memory fragmentation is another concern: the mem_fragmentation_ratio in INFO memory above 1.5 indicates Redis is using much more RAM than the dataset requires, often resolved by restarting Redis or using MEMORY PURGE to return freed memory to the OS (Redis 4+ with jemalloc).