1. The significance of redis persistence

The significance of Redis persistence lies in failure recovery. If the data is not persisted, then redis will lose all of its data if it encounters a catastrophic failure. If by redis persistence mechanism to data persistence to the hard drive, and then in the file on disk backup regularly to other servers (such as: cloud server) above, so you can ensure even redis met disaster accident, also can use the backup file to reply the data in advance, after the loss of part of the recent data, You don’t lose all your data.

2. Two persistence methods of Redis

Redis persistence is high availability related. Redis has two types of persistence, RDB and AOF.

(1) Introduction of RDB and AOF persistence mechanisms

RDB persistence mechanism, periodic persistence of data in REDis. The AOF mechanism logs each write command and writes it to a log file in appends-only mode. When Redis restarts, the entire data set can be rebuilt by replying to the write command in the AOF log. If we want Redis to be used only as a pure memory cache, then we can disable all RDB and AOF persistence mechanisms. Using either RDB or AOF, redis memory data can be persisted to disk, which can then be backed up elsewhere. If Redis fails, the memory and disk data on the server are lost. You can copy back the previous data from the cloud service and put it in the specified directory. Then restart Redis, redis will automatically restore the data in the memory according to the data in the persistent data file and continue to provide services. If both RDB and AOF persistence mechanisms are used, then when Redis restarts, AOF will be used to rebuild the data, because the data in AOF is more complete.

(2) Advantages of RDB persistence mechanism

①RDB will generate multiple data files, and each data file represents the data of REDis at a certain time. This multiple data file method is very suitable for cold backup. Do cold standby advantage: redis to control the fixed time generated snapshot file things, more convenient; In the worst case, it provides data recovery faster than AOF.

(2) RDB persistence has very little impact on the external read and write services provided by Redis, so that Redis can maintain high performance, because the main redis process will fork a child process, let the child process to perform disk I/O operations for RDB persistence. Reason: RDB writes directly to redis memory, only at certain times, data will be written to memory. AOF writes files directly each time. Although AOF can write files to OS cache quickly, it still takes a certain amount of time, which is slightly slower than RDB.

③ Compared with AOF persistence mechanism, it is faster to restart and restore redis process directly based on RDB data files. Reason: AOF stores instruction logs. When data recovery is performed, all instruction logs need to be played back and executed to recover all data in memory. RDB is a data file that can be loaded directly into memory during recovery.

(3) Disadvantages of RDB persistence

① If you want to lose as little data as possible when Redis fails, RDB is not as good as AOF. Because the RDB is backed up every once in a while, if Redis goes down, all data will be lost during this period. So it’s not a first-priority recovery plan.

② The RDB forks the RDB snapshot file every time. If the RDB snapshot file is very large, the service provided to the client may be suspended for milliseconds. In general, do not allow the RDB interval to be too long, otherwise the RDB file generated each time is too large, which will affect the performance of Redis itself.

(4) Advantages of AOF persistence

①AOF can better protect data from loss. Cause: AOF normally performs a fsync operation every second through a background process. Flush OS cache data to disk, so that if Redis fails, at most a second of data is lost.

②AOF log files are written in appends-only mode, so there is no disk addressing overhead, high write performance, and the file is not easy to damage, even if the file is broken, it is easy to repair.

③ Even if the AOF log file is too large, the background rewrite operation will not affect the client read and write. Reason: Rewrite log compacts the instructions in it to create a minimal log of data recovery. When a new log file is created, the old log file is written as usual. When the log files after the merge are ready, the old and new log files can be exchanged.

④AOF log file commands are recorded in a very readable manner, which is ideal for emergency replies to operations such as delete by mistake. Flushhall flushes the AOF file in flushhall. Flushhall flushes the AOF file in flushhall. Flushhall flushes the AOF file in flushhall and flushes the AOF file in flushhall.

(5) Disadvantages of AOF persistence

① AOF logs are usually larger than RDB files for the same data.

② After AOF is enabled, the QPS supported by RDB is lower than that supported by AOF. Reason: Because AOF is typically configured to fsync log files once per second, of course, fsync performance per second is fine.

(3) AOF is more fragile than RDB, and the recovered data may be inconsistent. The more complex command logging /merge/ playback approach, such as AOF, is more vulnerable and bug-prone than the RDB-based approach of persisting a full data snapshot file at a time. AOF, however, is designed to avoid bugs in the rewrite process, so instead of merging the rewrite log, rewrite it based on the data in memory at the time, which is much more robust.

(4) Data recovery is slow, and it is troublesome to use it for cold backup and regular backup. You may need to write scripts to do it. Not very good for cold preparation.

(6) How to choose RDB and AOF

① Don’t just use RDB, this will cause a lot of data loss.

(2) Do not only use AOF, because it is too slow to use AOF for cold backup recovery and data inconsistency may occur.

Thirdly, RDB and AOF persistence mechanisms are integrated, and AOF is used as the first choice of data recovery to ensure that data is not lost. Use RDB to do different degrees of cold backup, in the case of AOF files are lost or not available, RDB can also be used for fast data recovery.