Turn on both Redis persistence modes, RDB snapshots and the AOF log, whenever Redis holds data you cannot rebuild. Persistence is how Redis saves its in-memory data to disk so it survives a restart or a crash. An RDB snapshot is a compact copy of the whole dataset written every few minutes, so it loads fast but loses whatever changed since the last one. The AOF (append-only file) records every write as it happens, so a crash loses at most about one second of writes with the default setting, but replaying it on restart is slower. Running both gives you the AOF’s durability with a fast restart, and if Redis is only a cache you can refill, snapshots alone are enough.
How to verify
# Current persistence settings
redis-cli CONFIG GET save
redis-cli CONFIG GET appendonly
redis-cli CONFIG GET appendfsync
redis-cli CONFIG GET dir
# Last successful persistence
redis-cli INFO persistence | grep -E 'rdb_last|aof|aof_last|aof_current_size|loading'
# Files on disk
sudo ls -lh /var/lib/redis/
What’s happening
RDB is a binary dump of the entire dataset taken at points in time. The default save policy is “if at least N keys changed in the last M seconds, write a snapshot”. Snapshots are produced via BGSAVE, which forks the Redis process so the parent serves clients while the child writes the dump to disk. The resulting file is compact (around the size of the dataset), loads quickly on restart, and is easy to ship as a backup. The catch: between snapshots, anything written is lost on a crash.
AOF is an append-only log of every write command. With appendfsync everysec (the default), the OS flushes the log to disk once per second — meaning at most one second of writes is lost on a crash. With appendfsync always, every write is flushed before acknowledgment — full durability at the cost of latency. With appendfsync no, the OS decides — fastest but loses data on crash.
The AOF file grows continuously, so Redis periodically rewrites it: scan the live dataset, write the smallest set of commands that reproduces it, replace the old file. This is BGREWRITEAOF, which (like BGSAVE) forks. After rewrite, the AOF is compact again.
The combined mode that most production Redis uses: appendonly yes plus the default save snapshot triggers. With aof-use-rdb-preamble yes (default on modern Redis), the rewritten AOF file starts with an RDB snapshot section then appends incremental commands. Loading on restart reads the RDB section (fast) and replays the trailing commands (durability up to last fsync). Best of both.
The decision
| Concern | RDB | AOF | Combined |
|---|---|---|---|
| Max data loss on crash | Up to save interval (minutes) | 1 second (everysec) | 1 second |
| Recovery time | Fastest (binary load) | Slower (replay commands) | Fast (RDB preamble + tail) |
| File size | Compact | Larger (grows then rewrites) | Compact after rewrite |
| Write amplification | None during snapshots | Every write goes to disk | Every write goes to disk |
| Fork cost | Yes during BGSAVE | Yes during BGREWRITEAOF | Yes for both |
| Backup ease | Excellent (single file) | Less convenient | Excellent (RDB preamble) |
Use RDB only when:
- The data is reconstructable (cache) and minutes of loss are acceptable.
- You need the smallest fork-time memory pressure (only one fork per snapshot).
Use AOF only when:
- You want sub-second durability and explicitly do not want snapshot files.
- The dataset is small enough that AOF replay on restart is fast.
Use both (recommended for primary stores) when:
- Sub-second durability matters AND fast restart matters.
- You want backups that are easy to ship and restore.
The procedure
-
Enable AOF and snapshots in
/etc/redis/redis.conf:# AOF appendonly yes appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-use-rdb-preamble yes # RDB save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb -
Provision a dedicated disk for
dir. Persistence on the same disk as the OS or as application logs creates fsync contention:sudo mkfs.xfs /dev/vdb sudo mkdir -p /var/lib/redis sudo mount /dev/vdb /var/lib/redis sudo chown redis:redis /var/lib/redis echo "/dev/vdb /var/lib/redis xfs defaults 0 0" | sudo tee -a /etc/fstab -
Restart and verify:
sudo systemctl restart redis-server redis-cli INFO persistence ls -lh /var/lib/redis/ -
Test a restart recovery by writing data, then restarting:
redis-cli SET test1 "value1" redis-cli BGSAVE redis-cli LASTSAVE sudo systemctl restart redis-server redis-cli GET test1 # Expected: "value1" -
Trigger a manual AOF rewrite during off-peak to keep the file compact:
redis-cli BGREWRITEAOF -
Ship snapshots off-host. A copy of
dump.rdbto S3 every hour is cheap insurance:cp /var/lib/redis/dump.rdb /backup/redis-$(date +%FT%H).rdb aws s3 cp /backup/redis-$(date +%FT%H).rdb s3://backups/redis/ -
Restore. Stop the daemon, copy the desired RDB or AOF into
dir, start the daemon. The file Redis loads first is the AOF ifappendonly yes; otherwise the RDB.
Common pitfalls
appendfsync alwayslooks safer but adds millisecond fsync latency to every write — on a busy master this kills throughput.everysecis the right choice for almost every workload.stop-writes-on-bgsave-error yes(default) is correct — it pauses writes if BGSAVE fails, alerting you to disk issues before data is silently lost. Do not disable.- AOF replay on restart is single-threaded and proportional to AOF size. A 20 GB AOF takes minutes to load — for fast recovery, rewrite often (lower
auto-aof-rewrite-percentage). - A fork during peak load can take seconds on a CPU-bound host, during which Redis appears to hang. Provision CPU headroom or schedule heavy persistence operations.
- Persistence to the same disk as the OS competes for IOPS with logging and syslogd. Dedicate a disk.
In the engagements we run, every primary-store Redis comes up with appendonly yes, aof-use-rdb-preamble yes, a dedicated data volume, and dump.rdb shipped to S3 hourly. We operate that under Managed Operations — and for cache-only deployments where RDB-only suffices, the lighter config is documented in the same runbook.