Why Redis BGSAVE Doubles Your Memory (and How to Fix It)

Murtaza Shajapurwala ·

Your Redis has sat at 12 GB for days. A snapshot starts, the container climbs toward 24 GB, and the kernel's OOM killer takes something down. Or the log says Can't save in background: fork: Cannot allocate memory, and snapshots stop altogether. Run ps at the wrong moment and you see two processes of the same size: redis-server and redis-rdb-bgsave.

None of that is a leak. It's how Redis and Valkey take snapshots. This post explains why BGSAVE doubles memory, how to size and configure for it, and how a server that snapshots without forking avoids it. We measured it on Sentry self-hosted's own Valkey setup, where it was reported at 26 GB + 26 GB.

Why does Redis memory double during BGSAVE?

Redis and Valkey write a point-in-time snapshot (BGSAVE, a scheduled save, or the RDB sent to a new replica) by forking:

  1. The server calls fork(). The child process, shown as redis-rdb-bgsave in ps, sees the dataset frozen at that instant and writes it to dump.rdb.
  2. The parent keeps serving clients. Parent and child share the same memory pages.
  3. When the parent changes a page, the kernel copies it first, so the child still sees the old version. This is copy-on-write.

On a quiet instance, few pages change during the save, and the extra memory stays small. On a busy one (a job queue, an event pipeline, a cache under constant writes), almost every page is touched before the child finishes. The copies grow toward the size of the whole dataset. Peak memory approaches twice your data for as long as the snapshot runs. Redis logs the amount at the end of a save: RDB: 2930 MB of memory used by copy-on-write.

What is redis-rdb-bgsave, and why is it using so much RAM?

It's the forked child writing the snapshot. Its RSS looks like a full copy of the server, because it maps the same pages. The real extra cost is the pages copied since the fork, and under heavy writes that approaches a full copy.

Why does BGSAVE fail with "fork: Cannot allocate memory"?

Before the fork succeeds, Linux has to agree that both processes could, in the worst case, need their full memory. With the default vm.overcommit_memory = 0, a large Redis on a host without that much free memory gets its fork refused. The save fails, and with stop-writes-on-bgsave-error yes (the default) Redis then refuses writes as well.

How to stop Redis running out of memory during snapshots

  1. Leave headroom equal to your dataset. Check used_memory in INFO memory, and give the container or host room for up to twice that.
  2. Set vm.overcommit_memory = 1. Redis's own docs recommend it. The fork then succeeds even when memory is tight, but the copies still need real RAM under writes.
  3. Turn off transparent huge pages. With THP, a single changed byte copies a 2 MB page instead of a 4 KB one, which multiplies copy-on-write.
  4. Cap the dataset with maxmemory. With maxmemory 0 (no limit), a busy period can grow the data until the next snapshot no longer fits.
  5. Snapshot less, or not at all, if the data can be rebuilt: save "" turns scheduled snapshots off. AOF (appendonly yes) still forks for rewrites, but less often.
  6. Use a server that doesn't fork. That's the rest of this post.

Snapshotting without a fork: measured on Sentry self-hosted's Valkey

Sentry self-hosted ships valkey/valkey:8.1.11-alpine with a redis.conf that sets only maxmemory 0 and maxmemory-policy volatile-lru. Default snapshots are on and the dataset is unbounded, which is exactly the setup from the report above.

We ran that image and config, loaded 6 million keys with 64-byte values (about 640 MiB of data), and ran BGSAVE while two clients kept overwriting random keys. Then we ran the same test against KiviDB 1.2.2 on the same 8-vCPU machine. Every server was started fresh for each run, and each test was run twice.

Peak memory during a Redis/Valkey BGSAVE under write load: Valkey 8.1.11 1,283 MiB versus KiviDB 1.2.2 652 MiB; time to finish: Valkey 3.13 s versus KiviDB 1.25 s.

Valkey 8.1.11 (Sentry self-hosted config)KiviDB 1.2.2
Peak memory during the snapshot1,282 / 1,283 MiB640 / 652 MiB
Time to finish BGSAVE3.05 / 3.13 s1.20 / 1.25 s

Valkey peaks at twice the data: that's the fork. KiviDB peaks at the data, and finishes in under half the time. Redis 8.10.3 on the same test behaved like Valkey: 1,422 MiB at peak and 3.9 s, against KiviDB's 679 MiB and 1.2 s.

How KiviDB takes a snapshot without forking

KiviDB is a multi-threaded, Redis-compatible server. It captures the dataset inside the process:

  • String values are immutable shared buffers. Holding a reference to one already is a point-in-time copy, with no bytes duplicated. In most Redis datasets, strings are the bulk of the memory.
  • Hashes, lists, sets, sorted sets and streams are serialised during the capture, because they can change in place.
  • The file is written out afterwards, while clients keep being served.

There is no child process and no copy-on-write, so there's no second copy to grow, whatever your write rate. Memory during a snapshot stays at your data plus the file being written. There's no vm.overcommit_memory or THP tuning for it, and fork: Cannot allocate memory can't happen.

Try it with your own snapshot

KiviDB reads a Redis or Valkey dump.rdb at startup. Put yours in a directory and start KiviDB on it:

docker run -d --name kividb -p 6379:6380 \
  -v /path/with/dump.rdb:/home/kividb \
  kividb/kividb:latest --aof --requirepass "change-me"

Point a copy of your application at it, and compare memory during BGSAVE with what you see today. If you run Sentry self-hosted and hit this, we're following up on getsentry/self-hosted#4619 and would like to test it on your workload with you.