Skip to main content

How does Redis use memory?

Redis uses memory as its primary data store - every piece of data, key, structure, and index lives in RAM. That's the foundation of its speed, and also the source of its limits.

Let's break it down:

1. All the data lives in RAM

Redis stores everything: keys, values, and metadata, right in RAM. Every key is an object in memory, usually represented by C structures (dict, sds, and others).

Example: If you write the string "user:1" → "Alice" into Redis, both values, the key and the value itself, live in RAM.

The consequence: operations run in microseconds, but the data volume is limited by how much RAM is available.

2. Memory is managed dynamically

Redis uses a memory allocator (usually jemalloc), which efficiently distributes RAM across its data structures. It tries to minimize fragmentation (memory scattered into small pieces).

A note: over a long-running process with many allocate/free operations, memory can fragment, causing actual RAM usage to grow.

3. A memory limit (maxmemory)

redis.conf lets you set a cap:

javascript
maxmemory 2gb

Once Redis hits that limit, it starts evicting old data according to the chosen policy (LRU, LFU, TTL, and others).

An example setting:

javascript
maxmemory-policy allkeys-lru
  • evict the least-used keys.

The takeaway: Redis can "self-clean" like a cache, without crashing when memory fills up.

4. Persistence doesn't affect the live store

When Redis saves data to disk (dump.rdb, appendonly.aof), it creates a copy from RAM, but the primary state still lives in memory.

Disk's role: backup only, not live operation.

5. How Redis stores different data types

Different structures take up different amounts of memory:

  • String, a minimum of 50-100 bytes per key/value.
  • List and Set, use internal structures (ziplist, hashtable).
  • Hash, can be packed compactly when there are few elements.

Redis automatically picks the optimal internal structure based on the data's size.

6. Memory-analysis tools

Redis has built-in commands:

  • INFO MEMORY, overall memory statistics.
  • MEMORY USAGE key, how much memory a specific key takes up.
  • MEMORY STATS, a detailed breakdown (fragmentation, allocator, overhead).

Summary

Redis uses RAM as its primary data store, manages allocation dynamically, and evicts data by a set policy once it hits its limit.

The main points:

  • Data in RAM = instant access.
  • Disk is used only for backup persistence.
  • The memory limit is controlled via maxmemory.

Redis is, in essence, an intelligent memory manager that behaves like a database.

Short Answer

Interview ready
Premium

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