Scrivio Interview library
DebuggingIntermediate6 min read · updated 13 November 2026

A cache Redis on a 32 GB VM held 22 GB of data. A cleanup job deleted about 40 percent of the keys, yet the host still shows Redis at 23 GB resident. INFO reports used_memory of 13 GB and mem_fragmentation_ratio 1.75.

Why did resident memory not fall, and what are your options to reclaim it without an outage?

Resident memory did not fall because jemalloc packs values of the same size class into pages and returns a page to the operating system only when it is completely empty; deleting 40 percent of keys scattered across memory left most pages partly used. To reclaim it without an outage, I would enable active defragmentation with conservative CPU limits and watch latency, try MEMORY PURGE for pages that are already free, and if that is not enough, fail over to a freshly synced replica, which loads the data compactly, and rebuild the old node.

Why this question is asked

After a big cleanup, everyone expects the memory graph to drop, and when it does not, the first guess is a leak. Interviewers ask this because the real answer requires understanding how a memory allocator works, and because the options for fixing it differ in risk. In this answer, we will see how jemalloc lays out memory, why scattered deletes leave pages stranded, then go through active defragmentation, MEMORY PURGE and the replica failover, and finish with how to avoid the surprise next time.

1How jemalloc Lays Out Memory

jemalloc, the allocator Redis uses by default on Linux, does not hand out arbitrary sizes. It rounds every request up to a size class, such as 32, 48, 64, 80 or 128 bytes, and it groups blocks of the same class together in runs of pages. All 48-byte blocks of a run live side by side in the same pages.

When Redis frees a block, jemalloc marks it free in its run and can reuse it for the next allocation of that class. But it can return memory to the operating system only in whole pages, and only when every block in the page is free. One live 48-byte value in a 4 KB page keeps the whole page resident.

2Why Scattered Deletes Leave RSS High

The cleanup job deleted 40 percent of keys, but not 40 percent of pages. Keys created over months are spread through memory, so the deleted ones were sprinkled across almost every page. Most pages went from full to about 60 percent full; very few became completely empty.

So used_memory, the allocator's count of blocks in use, fell from about 22 GB to 13 GB, while used_memory_rss, the pages the operating system sees, stayed at 23 GB. Their ratio, mem_fragmentation_ratio, is 1.75. Nothing leaked; the free space is real, it is just in holes that only same-sized future allocations can fill.

Pages before and after the cleanup
PageBefore cleanupAfter deleting 40 percent of keys
Page 110 of 10 blocks used6 of 10 used, stays resident
Page 210 of 10 blocks used5 of 10 used, stays resident
Page 310 of 10 blocks used7 of 10 used, stays resident
Page 410 of 10 blocks used0 of 10 used, returned to the OS
Totalused 22 GB, RSS 22-23 GBused 13 GB, RSS 23 GB, ratio 1.75
Only completely empty pages go back; the rest keep RSS near the old peak.

3Option 1: Active Defragmentation

Active defragmentation makes Redis move values out of sparse pages into fuller ones: for each value in a sparsely used page, it allocates a new copy where jemalloc prefers, updates the pointers and frees the old block, so the sparse pages empty out and can be returned. It only works with the jemalloc bundled with Redis, because it relies on a jemalloc hint about where a block lives.

Text
CONFIG SET activedefrag yes
# start only when there is real waste:
CONFIG GET active-defrag-ignore-bytes -- 100mb (ignore less waste than this)
CONFIG GET active-defrag-threshold-lower -- 10 (start above 10 percent fragmentation)
# how much main-thread CPU it may use, in percent:
CONFIG SET active-defrag-cycle-min 1
CONFIG SET active-defrag-cycle-max 10 -- raise gradually, watch p99
INFO memory | grep -E 'allocator_frag_ratio|used_memory_rss'
INFO stats | grep active_defrag_hits -- values moved so far

The important caveat is that defragmentation runs on the main thread, in small steps between commands. Its CPU share is bounded between active-defrag-cycle-min and active-defrag-cycle-max percent, and the defaults vary by version, so check yours. Start low, watch p99 and main-thread CPU, and raise the maximum only while latency stays acceptable. On 13 GB of data it may take hours at a low setting, which is fine; there is no deadline.

