Unlocking Redis: Deep Dive into Its Memory Model and Optimization Techniques
This article explains Redis’s memory model—including memory statistics, allocation, object structures, internal encodings, and practical optimization strategies—providing detailed insights into how Redis stores data, manages memory fragmentation, and how developers can estimate and reduce memory usage for high‑performance deployments.
Redis Memory Statistics
After connecting with redis-cli, the INFO MEMORY command shows memory‑related fields such as used_memory , used_memory_rss , mem_fragmentation_ratio and mem_allocator . used_memory is the total memory allocated by Redis (including virtual memory), while used_memory_rss is the resident set size reported by the OS. Their ratio defines the memory fragmentation rate.
Redis Memory Partition
Redis memory consumption can be divided into four parts:
Data stored in used_memory (key‑value pairs).
Memory used by the Redis process itself (code, constants, etc.), which is small and not counted by used_memory.
Buffer memory (client buffers, replication backlog, AOF buffer) allocated by jemalloc and counted in used_memory.
Memory fragmentation, which is not counted in used_memory but influences mem_fragmentation_ratio.
Redis Data Storage Details
jemalloc
Redis uses jemalloc as the default allocator because it reduces fragmentation. jemalloc divides memory into small, large and huge classes and allocates the smallest fitting block (e.g., a 130‑byte object is placed in a 160‑byte slot).
redisObject
All Redis values are wrapped in a redisObject structure containing fields such as type, encoding, lru, refcount and ptr. The type identifies one of the five data types; encoding selects the internal representation; lru tracks idle time for eviction; refcount enables reference counting and shared objects.
SDS (Simple Dynamic String)
Redis stores strings in SDS structures, which keep the buffer pointer, length and free space. SDS provides O(1) length retrieval, automatic reallocation to avoid buffer overflows, and can store binary data because the length is explicit.
Object Types and Internal Encodings
String
Three encodings: int (8‑byte integer), embstr (≤39 bytes, stored contiguously with the object), and raw (>39 bytes). Conversion from embstr to raw occurs on modification.
List
Encoded as a ziplist for small lists (<512 elements and each element ≤64 bytes) or a doubly linked list otherwise. Conversion is one‑way from ziplist to linked list.
Hash
Internal encoding can be a ziplist or a hash table. Ziplist is used when the hash has <512 entries and all keys/values are ≤64 bytes; otherwise a hash table is used.
Set
Implemented as an intset when all members are integers and the set has <512 elements; otherwise a hash table is used.
Sorted Set
Encoded as a ziplist for ≤128 elements with member strings ≤64 bytes, otherwise a skiplist is used.
Application Examples
Estimating Memory Usage
For 90 000 string key‑value pairs (key and value length 7 bytes, using embstr), each dictEntry occupies 80 bytes (including jemalloc overhead). The bucket array size is the next power of two (131 072) with 8‑byte pointers. Total estimated memory: 90 000 × 80 + 131 072 × 8 = 8 248 576 bytes, which matches experimental results.
Optimizing Memory Usage
Leverage jemalloc size classes: reducing key length from 8 to 7 bytes halves the allocation from 32 bytes to 16 bytes.
Prefer integer storage where possible; Redis stores integers as 8‑byte int values.
Use shared objects for frequently occurring small integers (default 0‑9999, configurable).
Avoid over‑engineering; the memory saved must justify added complexity.
Monitoring Fragmentation
A healthy mem_fragmentation_ratio is around 1.03 for jemalloc. Ratios >1 indicate fragmentation; ratios <1 suggest swapping to virtual memory, which degrades performance. Adjust maxmemory policies or restart Redis to defragment memory when needed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
