Quick revision
The short answer
At 02:00 the RDB snapshot forks Redis, and copy-on-write duplicates every memory page the parent changes while the child writes, so with 40 GB of data under heavy writes the process can approach 80 GB and the OOM killer ends it on a 64 GB machine. The daytime spikes come from the fork itself, whose cost grows with the page table, and from AOF rewrites that fork too, made worse by transparent huge pages. We fix it by leaving memory headroom, setting vm.overcommit_memory = 1, disabling THP and snapshotting at quiet times, or better by moving persistence to a replica or sharding into smaller instances, and by asking whether a cache needs persistence at all.
What to remember
In this answer, we saw that RDB snapshots and AOF rewrites fork Redis, and copy-on-write copies every page the parent modifies during the snapshot, so a 40 GB dataset under heavy writes can need far more than 64 GB and the OOM killer ends Redis at 02:00. The fork itself and transparent huge pages cause the daytime latency spikes. We verified it with latest_fork_usec, the copy-on-write sizes and the kernel log, and fixed it with THP off, overcommit set to 1, headroom and quieter snapshot times, then structurally by persisting on a replica, sharding, or dropping persistence for a pure cache.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
At 02:00 the RDB snapshot forks Redis, and copy-on-write duplicates every memory page the parent changes while the child writes, so with 40 GB of data under heavy writes the process can approach 80 GB and the OOM killer ends it on a 64 GB machine. The daytime spikes come from the fork itself, whose cost grows with the page table, and from AOF rewrites that fork too, made worse by transparent huge pages. We fix it by leaving memory headroom, setting vm.overcommit_memory = 1, disabling THP and snapshotting at quiet times, or better by moving persistence to a replica or sharding into smaller instances, and by asking whether a cache needs persistence at all.
Why this question is asked
This scenario combines two symptoms that look unrelated, a nightly crash and daytime latency, and both come from the same mechanism: how Redis uses fork() to persist data while it keeps serving clients. Interviewers like it because it tests whether we understand what happens in the operating system underneath Redis, not just its configuration file. In this answer, we will recap how fork and copy-on-write work, explain the nightly OOM kill with numbers, explain the daytime spikes, verify the diagnosis, and then go through the fixes from quick to structural.
1A Quick Recap of fork() and Copy-on-Write
To write an RDB snapshot or rewrite the AOF, Redis calls fork(). The operating system creates a child process that initially shares all memory pages with the parent; nothing is copied yet. The child then has a frozen view of the data and writes it to disk while the parent continues serving clients.
The sharing works through copy-on-write. As long as neither process changes a page, both use the same physical memory. When the parent modifies a page, because a client wrote a key stored on it, the kernel first copies that page so the child keeps its frozen version. Every modified page therefore exists twice until the child finishes. Pages are 4 KB normally, so a write to a single small key can copy one 4 KB page.
2Why the OOM Killer Strikes at 02:00
The machine has 64 GB. Redis holds 40 GB of data, plus some fragmentation and buffers, so it starts the night at perhaps 42 GB of resident memory. At 02:00 the save rule triggers BGSAVE. Writing 40 GB to disk takes several minutes, and if the nightly batch jobs or regular traffic write across much of the keyspace during that time, a large fraction of pages are copied.
6 messages in order. Play them, or step through them one at a time.
Copy-on-write memory belongs to the machine, not to Redis's maxmemory.
Notice that the operating system's OOM killer, not Redis, ends the process. Redis's maxmemory limit counts only the data Redis allocates; it does not include the copies the kernel makes for the child. So a dataset that is well within maxmemory can still exhaust the machine during a snapshot. Redis then restarts, loads its data from disk and the nightly batch hits a cold or restarting server.
3Why Latency Spikes During the Day
The daytime spikes have related causes. The fork call itself is not free: the kernel must copy the parent's page table, the map from virtual to physical memory, and for 40 GB of 4 KB pages that is about 10 million entries. While that happens the main thread is blocked, which can take a few hundred milliseconds on some virtual machines. AOF rewrites run during the day whenever the AOF grows past its rewrite threshold, so every rewrite causes a fork pause.
Transparent huge pages make it worse. THP lets the kernel use 2 MB pages instead of 4 KB ones. Then a write to a single key copies a 2 MB page instead of 4 KB, so copy-on-write uses far more memory and CPU, and latency rises after every fork. Redis warns about THP in its startup log for this reason. Finally, with appendfsync everysec, a disk busy with a snapshot or rewrite can make fsync slow, and Redis may delay writes, logging that asynchronous AOF fsync is taking too long.
4Verifying the Diagnosis
Before changing settings, we confirm. Redis reports the copy-on-write cost of each fork in INFO persistence, and INFO stats tells us how long the last fork took.
If the copy-on-write size plus the dataset approaches the machine's RAM, and the latency spikes line up with latest_fork_usec and the times of AOF rewrites, the diagnosis is confirmed. LATENCY HISTORY fork, with latency monitoring enabled, shows the same picture over time.
5The Fixes, From Quick to Structural
The quick fixes come first. Disable THP with never, so copy-on-write works on 4 KB pages again. Set vm.overcommit_memory = 1, which Redis recommends: without it, the kernel may refuse the fork because it fears the child could double memory, and BGSAVE fails with a fork error. Note that this setting prevents failed forks; it does not create memory, so it does not prevent the OOM kill by itself. Move the snapshot to the quietest time, after the batch jobs rather than during them.
The real fix is headroom. With persistence that forks, a Redis primary should not use most of the machine's RAM; a common rule is to keep maxmemory well below RAM, so a heavy-write snapshot still fits. For this instance that means either a larger machine or less data per instance.
The structural fixes remove the fork from the primary. We can disable RDB and AOF on the primary and enable them on a replica, so the replica forks and the primary serves clients undisturbed; the risk is that if the primary restarts with persistence off, it comes back empty and replicates its emptiness, so automatic restarts of the primary must be disabled or it must resync from the replica. Or we shard the 40 GB into four instances of 10 GB, so each fork is a quarter of the size and the copies fit easily.
Finally, we ask whether this data needs persistence at all. If it is a cache that can be rebuilt from the database, turning persistence off removes every fork, and the only cost is a cold start after a restart.
- THP enabled?YesSet it to neverNoKeep it off
- vm.overcommit_memory = 1 set?YesFineNoSet it so forks do not fail
- Is the data a rebuildable cache?YesTurn persistence offNoKeep persistence
- Can a replica do the persistence?YesPersist on the replica onlyNoShard or add RAM headroom
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
How would you verify that copy-on-write is the cause?
Compare rdb_last_cow_size and aof_last_cow_size from INFO persistence with the free memory on the machine, and read the Redis log line that reports the copy-on-write megabytes after each save. Then check the kernel log for the OOM kill at the time of the snapshot. If the dataset plus the copied memory exceeds RAM exactly when the 02:00 save runs, copy-on-write is the cause.
Why does Redis recommend vm.overcommit_memory = 1?
With the default heuristic mode, the kernel may refuse a fork when free memory looks too small to cover a full copy of the parent, even though copy-on-write usually needs much less. The fork then fails and the snapshot is skipped. Setting it to 1 lets the fork succeed; real memory use still has to fit, which is why headroom remains essential.
Could we just use a bigger machine?
Yes, and moving from 64 GB to 128 GB would stop the OOM kills quickly. But the fork pauses grow with the dataset, so latency spikes remain, and costs grow too. A bigger machine buys time; sharding or persisting on a replica removes the cause.
7Conclusion
In this answer, we saw that RDB snapshots and AOF rewrites fork Redis, and copy-on-write copies every page the parent modifies during the snapshot, so a 40 GB dataset under heavy writes can need far more than 64 GB and the OOM killer ends Redis at 02:00. The fork itself and transparent huge pages cause the daytime latency spikes. We verified it with latest_fork_usec, the copy-on-write sizes and the kernel log, and fixed it with THP off, overcommit set to 1, headroom and quieter snapshot times, then structurally by persisting on a replica, sharding, or dropping persistence for a pure cache.
Open with
Both symptoms come from fork. At 02:00 the RDB save calls fork, and the child shares memory with the parent through copy-on-write: every page the parent modifies while the child is writing gets copied.
Then, if they want more
With 40 GB of data and heavy writes during the snapshot, that can approach double the dataset, which does not fit in 64 GB, so the kernel's OOM killer kills Redis. Redis's maxmemory does not count those copies.
During the day, AOF rewrites fork too, and the fork itself blocks the main thread while the page table is copied, which grows with the dataset; latest_fork_usec in INFO stats shows it. Transparent huge pages make it much worse because each write copies 2 MB pages.
I would verify with rdb_last_cow_size, the copy-on-write line in the Redis log, and the OOM message in dmesg. Quick fixes: disable THP, set vm.overcommit_memory to 1 so forks do not fail, and schedule snapshots when writes are low. The real fix is headroom, keeping maxmemory well below RAM.
Structurally, I would move persistence to a replica so the primary never forks, or shard into smaller instances so each fork is cheap. And if this is a cache, I would ask whether it needs persistence at all.
About this answer
- Last updated
- 12 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 "A Redis instance with 40 GB of data on a 64 GB machine is killed by the OOM killer every...": https://scrivio-interview-library.netlify.app/databases/redis/305fb5939b/