4Option 2: MEMORY PURGE

MEMORY PURGE asks jemalloc to release pages that are already entirely free but which it kept for reuse. It is instant and cheap, so it is worth running first, but it does not move any data, so it cannot fix pages that still hold even one live value. In this case it might return a few hundred megabytes, not the 9 GB we hope for.

5Option 3: Fail Over to a Fresh Replica

The clean fallback is a replica. A replica that performs a full sync loads the dataset from a snapshot and allocates every value fresh, so its memory is compact, close to used_memory. We can add a new replica (or force a full resync of an existing one), wait until it is in sync, check that its RSS is near 13 GB, and then fail over to it with Sentinel, the cluster's FAILOVER command, or the provider's tooling. Clients see a brief reconnect, not an outage. The old node is then rebuilt as a replica and comes back compact too.

This is often the fastest way to recover 9 GB on a host under pressure, and it also proves that our failover works, which is worth knowing anyway.

6Avoiding the Surprise Next Time

Two habits help. Alert on allocator_frag_ratio, which isolates fragmentation inside jemalloc, and on RSS headroom on the host, not only on used_memory. And size hosts for RSS, with room for fragmentation, buffers and copy-on-write, rather than for the data alone. Large deletes can also be planned: run them with UNLINK in batches, and turn on active defragmentation before the cleanup rather than after it.

7Follow-up questions

Why does a mix of 100-byte and 5 KB values fragment more than uniform values?

Different sizes land in different size classes, each with its own pages. When the workload shifts, for example many 5 KB values are deleted and many 100-byte values created, the freed 5 KB slots cannot be used for 100-byte values, so those pages stay mostly empty while new pages are allocated for the small class. Uniform values reuse each other's slots exactly, so holes are filled quickly.

Is a fragmentation ratio of 1.75 dangerous by itself?

Not by itself; it wastes RAM. It becomes dangerous when RSS approaches the host's memory, because then a fork for BGSAVE, with its copy-on-write, or a burst of writes can trigger the out-of-memory killer. That is why the headroom matters more than the ratio.

Will a restart fix it?

Yes, because loading the RDB allocates everything compactly, but a restart of a primary without failover is downtime, and a cache restarted without persistence starts empty. The replica failover gets the same result without either.

8Conclusion

In this answer, we saw that jemalloc groups blocks by size class into pages and returns only completely empty pages, so deleting scattered keys lowered used_memory but left RSS near the old peak with a 1.75 ratio. Active defragmentation compacts memory gradually on the main thread within CPU limits we raise carefully, MEMORY PURGE releases already empty pages, and a failover to a freshly synced replica gives a compact copy with only a brief reconnect. Alerts on allocator_frag_ratio and RSS headroom, and sizing for RSS, prevent the surprise.

What to say in the interviewAbout 80 seconds aloud

Open with

jemalloc puts allocations of the same size class together in pages, and a page only goes back to the operating system when it is completely empty. The cleanup deleted keys scattered across memory, so most pages went from full to partly full and stayed resident.

Then, if they want more

That is why used_memory dropped to 13 GB while RSS stayed at 23 GB and the fragmentation ratio is 1.75; nothing leaked. My first option is active defragmentation: activedefrag yes, which needs the bundled jemalloc.

It starts when waste exceeds active-defrag-ignore-bytes, 100 MB, and fragmentation is above active-defrag-threshold-lower, 10 percent, and it moves values into compact pages using CPU between active-defrag-cycle-min and cycle-max. Because it runs on the main thread, I start with a low maximum and raise it gradually while watching p99.

MEMORY PURGE is a cheap first step that releases pages that are already free. If I need the memory back quickly, the clean option is a replica: a freshly synced replica loads the data compactly, so I fail over to it and rebuild the old node.

Going forward I would alert on allocator_frag_ratio and RSS headroom, and size hosts with room for fragmentation, not just for used_memory.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why did resident memory not fall, and what are your options to reclaim it without an...": https://scrivio-interview-library.netlify.app/databases/redis-internals/7f75ccedc1/