Explain the persistence mechanism of redis in detail

Many times we need to persist Redis, that is, to write the data stored in memory to the hard disk, most of the reason is to reuse the data later (for example, machine restart), or to prevent system failure and backup .

Redis provides two persistence mechanisms, one is snapshot persistence (RDB) , and the other is append-only file persistence (AOF) . Let us introduce the following two persistence mechanisms respectively.

1. RDB endurance

We can create a snapshot to obtain a copy of the data stored in memory at a certain point in time . After Redis creates a snapshot, we can copy the snapshot to other servers to create a server copy of the same data; or take the snapshot Stay in place for use when the machine restarts.

Snapshot persistence is the persistence method adopted by Redis by default.


2. AOF persistence

Compared with RDB persistence, AOF persistence has higher real-time performance, so most of them use AOF persistence. Redis does not enable AOF persistence by default, and AOF persistence can be enabled by appendonlyparameters:

appendonly yes

After AOF persistence is enabled, when a command that will modify data is executed, Redis will write the command to the aof file. The save location of the AOF file is the same as the location of the RDB file, which can dirbe modified by using parameters. The default file name of aof is appendonly.aof.

Aof has the following three persistence mechanisms:

appendfsync always		# 每执行一次修改数据的命令,就写入aof文件,这种做法会严重影响redis的性能
appendfsync everysec	# 每秒钟同步一次,将多次写操作同步到硬盘中
appendfsync no		    # 让操作系统决定何时同步

In order to take into account both data and performance, it is generally chosen appendfsync everysecto let redis synchronize the AOF file once a second, so that the speed of redis will not be affected; and even if the system fails, users will lose the data generated within one second at most.

1. Redis4.0's optimization of persistence

After redis4.0, it began to support the hybrid persistence of RDB and AOF. It is not enabled by default and can aof-use-rdb-premblebe enabled with parameters.

If the hybrid persistence is turned on, the RDB will be written directly to the beginning of the AOF file when the AOF is rewritten . In this way, the advantages of RDB and AOF can be combined to quickly load while avoiding excessive data loss . However, there are disadvantages to this, that is, the RDB part of the AOF file is in a compressed format instead of the AOF format, which is poor in readability .

2. About AOF rewrite

AOF rewrite will generate a new AOF file, the new AOF file is consistent with the database state saved in the old AOF file, but the file size is smaller. AOF rewriting is a more ambiguous name, because AOF rewriting is done by reading the key-value pairs of the database, and the program does not need to perform any reading, analysis, and writing operations on the original AOF file .

3. AOF rewriting process

When executing a BGETWRITEAOFcommand, the Redis server will maintain an AOF rewrite buffer, which will record all write operations of the server while the child process generates a new AOF file. After the child process completes the generation of the new AOF file, the content of the AOF rewrite buffer will be appended to the end of the new AOF file, so that the status of the database saved by the new and old AOF files is consistent. Then the server overwrites the old AOF file with the new AOF file, thus completing the rewriting operation.

Guess you like

Origin blog.csdn.net/qq_14810195/article/details/108943363