What you will learn
- How RDB snapshots and the AOF log work.
- What
appendfsynccontrols and what you can lose. - A real crash test of both modes, and how to choose.
Two mechanisms
RDB (snapshots): Redis forks a child process that writes the whole dataset to a compact dump.rdb file. Configured with save <seconds> <changes> rules or triggered with BGSAVE. Fast restarts and easy backups, but anything written after the last snapshot is lost on a crash.
AOF (append-only file): every write command is appended to a log and replayed on restart. The appendfsync setting controls how often the log is forced to disk:
appendfsync | Data at risk on crash | Cost |
|---|---|---|
always | Almost nothing | Slowest: fsync per write |
everysec (default) | Up to about 1 second | Good balance |
no | Whatever the OS hasn’t flushed (often up to ~30 s) | Fastest |
The AOF is periodically rewritten (compacted) in the background. Since Redis 7, AOF is stored in a directory as a base file (RDB format) plus incremental files, tracked by a manifest.
Crash test
This script writes data, kills the server with kill -9 (no chance to save), restarts and checks what survived:
#!/usr/bin/env bash
# Crash-test RDB vs AOF: write keys, kill -9 the server, restart, count what survived.
set -euo pipefail
PORT=${PORT:-6392}
DIR=$(mktemp -d)
start() { redis-server --port "$PORT" --dir "$DIR" --daemonize yes --logfile "$DIR/log" "$@"; sleep 0.5; }
crash() { kill -9 "$(redis-cli -p "$PORT" INFO server | awk -F: '/process_id/{print $2}' | tr -d '\r')"; sleep 0.5; }
echo "--- RDB only (snapshot after 60 s if at least 1 change) ---"
start --save "60 1" --appendonly no
redis-cli -p "$PORT" SET before_snapshot 1 >/dev/null
redis-cli -p "$PORT" BGSAVE >/dev/null; sleep 1 # explicit snapshot
redis-cli -p "$PORT" SET after_snapshot 1 >/dev/null
crash
start --save "60 1" --appendonly no
echo "before_snapshot: $(redis-cli -p "$PORT" EXISTS before_snapshot), after_snapshot: $(redis-cli -p "$PORT" EXISTS after_snapshot)"
redis-cli -p "$PORT" SHUTDOWN NOSAVE >/dev/null 2>&1 || true; sleep 0.5
rm -rf "${DIR:?}"/*
echo "--- AOF with appendfsync everysec ---"
start --save "" --appendonly yes --appendfsync everysec
for i in $(seq 1 1000); do echo "SET k$i v$i"; done | redis-cli -p "$PORT" >/dev/null
sleep 1.5 # let the once-per-second fsync happen
crash
start --save "" --appendonly yes --appendfsync everysec
echo "keys after crash: $(redis-cli -p "$PORT" DBSIZE)"
ls "$DIR/appendonlydir"
redis-cli -p "$PORT" SHUTDOWN NOSAVE >/dev/null 2>&1 || true
rm -rf "$DIR"
Output:
--- RDB only (snapshot after 60 s if at least 1 change) ---
before_snapshot: 1, after_snapshot: 0
--- AOF with appendfsync everysec ---
keys after crash: 1000
appendonly.aof.1.base.rdb
appendonly.aof.1.incr.aof
appendonly.aof.manifest
- With RDB only, the key written after the last snapshot was lost.
- With AOF
everysec, all 1,000 keys survived because the script waited past the one-second fsync. A crash within that second could lose the most recent writes. - The listing shows the Redis 7+ multi-part AOF layout.
Choosing
| Use case | Suggested setup |
|---|---|
| Pure cache (data can be rebuilt) | Persistence off, or RDB for faster warm restarts |
| Sessions, rate limits, queues | AOF everysec (optionally with RDB for backups) |
| Must not lose acknowledged writes | AOF always and replicas with WAIT, or a database designed for durability |
Operational notes:
fork()forBGSAVEand AOF rewrites uses copy-on-write; with heavy writes, memory can grow noticeably during a save. Leave headroom.- Copy
dump.rdboff the machine for backups; it’s a consistent point-in-time file. redis-check-aof --fixrepairs a truncated AOF after a crash.
Key takeaways
- RDB: periodic compact snapshots, fast restarts, loses writes since the last snapshot.
- AOF: logs every write;
everysecrisks about a second of writes,alwaysalmost none at a throughput cost. - Combine them for fast restarts plus durability, or disable both for a pure cache.
- Plan memory headroom for fork-based saves.
Exercises
- Rerun the AOF test with
appendfsync noand crash immediately after writing. How many keys survive? - Measure
redis-benchmark -t set -n 100000 -qwithappendfsync alwaysversuseverysec. - Run
BGREWRITEAOFafter deleting most keys and compare the AOF directory size before and after.
Quiz
- What does RDB lose on a crash?
- What is the default
appendfsyncsetting, and the worst-case loss? - Why can memory use rise during
BGSAVE?
Show answers
- Every write since the last completed snapshot.
everysec; roughly the last second of writes.- The forked child shares memory pages with the parent via copy-on-write; pages the parent modifies during the save get duplicated.