Comparison of RDB and AOF
|Data security||Loss of data for several times||Decision based on Strategy|
Persistence mode selection
If the data security requirements are very high, two persistence methods should be used at the same time.
If you can withstand data loss for a certain period of time, you can only use RDB persistence.
It is not recommended to use AOF persistence only, because generating RDB snapshot regularly is very convenient for database backup, and RDB can recover datasets faster than AOF.
Recovery priority of RDB and AOF
If redis uses both RDB and AOF for persistence, redis will give priority to AOF for data recovery.
When starting redis, if it already exists appendonly.aof File, based on appendonly.aof File recovery data; if it does not exist appendonly.aof File, an empty appendonly.aof File and based on this empty appendonly.aof File start.
Due to the above characteristics, special care should be taken when using RDB to recover data and switching to RDB + AOF persistence to avoid data loss.
The correct approach should be as follows:
- Stop redis, turn off AOF persistence and keep RDB persistence to prevent it from being generated at startup appendonly.aof Documents;
- Copy the RDB file to the data directory and start redis. After starting, redis will use the RDB file to recover the data;
- Confirm data recovery and enable AOF persistence by hot modifying the configuration on the command line
config set appendonly yes；
- Wait for redis to write data in memory appendonly.aof At this time, RDB and AOF data are synchronized;
- Stop redis, modify the configuration file, and enable AOF persistence and RDB persistence;
- After redis is started, data recovery and persistence configuration are completed.