AOF study notes for Redis persistence

AOF(Append Of File)

1 concept

​ Record each write operation in the form of a log (incremental saving), record all instructions executed by Redis (read operations are not recorded), only need to append files but not rewrite files, redis will read at the beginning of startup This file reconstructs the data, that is, executes the instructions from beginning to end according to the contents of the file

2 AOF persistence process

(1) The client's request write command will be appended to the AOF buffer by append

(2) The AOF buffer synchronizes the operation sync to the AOF file on the disk according to the AOF persistence strategy [always, everysec, no]

(3) When the AOF file size exceeds the rewrite policy or manual rewrite, the AOF file rewrite will be rewritten to compress the AOF file capacity

(4) When the Redis service restarts, it will reload the write operation in the AOF file to achieve the purpose of data recovery

3 AOF configuration

​ AOF is not enabled by default, you can configure the file name in redis.conf, the default is appendonly.aof, the save path of the AOF file is consistent with the path of the RDB

​ The default is no in the configuration file, and the file name defaults to appendonly.aof

image-20211014204703083

Change it to yes to enable AOF

image-20211014204808472

After restarting redis, you can see that the corresponding file is generated

image-20211014204931434

​ When AOF and RDB are enabled at the same time, the system will take AOF data by default

4 exception recovery

​ If the AOF file is accidentally damaged, it can be restored by /usr/local/bin/redis-check-aof–fix appendonly.aof

​ Open the appendonly.aof file with the vi command and append a useless message after it

image-20211014205624208

​ After restarting redis, starting the client failed because the AOF file could not be read correctly

image-20211014205714951

At this time, enter the above command to repair the aof file

image-20211014205807962

At this point, connect correctly and read data successfully

image-20211014205847537

5 AOF synchronization frequency setting

​ Synchronization frequency setting is divided into three types

(1)appendfsync always:

​ Indicates that it is always synchronized, and every write to Redis will be recorded in the log immediately. The performance is poor but the data integrity is better

(2)appendfsync evverysec

​ Synchronize every second and record it into the log once every second. If it crashes at this time, the data of this second may be lost

(3)appendfsync no

​ Do not take the initiative to synchronize, and leave the timing of synchronization to the operating system

[External link image transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the image and upload it directly (img-1artBSYI-1634217992319)(/Users/sundaohan/Library/Application Support/typora-user-images/image-20211014210243169 .png)]

6 Rewrite compression

​ AOF adopts the method of file appending, and the file will become larger and larger. To avoid this situation, a rewriting mechanism has been added. When the size of the AOF file exceeds the set threshold, Redis will start the content compression of the AOF file. Only keep the minimum set of instructions that can restore data. You can use the command bgrewriteaofs

​ When the AOF file continues to grow and is too large, a new process will be forked to rewrite the file (also write the temporary file first and then rename it). The rewrite after redis4.0 is to convert the rdb snapshot to binary The form is attached to the new aof head, as the existing historical data, replacing the original journal operation.

7 advantages

(1) The backup mechanism is more robust and the probability of data loss is lower

(2) Readable log text, through the operation of AOF files, can handle misoperations

8 Disadvantages

(1) It takes up more space than RDB

(2) Restoring backups is slow

(3) If every read and write is synchronized, the performance pressure will be high. It
is not recommended to use AOF alone. It is best to enable both RDB and AOF to avoid bugs

Guess you like

Origin blog.csdn.net/weixin_42195126/article/details/120773197