Redis persistence mechanism RDB (Redis DataBase) and AOF (Append-Only File) learning summary

1. RDB (Redis DataBase) snapshot

Back up all the data in the current redis at a certain point in time or at regular intervals.

1.1 Trigger method:

save command, bgsave command, automatic configuration

save command :
block the current redis server. During the command execution, Redis cannot process other commands. If it is still in use, use it with caution!

bgsave command :
The main process forks a sub-process to complete the RDB process, and automatically terminates the sub-process after completion, with extremely short blocking time.

Automatic configuration :
trigger as a configuration item, for example, configure the first line in redis.conf
to turn off AOF mode first, so that only RDB is turned on

appendonly no

save 600 10

save 600 10 can be translated as if 10 or more keys are modified within 600 seconds, then the backup task will be triggered.
save can write more than one, for example

save 600 10
save 60 100

When writing more than one, it is an or relationship, and any one that is satisfied will trigger

1.2 Execution process

1. The main process first checks whether there is an RDB or AOF persistent task being executed, and if so, it will end without doing anything.

2. The main process will fork a sub-process to perform RDB backup tasks. The fork operation will temporarily block the main process (Redis read and write operations). After the sub-process operation is completed, it will send a message to the main process, and the main process will resume reading and writing capabilities.

3. The child process generates a temporary snapshot file based on the current memory data of the main process. After the persistence is completed, the temporary snapshot file will be used to replace the original RDB file.
(The reading and writing of the main process will not be affected during this process, but the write operation of Redis will not be synchronized to the main memory of the main process, but will be written to a temporary memory area as a copy)

4. After the child process completes the RDB persistence, it will send a message to the main process to notify the RDB persistence completion (the data generated by the incremental write operation in the memory copy in the previous stage will be synchronized to the main memory)

1.3 Advantages and disadvantages

Advantages :
1. RDB uses a compact binary format to store data (RDB format files), so the speed is very fast. It is usually much faster than AOF when recovering large amounts of data.

2. RDB is very useful for data backup and recovery on disk, and the sub-process operation data has little impact on the main process.

Disadvantages :
RDB snapshots are performed periodically but not in real time, so the real-time performance of data cannot be guaranteed (it takes time to read and write disk files, etc.). At the same time, if redis crashes during snapshot generation or replacement, the latest data will be lost.

2. AOF(Append-Only File)

It is an incremental record of all write operations to the aof log file, and restores all data by re-executing all instructions in the log file.

Updating the backup data dynamically and in real time can compare the complete backup of all data, but too frequent operations will have a greater impact on redis performance.

2.1 Trigger method:

How to enable configuration
Configure AOF by modifying redis.conf

Turn on AOF persistence, just remove the comment

appendonly yes

Persistence strategy:
set the persistence strategy (always, everysec, no)
for example

appendfsync everysec

always : synchronous persistence, update as long as there is a change, although it is guaranteed that the data can be backed up, but a large amount of data has a greater impact on performance
everysec : asynchronous persistence, update data every second, and lose at most one second of data
no : The persistence frequency is determined by the operating system. Compared with the other two methods, this method has the best performance, but when the interval is too long, a lot of data will be lost, and the amount of loss is hard to estimate and uncontrollable.

2.2 Advantages and disadvantages

Advantages :
1. With incremental record writing operations, almost all data can be recorded, and little or no data is lost.
2. The writing speed is fast, and there is no need for disk addressing
. 3. When the AOF log file is too large, the instructions will be compressed to generate a smaller log file that can restore all data, and will not affect the main process's reading and writing.

Disadvantages :
1. Record every write operation, which is more frequent than RDB operation, slower, and has a greater impact on performance.
2. The write command used to restore data is more error-prone than RDB. The AOF file of the same data will be larger and needs to be compressed and cleaned regularly.

2.3 AOF rewriting process

1. When the AOF file reaches a certain condition, it will trigger rewriting, which can be configured and modified

If the current AOF file is 100% larger than the file when the last rewrite was performed, the rewrite operation will be triggered again.
However, if the value of this parameter is 0, the rewriting operation will not be triggered.

auto-aof-rewrite-percentage 100

Specifies the AOF file size when rewriting is triggered

auto-aof-rewrite-min-size 64mb

That is, the default configuration is triggered when the size of the AOF file is twice the size after the last rewrite and the file is larger than 64MB

2. When rewriting, the main process will create a sub-process to generate a new AOF log file, which will retain the minimum instruction set that can restore data.

3. During the creation of a new AOF log file, redis will create an additional buffer to store all the write operations during the creation of the file.

4. Then read the key-value pairs in redis to generate a minimal file that can restore data. After the new file is generated, add the write command of the above cache area, and finally replace the old AOF file.

3. Hybrid Persistence

Redis 4.0 adds
literal meaning, enabling both persistence methods.

Enabled by configuration

aof‐use‐rdb‐preamble yes

For example, when restoring, first restore the data left by the last snapshot, and then restore all write operation instructions recorded after the snapshot.

3.1 Hybrid persistence process

1. The data in the memory before the moment of AOF rewriting is processed as an RDB snapshot.
2. Then the data in the RDB and the incremental write operation instructions are written together into a new AOF file. After the writing is complete, the original old file will be overwritten.

When restarting, you can first load RDB to quickly restore a large amount of data, and then use AOF to restore incremental write instructions, so that the performance and restart speed can be improved while keeping the data as complete as possible.

The reference materials are as follows:
https://blog.csdn.net/weixin_37672801/article/details/127476772

https://new-developer.aliyun.com/article/1267270

Guess you like

Origin blog.csdn.net/weixin_44131922/article/details/131721620