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).
Previous
What is consistent hashing and how does it compare to Redis Cluster's approach?
Next
How does AOF rewrite work and how does it compare to RDB performance?
More Redis Questions
View all →- Advanced How does Redis Cluster sharding and the hash slot algorithm work in detail?
- Advanced What is consistent hashing and how does it compare to Redis Cluster's approach?
- Advanced How does AOF rewrite work and how does it compare to RDB performance?
- Advanced What are Redis Functions (replacing Lua scripts in Redis 7+)?
- Advanced What is Redis Stack and what does it add to vanilla Redis?