Video in progress
Why can the memory of a Redis server jump sharply while an RDB snapshot is being taken?
BGSAVE does a fork(), and the child process shares the parent's memory through copy-on-write. During the snapshot, every page the parent writes to gets copied, so with many writes memory can reach almost double.
📖 Answer
The sequence:
- Redis calls
fork(). The child process writes the whole dataset to disk. - At first both use the same physical pages.
- The parent keeps accepting writes. The OS copies every 4 KB page that changes.
If the snapshot takes 2 minutes and half the dataset changes during that time, you need an extra 50% of memory. If the container limit is hard, the OOM killer kills Redis, and this usually happens at night, during an automatic snapshot.
What to check:
INFO persistence:rdb_last_cow_size,latest_fork_usec;fork()itself isn't free either: on a large heap the page table gets copied, and at 20-30 GB that causes a "freeze" of hundreds of milliseconds;- if Transparent Huge Pages is on in Linux, CoW works with 2 MB pages and memory grows much faster. Redis warns about this in its log, so turn THP off;
vm.overcommit_memory = 1, otherwisefork()can fail with "not enough memory".
The practical rule: set maxmemory at around 50-70% of the machine's or container's memory, take snapshots when writes are low, or do persistence on a replica.
Sign up to mark studied questions and save your progress.