Module P-1·20 min read

How BGSAVE uses fork() and copy-on-write, the RDB file format, snapshot scheduling with save directives, RDB compression, and the trade-off: data loss up to the last snapshot interval.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

P-1 — RDB Snapshots: Point-in-Time Persistence

Who this module is for: You know Redis loses data on restart by default and that "persistence" is something you can configure, but you have never understood how it actually works, what the performance cost is, or when RDB is the right choice vs AOF. This module covers the RDB snapshot mechanism end-to-end, including the fork-and-copy-on-write internals that most documentation glosses over.


The Default: No Persistence

When you start Redis with default settings and no redis.conf, persistence is partially enabled via default save directives:

text

For a pure cache (Redis as a read-through cache in front of PostgreSQL), you should disable this:

save "" → disable all automatic snapshots

For any use case where data must survive a restart (session store, rate limiter state, queues), you must configure persistence explicitly — either RDB, AOF, or both.


What RDB Is

RDB (Redis Database Backup) is a point-in-time snapshot of the entire Redis dataset, written to a single compact binary file on disk. By default, this file is named dump.rdb.

When Redis restarts, it reads dump.rdb and restores the entire dataset into memory. Startup time is proportional to the size of the dataset.

The guarantee: All data that existed at the moment the snapshot was taken survives a restart. Data written after the snapshot and before the crash is lost.

The trade-off: The maximum data loss equals the time between snapshots. With save 60 10000, you can lose up to 60 seconds of writes.


How BGSAVE Works: Fork and Copy-on-Write

This is the part that most tutorials skip, and it is the most important thing to understand about RDB.

Redis cannot stop serving requests while it writes the snapshot — that could take minutes for a large dataset. Instead, it uses the Unix fork() system call to create a child process that handles the snapshot while the parent continues serving clients.

text

Copy-on-Write (CoW)

After fork(), both the parent and child share the same physical memory pages. The OS uses copy-on-write semantics: when the parent modifies a page (because a client writes to Redis), the OS creates a private copy of that page for the parent. The child still sees the original page.

This means:

  • The child writes the snapshot based on the data as it existed at fork time — a consistent point-in-time view
  • The parent continues serving writes normally
  • Modified pages are duplicated in RAM — one version for the parent (new value) and one for the child (old value, being written to disk)

The memory spike: During a snapshot, Redis can temporarily use up to 2× its normal working set of RAM. If your dataset is 4GB and writes are heavy during the snapshot, Redis might use 6-7GB before the snapshot finishes and the child exits.

This is why you must size your Redis server's RAM to accommodate BGSAVE overhead. A rule of thumb: provision 1.5× your expected dataset size for a write-heavy Redis.

Fork Latency

fork() itself is a fast system call — it does not copy memory, only the page table. But on large datasets, the page table itself can be large. Redis may pause briefly (milliseconds) while the OS copies the parent's page table for the child.

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.