Redis Persistence
RDB snapshots vs AOF and hybrid persistence
Skills taught: redis.persistence
Why Persistence Matters
Redis stores everything in RAM. Without persistence, a server restart, power failure, or crash means total data loss. Redis provides two persistence mechanisms with different tradeoffs between durability and performance.
RDB — Periodic Snapshots
RDB creates a point-in-time snapshot of all data. Redis forks a child process to write the snapshot — the parent keeps serving requests with minimal impact.
# redis.conf — RDB configuration
# Save rules: save <seconds> <changes>
# Save if at least 1 change happened in 3600 seconds
save 3600 1
# Save if at least 100 changes happened in 300 seconds
save 300 100
# Save if at least 10000 changes happened in 60 seconds
save 60 10000
# RDB filename and directory
dbfilename dump.rdb
dir /var/lib/redis
# Compress RDB (uses LZF compression)
rdbcompression yes
# Manual save (blocks server — don't use in production)
# redis-cli SAVE
# Non-blocking save
# redis-cli BGSAVE
# Check last save time
# redis-cli LASTSAVE
- RDB
- Redis Database — a compact binary snapshot of all data at a point in time. Fast to load on restart but may lose up to minutes of writes if Redis crashes between saves.
AOF — Append-Only File
AOF logs every write command. On restart, Redis replays all commands to reconstruct the dataset. It provides much better durability — typically less than 1 second of data loss.
# redis.conf — AOF configuration
appendonly yes
appendfilename "appendonly.aof"
# Fsync policy (how often to flush to disk)
# always — fsync after every write (safest, slowest)
# everysec — fsync once per second (recommended default)
# no — let OS decide (fastest, least safe)
appendfsync everysec
# Rewrite AOF to compact it (removes historical commands)
auto-aof-rewrite-percentage 100 # rewrite when AOF doubles in size
auto-aof-rewrite-min-size 64mb # only if AOF is at least 64MB
# Trigger manual AOF rewrite (non-blocking)
# redis-cli BGREWRITEAOF
- AOF
- Append-Only File — a log of every write command in RESP format. Near-zero data loss with everysec fsync (up to 1 second loss), but slower startup and larger files than RDB.
Hybrid Persistence (Recommended)
Hybrid persistence combines the fast startup of RDB with the low data loss of AOF. At AOF rewrite time, Redis writes an RDB preamble, then appends subsequent commands. On restart it loads the RDB preamble (fast) then replays only the new AOF tail (small).
# redis.conf — Recommended production setup
appendonly yes
appendfsync everysec
aof-use-rdb-preamble yes # hybrid mode: RDB preamble + AOF tail
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
# Save RDB as backup too
save 3600 1
save 300 100
RDB vs AOF
- RDB: Periodic point-in-time snapshots
- AOF: Every-command write log
Tip: For most production Redis deployments, enable hybrid persistence: appendonly yes + aof-use-rdb-preamble yes + appendfsync everysec. This gives fast startup and at most 1 second of data loss.
Warning: RDB fork() can temporarily double memory usage under a heavy write load due to copy-on-write. Set maxmemory to no more than 50% of RAM if RDB saves are frequent.
Frequently Asked Questions
Can I use both RDB and AOF?
Yes. The recommended setup is hybrid persistence: AOF with an RDB preamble (aof-use-rdb-preamble yes). This gives fast startup from the RDB portion and minimal data loss from the AOF tail.
How do I disable persistence for a pure cache?
Set 'save ""' (empty string) in redis.conf to disable RDB, and appendonly no to disable AOF. The instance becomes volatile — all data is lost on restart.
How large will the AOF file get?
It grows with every write until a rewrite compacts it. Redis triggers auto-rewrite when the AOF doubles in size (auto-aof-rewrite-percentage 100). Force a rewrite with BGREWRITEAOF.