[Redis study notes (7)] AOF persistence detailed

This article is published by the official account [Developing Pigeon]! Welcome to follow! ! !


Old Rules-Sister Town House:

One. AOF persistence

(I. Overview

       In addition to the RDB persistence function, Redis also provides the AOF persistence function, which records the state of the database by saving the write commands executed by the Redis server. All commands written into the AOF file are saved in the Redis command request protocol format. When the server is started, the database state before the server is shut down can be restored by loading and executing the commands saved in the AOF file.

(Two) the realization of AOF persistence

       The realization is divided into three steps: command appending, file writing, and file synchronization.

1. Add instruction

       When the AOF persistence function is turned on, the server will append the executed write command to the end of the aof_buf buffer in the server state in the protocol format after executing a write command.


2. Write and synchronize

       The Redis server process has an event loop. The file event in this loop is responsible for receiving the client's command request and sending the command reply to the client, while the time event is responsible for executing functions that need to be run regularly like the serverCron function. The server will call the flushAppendOnlyFile function every time before ending an event loop to consider whether the content in the aof_buf buffer needs to be written and saved to the AOF file. The judgment is based on the appendfsync option configured by the server. This option may have three values. , The default is everysec:

(1) always

       Write and synchronize all the contents in the aof_buf buffer to the AOF file. The efficiency is the slowest, but the safest, and the command data in one time cycle is lost at most.


(2) everysec

       Write all the contents in the aof_buf buffer to the AOF file. If the time of the last synchronization of the AOF file is more than 1s, then the AOF file will be synchronized again. At most one second of command data is lost.


(3) no

       Write all the contents in the aof_buf buffer to the AOF file, but do not synchronize the AOF file. All write command data after the last synchronization of the AOF file will be lost.


(3) Loading and restoring of AOF files

       The steps for Redis to read the AOF file and restore the database state are as follows:

       1. Create a pseudo client without a network connection, because Redis commands can only be executed in the client context, and the commands used when loading the AOF file are directly derived from the AOF file instead of the network connection, so the server uses A pseudo client without a network connection.

       2. Analyze and read a write command from the AOF file

       3. Use the pseudo client to execute the read command

       4. Repeat the above steps until all commands have been read.


(4) AOF rewrite

1 Overview

       With the running time of the server, the content in the AOF file will become more and more, and the volume of the file will become larger and larger. If it is not controlled, it will affect the Redis server and the host. Therefore, in order to solve the problem of AOF file volume expansion, Redis provides the AOF file rewriting function. The server will create a new AOF file to replace the existing AOF file. The two AOF files have the same database status, but the new AOF file. The file does not contain redundant commands that waste space, so the volume is smaller.


2. Realization

       In fact, AOF rewrite does not require any reading, analysis, and writing operations on existing AOF files, it is achieved by directly reading the current database status. If 5 write commands for a key are recorded in the previous AOF file, then the new AOF file can directly record the latest write command, which reduces the volume of the AOF file. In order to avoid the client input buffer overflow when executing commands, the rewrite program will first check the number of elements contained in the key, and if there are too many, multiple commands will be used to record the value of the key.


3. AOF background rewrite

       The AOF rewrite program aof_rewrite function can complete the task of creating a new AOF file, but this function will perform a lot of write operations, so the thread that calls this function will be blocked for a long time. Since the Redis server uses a single thread to process the command request, the AOF rewrite program is placed in the child process for execution, so that the parent process continues to process the command request. However, in the process of rewriting, the modification of the database state by the parent process may cause inconsistency with the state of the rewritten AOF file.

       In order to solve this problem, the Redis server sets up an AOF rewrite buffer, which is used after the server creates a child process. After the server executes a write command, it will send the write command to the AOF buffer and AOF rewrite at the same time. Buffer, the content in the AOF buffer will be periodically written and synchronized to the AOF file, and when the child process completes the AOF rewriting work, it will send a signal to the parent process, the parent process will receive the signal Call a signal processing function to write the contents of the AOF rewrite buffer to the new AOF file. At this time, the database status of the new AOF file is consistent with the current database. Rename the new AOF file to overwrite the original AOF file.

       In the entire AOF background rewriting process, only the signal processing function will block the server process. At other times, the AOF background rewriting will not block the parent process, minimizing the impact of AOF rewriting on server performance. This is also the principle of the BGREWRITEAOF command.

Guess you like

Origin blog.csdn.net/Mrwxxxx/article/details/114237249