3. Persistence: RDB, AOF & Durability Strategies
Redis Persistence: Keeping Data Safe
Redis keeps data in memory for speed, but this means data is lost on restart unless you enable persistence. Redis supports two persistence mechanisms: RDB (snapshots) and AOF (Append-Only File). Understanding persistence is essential for production Redis deployments where data durability matters.
RDB creates point-in-time snapshots of your data. It's efficient for backups and disaster recovery. RDB files are compact and compressed, making them ideal for backup storage. Redis forks a child process to create the snapshot, which doesn't block the main process. This makes RDB efficient and non-blocking. AOF logs every write operation, providing better durability. You can configure AOF with different fsync policies to balance durability and performance.
You can use RDB, AOF, or both together. RDB is better for backups and disaster recovery. AOF is better for durability and point-in-time recovery. Using both provides the best protection. Choose your persistence strategy based on your durability requirements and performance needs. This module covers both mechanisms in depth with configuration examples and best practices.
RDB Snapshots
RDB snapshots create point-in-time copies of your data. They're efficient for backups and disaster recovery. RDB files are compact and compressed, typically much smaller than the dataset size in memory. Redis forks a child process to create the snapshot, which doesn't block the main process. This makes RDB efficient and non-blocking.
RDB snapshots can be configured with save points based on time and change count. For example, save 900 1 saves if at least 1 key changed in 900 seconds. RDB is the default persistence mechanism. Use RDB for backups and disaster recovery. RDB is also useful for replication — replicas initially synchronize using an RDB file.
However, RDB has limitations. If Redis crashes between snapshots, you lose any data changed since the last snapshot. For workloads that need minimal data loss, consider AOF or both mechanisms. RDB snapshots can also cause performance spikes during the fork, especially with large datasets. Monitor memory usage and configure save points carefully.
# RDB configuration in redis.conf save 900 1 # Save if at least 1 key changed in 900 seconds save 300 10 # Save if at least 10 keys changed in 300 seconds save 60 10000 # Save if at least 10000 keys changed in 60 seconds dbfilename dump.rdb # RDB file name dir ./ # Directory for RDB file rdbcompression yes # Compress RDB file rdbchecksum yes # Add checksum for integrity # Manual save SAVE # Blocking save (not recommended for production) BGSAVE # Background save (recommended) # Check last save time LASTSAVE # Monitor RDB status INFO persistence
AOF (Append-Only File)
AOF logs every write operation, providing better durability than RDB. AOF can be configured for different fsync policies: always (every write), everysec (every second), or no (OS default). The everysec policy is recommended for most use cases, balancing durability and performance.
AOF files grow over time, but Redis can rewrite them. The AOF rewrite compacts the log by removing redundant commands and rewriting the file to be more efficient. This reduces file size and improves recovery time. AOF rewriting can be configured to happen automatically based on file growth. You can also trigger it manually.
AOF provides better durability than RDB because you lose at most one second of data with the everysec policy. However, AOF files are larger than RDB files and can be slower for recovery. Use AOF for workloads where data loss is unacceptable. Use both RDB and AOF for maximum durability.
# AOF configuration in redis.conf appendonly yes # Enable AOF appendfilename "appendonly.aof" # AOF file name # Fsync policies (choose one) appendfsync always # Fsync on every write (safest, slowest) appendfsync everysec # Fsync every second (recommended) appendfsync no # OS default (fastest, less safe) # AOF rewrite configuration auto-aof-rewrite-percentage 100 # Rewrite when file grows 100% auto-aof-rewrite-min-size 64mb # Minimum size for rewrite no-appendfsync-on-rewrite no # Don't fsync during rewrite # Manual AOF rewrite BGREWRITEAOF # AOF recovery (if corrupted) redis-check-aof --fix appendonly.aof
Choosing a Persistence Strategy
Choose your persistence strategy based on your durability requirements and performance needs. For caching-only use, you might not need persistence. For critical data, use both RDB and AOF. RDB provides efficient backups. AOF provides durability. Test your recovery process. Monitor persistence performance.
Consider your RPO (Recovery Point Objective) and RTO (Recovery Time Objective). RPO determines how much data you can lose. AOF with everysec gives you at most 1 second of data loss. RDB with a save every 60 seconds gives you at most 60 seconds of data loss. RTO determines how fast you can recover. RDB recovery is typically faster than AOF recovery because RDB files are smaller and more compact.
For production Redis deployments, use both RDB and AOF. RDB provides fast recovery and backups. AOF provides durability. Configure RDB saves for regular backups. Configure AOF with the everysec policy for durability. Test your recovery process regularly. Monitor persistence performance with the INFO command.
- No Persistence: For caching-only use cases where data loss is acceptable (lowest overhead).
- RDB Only: For backups and disaster recovery with acceptable data loss (e.g., 1-minute RPO).
- AOF Only: For better durability with acceptable performance (e.g., 1-second RPO).
- Both RDB and AOF: For maximum durability and recovery options (recommended for production).
Knowledge Check
Ready to test your understanding of 3. Persistence: RDB, AOF & Durability Strategies?