Suggest an editImprove this articleRefine the answer for “How does Redis manage memory? How does Redis allocate memory for keys and values?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)Redis actively controls memory usage through the jemalloc allocator (faster than standard malloc/free and less prone to fragmentation), and every key and value is a `redisObject` carrying a type, an encoding (int/embstr/raw for strings, etc.), and a reference to the actual data. **Key point:** the `maxmemory` setting caps total memory use, and once that limit is hit, an eviction policy kicks in (`noeviction`, `allkeys-lru`, `volatile-ttl`, and others); memory state can be checked via `INFO MEMORY`, `MEMORY USAGE key`, and `MEMORY STATS`.Shown above the full answer for quick recall.Answer (EN)ImageRedis manages memory **at the byte level inside RAM**, using its own structures and allocation strategy, tied to its in-memory architecture. It doesn't rely entirely on the OS, it actively controls **how much and how memory is used**, to keep speed at a maximum and behavior predictable. ## 1. The overall memory model Redis stores all its data in RAM. Every element in the database is a pair: ```javascript key → value ``` Both parts are represented as **Redis objects (redisObject)**. These objects take up space on the heap, managed by Redis through its chosen allocator (jemalloc, by default). ## 2. Allocating memory Redis uses **jemalloc**, because it: - is faster than standard `malloc`/`free`; - fragments memory less; - efficiently handles the huge number of small objects typical of Redis. When Redis creates a key or a value, it requests memory from jemalloc and keeps a reference to the allocated region. If the data grows, Redis can **extend** a buffer (SDS) without a full copy, keeping some "free space" for future changes. ## 3. The storage format Every element is stored as a `redisObject` structure, holding: - the data type (`string`, `list`, `hash`, etc.); - the encoding method (e.g. `int`, `embstr`, `raw`); - a reference to the actual data; - a reference counter and flags; - an expiration time (TTL), if set. Example: If a value is a short string, Redis can use **embstr**, placing the string and its metadata in a single block of memory for faster access. If the string is long, **raw** is used instead, keeping the string and the object separate. ## 4. Encodings and storage optimization Redis adaptively picks an **encoding** depending on the data's size and type: | Type | Possible encodings | Purpose | |---|---|---| | String | `int`, `embstr`, `raw` | storing a number, a short string, or a long string | | List | `quicklist` | a compact list with zip blocks | | Hash | `ziplist` / `hashtable` | saving memory when there are few fields | | Set | `intset` / `hashtable` | optimized for numeric values | | ZSet | `ziplist` / `skiplist+hash` | compact or full-size storage | | Stream | `listpack` | dense storage of events | Redis automatically re-encodes structures whenever their size or contents change. ## 5. Memory management (maxmemory and eviction) If this setting is defined: ```javascript maxmemory <bytes> ``` Redis caps the total amount of memory it uses. Once that limit is reached, an **eviction policy** (removing keys) kicks in. The available policies: - `noeviction`, don't accept new data (the default for persistent Redis); - `volatile-lru`, remove keys with a TTL that haven't been used in a while; - `allkeys-lru`, remove any old keys (the most common caching option); - `volatile-ttl`, remove keys closest to expiring; - `volatile-lfu` / `allkeys-lfu`, based on how often keys are used. ## 6. Checking and reporting usage Redis tracks memory statistics: ```bash INFO MEMORY ``` This shows: - the total volume, - fragmentation, - per-object overhead, - the allocator in use, - and details on the AOF/RDB buffers. To analyze individual keys: ```bash MEMORY USAGE key MEMORY STATS MEMORY PURGE ``` ## 7. Freeing memory and defragmentation Redis doesn't always return freed memory to the OS immediately. Instead, jemalloc reuses the blocks. Redis can force memory defragmentation with: ```bash MEMORY DOCTOR MEMORY PURGE ``` or free unneeded objects asynchronously in the background (`lazyfree`). ## 8. Summary Redis manages memory like this: 1. **All data lives in RAM.** 2. **Allocation** happens through **jemalloc**. 3. **Keys and values** are represented as objects with a type and an encoding. 4. **The storage format** is picked dynamically to save memory. 5. **maxmemory** caps the total volume, with old data possibly evicted. 6. **Defragmentation and reporting** are supported by built-in mechanisms. All of this makes Redis **as fast as possible**, while still keeping memory usage under control, even with very large datasets.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.