Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

2 · RDB snapshots

In one sentence: Redis forks a child process that writes the dataset, as it was at the fork, into dump.rdb, while the parent keeps serving clients; a crash loses every write since that fork.

The problem it solves

Data only survives a kill if it’s already on disk. The simplest way to put it there is to stop, write everything, and carry on: the SAVE command. On a Redis holding 4,445,566 keys, a PING sent half a second into a SAVE:

docker exec $N redis-cli PING  0.03s user 0.02s system 2% cpu 1.993 total
docker exec $N redis-cli SAVE  0.03s user 0.01s system 1% cpu 2.500 total

The PING, 0.121 s on its own (mostly docker exec), took 1.993 s: Redis runs commands one at a time, so every client waits for the whole save. A snapshot has to happen beside the work, not in front of it.

How it works: fork, write, rename

BGSAVE (background save) does it in three steps:

  1. fork. Redis calls fork(), which creates a child process that is a copy of Redis. The copy is cheap because the kernel doesn’t copy the memory: parent and child share the same pages (4,096-byte blocks of memory).
  2. write. The child walks the dataset and writes it, compressed, into temp-<pid>.rdb. The parent goes on answering clients.
  3. rename. When the file is complete, the child renames it to dump.rdb (replacing the old one in one step) and exits. A crash in the middle leaves the previous dump.rdb untouched.

The log shows all three, here for a million keys:

1:M 26 Nov 2026 07:14:58.146 * Background saving started by pid 73
73:C 26 Nov 2026 07:14:58.555 * BGSAVE done, 1000000 keys saved, 0 keys skipped, 35777608 bytes written.
73:C 26 Nov 2026 07:14:58.560 * DB saved on disk
73:C 26 Nov 2026 07:14:58.560 * Fork CoW for RDB: current 0 MB, peak 0 MB, average 0 MB
1:M 26 Nov 2026 07:14:58.655 * Background saving terminated with success

1:M is the parent (pid 1, the main process), 73:C the child. 152 MB of memory became a 36 MB file in about 0.41 s.

What if a client writes during the save? The kernel uses copy-on-write (CoW): before the parent changes a shared page, the kernel copies it, so the child keeps seeing the old one. The child’s view stays frozen at the fork, which is what a snapshot should be. Fork CoW counts the copies: 0 MB here, as nobody was writing.

Save rules: when Redis snapshots by itself

# in redis.conf: save <seconds> <changes> [<seconds> <changes> ...]
save 3600 1 300 100 60 10000    # the default (lesson 1)
save 10 1                       # every 10 s if anything changed (Practical 0)
save ""                         # no automatic snapshots

When a rule’s time has passed and its count of changes is reached, Redis starts a BGSAVE and logs which rule fired with the rule’s own numbers (not how many keys changed): 1 changes in 10 seconds. Saving.... redis-cli BGSAVE starts one by hand.

What a crash loses, before it happens

The count of changes since the last snapshot is in INFO persistence, and it’s the number a kill would lose right now. Here is a writer doing about 100 numbered writes a second against save 10 1, then the INFO line and a kill:

rdb_changes_since_last_save:462
rdb_last_bgsave_status:ok
...
last acknowledged write: 2539
...
2072

The writer’s last acknowledged write was 2539; after the restart the counter read 2072: 467 lost, against 462 predicted. The 5 extra are the writes acknowledged between reading INFO and the kill.

The fields worth knowing:

FieldMeans
rdb_changes_since_last_savewrites not in any snapshot: what a crash loses now
rdb_last_bgsave_statusok or err: did the last save work?
rdb_last_cow_sizebytes copied by copy-on-write during the last save
latest_fork_usec (in INFO stats)how long the last fork() blocked Redis, in microseconds

What a snapshot costs

fork() itself pauses Redis: the kernel copies the process’s page tables (the map of its memory), which grows with memory. Copy-on-write costs memory for every page touched during the save. Measured on this course’s machine:

DatasetDuring the saveFork pausePages copied (CoW peak)
1,000,000 keys, 152 MBno writes2.5 ms0 MB
5,000,000 keys, 771 MBno writes6.4 ms1 MB
4,261,469 keys (634 MB just before the writes began)random SETs from redis-benchmark24.6 ms690 MB

Plan for the last row: writes spread over the whole keyspace touched nearly every page, so the save briefly needed about twice the dataset’s memory. Leave room for that.

When a save fails

If a background save fails (full disk, no permission), Redis by default refuses writes with a MISCONF error until a save succeeds again (stop-writes-on-bgsave-error yes): loud on purpose, so persistence can’t stop silently.

Try it

Practical 0 · Your Redis, crashed: kill Redis under your own Go writer, turn on save 10 1, predict the next loss from INFO, and break the snapshot on purpose.

Go deeper: how a save rule fires, and retries

Redis checks the rules in serverCron, its periodic task (10 times a second by default), and only when no child is running. A rule fires when changes >= N and more than the rule’s seconds have passed since the last successful save. After a failed save, it waits at least 5 seconds (CONFIG_BGSAVE_RETRY_DELAY) before trying again, which is why fixing the cause of a MISCONF lets writes back in within seconds, without a restart. (server.c, persistence docs)

DEBUG POPULATE, used for the measurements above, doesn’t count as changes, which is why rdb_changes_since_last_save stayed 0 after loading a million keys with it: counters track client writes.

Common misunderstandings

  • “BGSAVE doesn’t block at all.” The fork does; watch latest_fork_usec.
  • “A snapshot finished at 12:00:10 holds the writes up to 12:00:10.” It holds the dataset as it was at the fork.
  • “Snapshots always need twice the memory.” Only the pages written during the save are copied.

Check yourself

1. save 60 1000. Your app writes 10 keys a second, steadily. Roughly how much can a crash lose? About 1,000. At 10 a second, the 1,000 changes take 100 s, so snapshots come about every 100 s, not 60: the count can delay a save well past its seconds.
2. INFO persistence says rdb_bgsave_in_progress:1 and rdb_changes_since_last_save:300. Redis is killed now. What survives? The previous complete `dump.rdb`. The save in progress was writing a temp file that never got renamed, so it's ignored, and all 300 changes are lost. The counter only drops when a save **finishes**: then Redis subtracts the changes that save held, which is why it counted 830 and then 1 in the animation. ([rdb.c](https://github.com/redis/redis/blob/8.10.3/src/rdb.c), `dirty_before_bgsave`)
3. Your app suddenly gets MISCONF Redis is configured to save RDB snapshots... on every write. Where do you look first? The Redis log, for why the background save failed (`Permission denied`, `No space left on device`, a fork error), and `INFO persistence` for `rdb_last_bgsave_status:err`. Fix that cause; Redis retries the save by itself and accepts writes again once it succeeds.

Next: Practical 0 · Your Redis, crashed, then lesson 3 · The append-only file and fsync.