Redis provides two persistence methods: RDB (Redis Database) and AOF (Append Only File). Each has its advantages and disadvantages, and they can also be used together.
RDB Persistence
Working Principle: RDB generates point-in-time snapshots of the dataset at specified time intervals. Redis forks a child process that writes the in-memory data to a temporary file, then replaces the old RDB file with this temporary file.
Advantages:
- Compact File: RDB files are compressed binary files, small in size, suitable for backup and disaster recovery
- Fast Recovery: RDB file recovery is faster than AOF because it doesn't need to re-execute commands
- Low Performance Impact: RDB is executed by a child process, with minimal impact on the main process
- Suitable for Cold Backup: RDB files can be easily transferred to remote servers for backup
Disadvantages:
- Data Loss Risk: If Redis crashes suddenly, all data after the last snapshot may be lost
- Fork Operation Time-Consuming: When data volume is large, forking a child process may block the main process
- Poor Real-time: RDB is based on time intervals and cannot achieve real-time persistence
Configuration Parameters:
save <seconds> <changes>: Set save conditions, e.g.,save 900 1means save if at least 1 key changes within 900 secondsrdbcompression yes: Whether to compress RDB filesrdbchecksum yes: Whether to checksum RDB files
AOF Persistence
Working Principle: AOF records every write operation command received by the server, appending these commands to the end of the AOF file. When Redis restarts, it re-executes the commands in the AOF file to restore data.
Advantages:
- High Data Security: AOF can be configured to sync every second or on every write operation, with low data loss risk
- High Readability: AOF files are in text format and can be manually viewed and modified
- Automatic Rewrite: When AOF files become too large, they are automatically rewritten to compress file size
Disadvantages:
- Large File Size: AOF files are typically larger than RDB files
- Slow Recovery: Need to re-execute all commands, recovery speed is slower than RDB
- High Performance Impact: Every write operation needs to sync to disk, with significant performance impact
Configuration Parameters:
appendonly yes: Enable AOF persistenceappendfsync always/everysec/no: Sync strategyalways: Sync on every write operation, safest but worst performanceeverysec: Sync once per second, recommended configurationno: Let the OS decide when to sync, best performance but least secure
auto-aof-rewrite-percentage 100: AOF file rewrite growth percentageauto-aof-rewrite-min-size 64mb: AOF file rewrite minimum size
RDB + AOF Hybrid Persistence
After Redis 4.0, hybrid persistence is supported. When enabled, AOF rewrite writes RDB content to the beginning of the AOF file, and subsequent incremental commands continue to be appended in AOF format. This ensures both data security and improves recovery speed.
Configuration:
aof-use-rdb-preamble yes: Enable hybrid persistence
Selection Recommendations
- If data security requirements are low: Use only RDB for better performance
- If data security requirements are high: Use only AOF, configured with
appendfsync everysec - If both performance and security are needed: Use RDB + AOF hybrid persistence
- If data volume is large: Recommend using RDB, as AOF recovery speed is too slow
In actual production environments, typically both RDB and AOF are enabled, or hybrid persistence is used, to balance performance and data security.