How do RDB and AOF persistence mechanisms work in Redis?
Redis offers two persistence options to protect in-memory data against power outages:
• RDB (Redis Database Snapshot): Creates point-in-time compact snapshots of the entire dataset at specified intervals (e.g. every 5 minutes). Uses copy-on-write `fork()` to avoid blocking the main server. Very fast restart times, but risks losing changes made since the last snapshot. • AOF (Append-Only File): Logs every write operation received by the server to a disk journal. Can be synced to disk every second (`fsync everysec`). Minimal data loss (at most 1 second), but produces larger disk files.
Production deployments usually enable both simultaneously.
# Redis configuration (redis.conf)
# Snapshot rules: save after 60 sec if at least 1000 keys changed
save 60 1000
# Enable Append Only File
appendonly yes
appendfsync everysec # High performance with max 1 sec potential data loss
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb`appendfsync everysec` provides a great balance: performance is nearly as fast as pure RAM, while worst-case disaster recovery loses at most one second of transactions.
Ready for Expiration & TTL?
Take a quick test to evaluate yourself, or move straight to the next module.