Skip to content

Lesson 4 of 9 · 23m

Memory Management: maxmemory, Eviction, and Fragmentation

How Redis actually uses memory, which eviction policy belongs on which workload, and how fragmentation and big keys cause death by a thousand cuts.

Redis memory incidents rarely begin with 'we stored too much data.' They begin with memory used by data plus overhead, replicas, buffers, fragmentation, and a eviction policy nobody remembers setting. Then the first OOM command not allowed appears and everyone discovers that 'cache' and 'database' teams had different assumptions.

Set the ceiling before Redis hits the machine ceiling

maxmemory 12gb
maxmemory-policy allkeys-lru

maxmemory is Redis' dataset-management boundary, not total process memory. Replication buffers, client output buffers, AOF rewrite work, fragmentation, and allocator overhead still consume RAM outside that accounting. Leave headroom — especially on systems where fork-based persistence relies on copy-on-write.

Pick eviction by data semantics

Common policies:

  • noeviction — reject writes when full; correct for authoritative data.
  • allkeys-lru — evict least recently used keys from everything; classic cache behavior.
  • volatile-lru — evict only keys with TTLs; dangerous if most keys lack expiry.
  • allkeys-lfu / volatile-lfu — frequency-based, often better for mixed hot/cold cache traffic.
  • allkeys-random / volatile-random — predictable memory behavior, unpredictable product behavior.
  • volatile-ttl — evict keys nearest expiry first; occasionally useful, often surprising.

A cache with noeviction becomes a write-failing database at the exact moment traffic is highest. A source-of-truth store with allkeys-lru silently deletes user data. Neither is a tuning detail.

Know the memory composition

redis-cli INFO memory
redis-cli MEMORY STATS
redis-cli MEMORY DOCTOR

Watch used_memory, used_memory_rss, mem_fragmentation_ratio, allocator stats, and client/output buffer growth. RSS above logical usage is normal to a point; severe fragmentation means the allocator cannot efficiently return or reuse memory.

A fragmentation ratio around 1.0 is comfortable. Persistently above roughly 1.5 deserves investigation. Very low ratios can mean swapping — usually worse.

Big keys are operational debt

A single 500 MB hash is not one harmless key. It makes deletion expensive, migration slow, replication painful, and latency spiky. Find them before they find you:

redis-cli --bigkeys
redis-cli --memkeys

These are sampling/diagnostic tools, not exact ledgers. For exact accounting, inspect object encoding and use MEMORY USAGE key on representative keys.

TTL is a memory-control feature

Unbounded growth is the normal failure mode. Keys written without TTL tend to become immortal because nobody knows whether they are safe to delete. Put expiry at creation time where the business logic permits it:

SET session:abc payload EX 3600

Use EXPIRE for immutable lifetimes and deliberate extension semantics. Avoid touching expiry as a side effect of every read unless refreshing sessions is truly intended.

Field notes

  • DEBUG SLEEP and DEBUG JMAP have no place near production because they are troubleshooting tools, not toys.
  • Eviction metrics matter: evicted_keys greater than zero means your capacity plan is already wrong or your policy is doing exactly what you asked.
  • Lazy expiration plus active expiry is probabilistic; a huge pile of expired keys can still occupy memory until accessed or reaped.
  • Deleting a giant key can block older Redis versions; use UNLINK for asynchronous deletion where semantics allow.
  • Replication memory pressure is real memory pressure. Backlog and replica output buffers grow during slow replicas and network events.

Was this lesson helpful?

Sign in to rate