Redis puts our stored data into the memory, which greatly improves its read and write speed. However, as we all know, the data in the memory cannot be stored for a long time. When the power failure or other abnormalities occur in our server, the data in the memory may be lost. In order to ensure that our data can be preserved as completely as possible in this case, Redis provides two methods of persistence: RDB and AOF. The following will introduce the advantages and disadvantages of these two methods as well as their corresponding advantages and disadvantages.
RDB (Redis DataBase)
The principle of RDB persistence is to periodically persist the data in memory to the hard disk and generate.rDB files. When Redis stops service for some reasons, when we restart Redis, the. RDB files will be read into the memory to recover our data, thus ensuring the security of our data.
Principle of RDB backup
When Redis uses RDB to back up data, a fork is opened for persistence. During persistence, Redis writes the data in memory to a temporary file first. When the data in memory is written, the new memory file replaces the original memory file. This ensures that during the backup process, existing persistent files will not be corrupted due to some accident. During the whole process, the main thread does not carry out any IO operations, and all the persistent operations are carried out by the child threads, thus ensuring the high performance of Redis operations.
Advantages:
- RDB persistence files take up less disk space than AOF
- When the amount of data is large, the data recovery speed is faster than that of AOF
Disadvantages:
- RDB is a periodic backup of memory data, so when Redis stops service, the data after the last backup is lost due to the lack of time to back up. If the integrity of data is very high, it is not suitable for this persistence mode.
- As the child process is created through the fork operation, the data in the memory is cloned, which occupies extra memory resources.
- Although copy-on-write at fork is used, performance can be poor if the amount of data is too large.
AOF (Apend Only File)
AOF persistence is when Redis executes a command, it appends the new write command to the persistent file in the hard disk. When Redis restarts, the command in the persistent file is executed to restore data.
AOF persistence process
Write commands received by Redis are appended to the AOF buffer, and Redis synchronizes the commands in the buffer to the AOF file on disk according to the persistence policy (always, Everysec, no). When an AOF file exceeds the size specified in the rewrite policy or is manually rewritten, the AOF file is rewritten and compressed to retain only the minimum instruction set that can restore data, reducing the disk usage.
Rewrite the principle of compression
When AOF persistent file is too large, Redsi will fork out rewriting a child process used for file operations, to ensure the main thread does not block, the child process in memory when they perform rewriting operation data to a temporary file, and at the same time will receive new write command at the same time to rewrite buffer and AOF buffer, ensure the integrity of their AOF file, And the newly generated AOF file will not lose the new write command during the rewrite process. When the rewriting is completed, the child process will send a signal to the main process, and the main process will append the write command in the rewrite buffer to the new AOF file. After the rewriting, the new AOF file will replace the original AOF file to complete the AOF rewriting.
Advantages:
- Because commands are appended to persistence files, data is more complete than RDB.
- Because each write command is appended, the persistence file is larger than AOF
Disadvantages:
- The commands in the persistence file are executed sequentially to recover data, which is slower than AOF
- Every write command has to be synchronized and there is some performance pressure
- There may be bugs and data cannot be recovered