CodePark/ questions / Persistence (RDB & AOF)
START PRACTICINGPRACTICE
Backend question · Redis & caching › Persistence (RDB & AOF)

How does BGSAVE take a snapshot without stopping Redis, and what is the memory risk?

BGSAVE calls fork() to create a child process. The child sees memory frozen at that moment and writes it to the RDB file. Meanwhile, the parent keeps serving commands.

This works because of copy-on-write. The parent and child share the same memory pages. When the parent changes a page, the operating system copies that page first. Only changed pages are copied.

The risks:

  • Many writes during the snapshot change many pages. In the worst case, memory use gets close to double the dataset.
  • The fork() call itself can pause the main thread briefly on very large instances, because the page tables must be copied.

What to do:

  • Leave free memory below the machine's RAM. Do not set maxmemory close to it.
  • Turn off Linux transparent huge pages for Redis. They make each page much bigger, so every copied page costs much more memory.
  • fork() creates a child process that writes the snapshot.
  • Copy-on-write shares memory pages until the parent changes them.
  • Heavy writes during a save can nearly double memory use.
  • The fork itself can cause a short latency spike on big instances.
  • Leave free memory headroom and turn off transparent huge pages.