Current location - Health Preservation Learning Network - Healthy weight loss - Configuration and workflow of redis AOF persistence
Configuration and workflow of redis AOF persistence
Or find the attribute appendonly in redis.conf

In fact, the data in redis is limited. Many data may automatically expire, may be deleted by users, or may be cleared by redis with cache clearing algorithm. Redis will constantly clear the old data, leaving only some frequently used data in memory. Therefore, some data execution commands that have been cleared are still in the AOF log file, which will make the AOF log file bigger and bigger. Therefore, AOF will do a rewrite operation every once in a while. For example, the data currently stored by AOF is100 w; At this time, the data of 10w is only stored in the actual redis, so at this time, a set of latest logs will be established based on the data of 10w, and the old a of file will be overwritten in a of to ensure that the log file of AOF is not too large and consistent with the memory of redis.

Rewrite policy configuration

Before redis2.4, you still need to manually develop some scripts crontab to rewrite AOF through BGREWRITEAOF command, but after version 2.4, you will automatically rewrite it or configure it in redis.conf Its strategy is:

The above policy configuration means: for example, after the last rewrite, the AOF file is 128M, and then it is written after 128M. If it is found that the growth ratio exceeds 100% of the previous size, that is, 256M, rewriting will be triggered, but before triggering, the size will be compared with 64mb of min-size. if

Rewritten workflow

A subprocess of (1)redis fork.

(2) Based on the data in the current memory, the subprocess establishes a log and starts writing the log into a new temporary AOF file.

(3) After receiving the new write instruction from the client, the 3)redis main process writes the log into the memory, and the new log instruction will also be written into the old AOF log file.

(4) After the child process finishes writing the new log file, the redis main process appends the newly written log instruction information in the memory to this new AOF file.

(5) Replace the old file with the new AOF file.

If the machine stops while redis is appending data to the AOF file, the AOF file may be damaged. Before restarting redis, we will find the AOF file and repair it with the tools provided by redis (execute the following command to repair the AOF file:).

(1) If RDB is performing snapshot operation, redis will not perform rewriting; ; If redis performs an AOF rewrite, then redis will not take a snapshot of RDB.

(2) If the user executes the command BGREWRITEAOF when redis executes the snapshot, redis will wait until the RDB snapshot is generated before rewriting.

(3) There are both snapshot files of RDB and log files of AOF. When redis restarts, the AOF log file will be used first, because the data in the AOF file is more complete.