Lesson 9 of 10 Operations

Redis Persistence: RDB vs AOF Explained

How Redis persists data: RDB snapshots vs the append-only file, fsync options, a kill -9 crash test of both, and how to choose for caches and stores.

35-45 min intermediate 0/10 lessons done
Course contents Lesson 9 of 10 · 0 done
On this page (7 sections)
  1. What you will learn
  2. Two mechanisms
  3. Crash test
  4. Choosing
  5. Key takeaways
  6. Exercises
  7. Quiz

What you will learn

  • How RDB snapshots and the AOF log work.
  • What appendfsync controls 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:

appendfsyncData at risk on crashCost
alwaysAlmost nothingSlowest: fsync per write
everysec (default)Up to about 1 secondGood balance
noWhatever 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 caseSuggested setup
Pure cache (data can be rebuilt)Persistence off, or RDB for faster warm restarts
Sessions, rate limits, queuesAOF everysec (optionally with RDB for backups)
Must not lose acknowledged writesAOF always and replicas with WAIT, or a database designed for durability

Operational notes:

  • fork() for BGSAVE and AOF rewrites uses copy-on-write; with heavy writes, memory can grow noticeably during a save. Leave headroom.
  • Copy dump.rdb off the machine for backups; it’s a consistent point-in-time file.
  • redis-check-aof --fix repairs a truncated AOF after a crash.

Key takeaways

  • RDB: periodic compact snapshots, fast restarts, loses writes since the last snapshot.
  • AOF: logs every write; everysec risks about a second of writes, always almost 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

  1. Rerun the AOF test with appendfsync no and crash immediately after writing. How many keys survive?
  2. Measure redis-benchmark -t set -n 100000 -q with appendfsync always versus everysec.
  3. Run BGREWRITEAOF after deleting most keys and compare the AOF directory size before and after.

Quiz

  1. What does RDB lose on a crash?
  2. What is the default appendfsync setting, and the worst-case loss?
  3. Why can memory use rise during BGSAVE?
Show answers
  1. Every write since the last completed snapshot.
  2. everysec; roughly the last second of writes.
  3. The forked child shares memory pages with the parent via copy-on-write; pages the parent modifies during the save get duplicated.

Finished this lesson?

Mark it complete to track your progress (saved in this browser).