Memory Fragmentation

Difficulty: Advanced

Question

What does `mem_fragmentation_ratio` in `INFO memory` mean, how do you interpret high and low values, and how do you fix fragmentation?

Answer

mem_fragmentation_ratio is used_memory_rss / used_memory: the physical memory the operating system has given the Redis process divided by the memory Redis believes it has allocated for data. Around 1.0 to 1.5 is normal. A value well above 1.5 means a lot of memory is wasted in fragmented allocator pages, and a value below 1.0 usually means part of Redis has been swapped to disk, which is far worse.

Where fragmentation comes from

Redis allocates through jemalloc, which groups allocations into size classes inside larger pages. When you delete or shrink many keys, the freed slots are scattered across pages. jemalloc can reuse them for new allocations of similar size, but it cannot hand a page back to the OS while even one live allocation remains in it. So:

  • used_memory drops after deletes,
  • used_memory_rss stays high,
  • the ratio climbs.

Typical triggers: a large cache that evicted or expired many keys, workloads where value sizes change over time (small sessions replaced by large JSON blobs), and big batch deletions after a peak.

Reading the numbers

RatioMeaningAction
< 1.0RSS smaller than allocated: memory is swapped outUrgent: add RAM, reduce data, disable or reduce swap
1.0 to 1.5Healthy, normal allocator overheadNothing
> 1.5Significant fragmentationEnable active defrag or plan a restart/failover

Two caveats:

  • On a nearly empty instance the ratio looks huge because fixed process overhead dominates. Look at mem_fragmentation_bytes too; a ratio of 3 on 5 MB of data is irrelevant.
  • Newer INFO memory output breaks it down into allocator_frag_ratio (fragmentation inside jemalloc, which active defrag can fix) and allocator_rss_ratio / rss_overhead_ratio (pages not yet returned or non-allocator memory). MEMORY DOCTOR gives a human-readable diagnosis.

Fixing it

  1. Active defragmentation (Redis 4.0+, needs the bundled jemalloc): CONFIG SET activedefrag yes. Redis moves live values into fresh allocations in small background steps. Tune with active-defrag-ignore-bytes (default 100mb), active-defrag-threshold-lower (default 10 percent), active-defrag-threshold-upper and the CPU bounds active-defrag-cycle-min / active-defrag-cycle-max.
  2. MEMORY PURGE asks jemalloc to release dirty pages it is holding; it helps with page retention, not true fragmentation.
  3. Restart or fail over. A replica that loads from a fresh sync has a compact layout. Promoting it and recycling the old primary removes fragmentation completely.

Why it matters for capacity planning

maxmemory is compared against used_memory, not RSS. With a ratio of 1.6, a maxmemory 10gb instance can use about 16 GB of real RAM and get killed by the OOM killer on a 16 GB VM. Leave headroom for fragmentation and for copy-on-write during RDB/AOF rewrites.

How fragmentation builds up. Deletes shrink used_memory, but partly filled pages keep RSS high.
  1. Pages filled with values: RSS and used_memory grow together.
  2. Many keys expire or are deleted: used_memory drops.
  3. Pages still hold a few live values: jemalloc cannot return them to the OS.
  4. RSS stays high: mem_fragmentation_ratio rises above 1.5.
  5. Active defrag relocates values: Emptied pages are released.

Code examples

Diagnosing and enabling active defrag

redis-cli INFO memory | grep -E 'used_memory:|used_memory_rss:|mem_fragmentation|allocator_frag'

redis-cli MEMORY DOCTOR

# Turn on active defragmentation at runtime
redis-cli CONFIG SET activedefrag yes
redis-cli CONFIG SET active-defrag-ignore-bytes 200mb
redis-cli CONFIG SET active-defrag-threshold-lower 15

# Watch progress
redis-cli INFO stats | grep active_defrag

Here roughly 4.5 GB is wasted. Active defrag starts only once both the ignore-bytes and lower-threshold conditions are met, and it uses bounded CPU.

Key points

  • mem_fragmentation_ratio = used_memory_rss / used_memory
  • 1.0 to 1.5 is normal; above 1.5 is fragmentation; below 1.0 suggests swapping
  • Ignore the ratio on tiny datasets; check mem_fragmentation_bytes
  • Fix with activedefrag yes (4.0+, jemalloc), or a restart/failover to a fresh replica
  • maxmemory counts used_memory, not RSS, so leave RAM headroom

Common follow-up questions

  • Why is a ratio below 1 more dangerous than a high ratio?
  • How does active defragmentation work without blocking Redis?
  • How much RAM would you provision for a 10 GB dataset and why?
  • What else besides fragmentation can make RSS larger than used_memory?

Interview tips

  • State the formula precisely and both directions of the ratio
  • Mention the maxmemory vs RSS capacity trap; it shows real operational experience
  • Name jemalloc and active defrag rather than only saying 'restart Redis'

Concepts covered

mem_fragmentation_ratio, used_memory_rss, jemalloc, Active defragmentation, Swapping

More Redis & Caching questions