Skip to main content

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:

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:

TypePossible encodingsPurpose
Stringint, embstr, rawstoring a number, a short string, or a long string
Listquicklista compact list with zip blocks
Hashziplist / hashtablesaving memory when there are few fields
Setintset / hashtableoptimized for numeric values
ZSetziplist / skiplist+hashcompact or full-size storage
Streamlistpackdense 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.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.