How does Redis manage memory? How does Redis allocate memory for keys and values?
Redis 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:
key → valueBoth 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:
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:
INFO MEMORYThis shows:
- the total volume,
- fragmentation,
- per-object overhead,
- the allocator in use,
- and details on the AOF/RDB buffers.
To analyze individual keys:
MEMORY USAGE key
MEMORY STATS
MEMORY PURGE7. 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:
MEMORY DOCTOR
MEMORY PURGEor free unneeded objects asynchronously in the background (lazyfree).
8. Summary
Redis manages memory like this:
- All data lives in RAM.
- Allocation happens through jemalloc.
- Keys and values are represented as objects with a type and an encoding.
- The storage format is picked dynamically to save memory.
- maxmemory caps the total volume, with old data possibly evicted.
- 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.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.