RDB Snapshots
Point-in-time snapshots, fork and copy-on-write, and what they can lose
Redis keeps everything in memory, so without persistence a restart
empties it. Redis has two persistence mechanisms, and this repo runs both.
The first, RDB, writes a compact binary snapshot of the whole
dataset to dump.rdb from time to time.
When snapshots happen
save 3600 1 300 100 60 10000
# snapshot if >= 1 change in 3600s, OR >= 100 changes in 300s, OR >= 10000 changes in 60sAlso on demand, and on a clean shutdown:
How BGSAVE works: fork and copy-on-write
fork() creates a child process that sees the dataset exactly as it was
at that instant. The OS doesn't copy the memory. Parent and child share
the same physical pages, marked copy-on-write: only when the parent
modifies a page does the kernel copy it, so the child keeps its frozen
view. The child serialises its view to a temp file, fsyncs it, and
renames it over dump.rdb, so a crash mid-save never corrupts the
previous snapshot.
INFO persistence | grep cow
rdb_last_cow_size:1282048 # bytes copied because the parent wrote during the last saveCopy-on-write is why Redis needs memory headroom. On a write-heavy
instance, most pages get touched during a long save, and memory use can
approach double the dataset. If the container or host can't
provide it, the save fails or the kernel's OOM killer takes out Redis.
It's also why fork() itself can take tens of milliseconds on a
large instance (copying page tables), a latency spike that shows up as
latest_fork_usec in INFO stats.
What RDB can lose
Everything written since the last successful snapshot. With the rules above and low traffic, that can be up to an hour of writes. RDB on its own is a good fit for:
- caches, where losing recent data only costs some misses
- backups: one compact file, easy to copy and restore
- fast restarts: loading an RDB file is much faster than replaying a log
It's a bad fit, on its own, for anything where losing minutes of writes matters. That's what AOF is for.
The file
docker exec redis ls -la /data
-rw------- 1 redis redis 9103 Oct 12 08:05 dump.rdb
# check a file's integrity
docker exec redis redis-check-rdb /data/dump.rdbRDB files are compressed (LZF) and versioned. Newer Redis versions read older RDB files, but not the other way round, which matters when you restore a backup onto an older server.
If a background save fails (disk full, permissions, fork failure), Redis
by default refuses writes (stop-writes-on-bgsave-error yes) so you
notice, with errors like MISCONF Redis is configured to save RDB snapshots, but it's currently unable to persist to disk. Monitor
rdb_last_bgsave_status. The Prometheus stack's RedisPersistenceFailing
alert does.
Lab
INFO persistenceand noterdb_changes_since_last_save.BGSAVE, thenLASTSAVEandls -la /data.- Check
rdb_last_cow_sizeandlatest_fork_usec(inINFO stats). - Restart Redis (
docker restart redis) and watch the log:DB loaded from append only file(AOF takes priority when enabled) and how long loading took.