Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
We continue with the AOF configuration above. To understand the following configuration, we need to explain the principle of “log rewriting” :
rewrite
Because AOF appends commands to the end of the file, the size of the AOF file gets larger as more write commands are added. To avoid this situation, a new override mechanism has been added: AOF files can be rebuilt without interrupting the service client.
Rewrite trigger: By executing the bgrewriteaOF command, a new AOF file can be generated that contains the minimum of commands required to rebuild the current data set. In Redis 2.2, you need to run this command manually. In Redis 2.4, you can automatically run this command by modifying the configuration file (configuration is described below).
Rewriting principle:
Redis
Execute system functionfork()
, create a child process (exactly the same as the main process);- The child process will start new
AOF
The contents of the file are written to a temporary file; - For all newly executed write commands, the parent process accumulates them into an in-memory cache while appending the changes to the existing one
AOF
The end of the file so that even if an downtime occurs in the middle of a rewrite, the existingAOF
Files are also secure; - When the child completes the rewrite, it sends a signal to the parent, which, upon receiving the signal, appends all the data in the memory cache to new
AOF
The end of the file. Redis
Replace the old file atomically with the new one, after which all commands are appended directly to the new oneAOF
The end of the file.
The configuration file
no-appendfsync-on-rewrite no
When we write both the main process and the child process at the same time, both of them operate on disk, and rewriting usually involves a lot of disk operations, causing the main process to block while writing aOF files.
To solve this problem, the no-appendfsync-on-rewrite argument comes in.
- If this parameter is set to
no
, is the most secure way, does not lose data, but has to endure the problem of blocking; - If set to
yes
, this is equivalent to theappendfsync
Set tono
, indicating that no disk operation has been performed, but only a buffer has been written. So this does not cause blocking (because there are no competing disks), but if at this pointredis
If you fail, you lose data. How much data is missing? inlinux
A maximum of 30 seconds of data will be lost under the default Settings of the operating system.
Therefore, if the application system cannot tolerate delays and can tolerate a small amount of data loss, set it to yes; If the application system cannot tolerate data loss, set this parameter to no.
auto-aof-rewrite-percentage 100
Rewrite percentage, which defaults to one times the size of the AOF file since the last rewrite.
auto-aof-rewrite-min-size 64mb
Minimum value for override trigger: 64MB.
Auto-aof -rewrite-min-size and auto-aof-rewrite-percentage parameters determine the automatic trigger time. Redis records the AOF size of the last rewrite, triggered by default when the AOF file size is double the size since rewrite and the file size is greater than 64M.
Big Internet companies generally start with 3G
aof-use-rdb-preamble yes
When overwriting AOF files, Redis is able to use RDB precursors in AOF files to speed up overwriting and recovery. With this option enabled, the rewritten AOF file consists of two distinct sections: RDB File and AOF tail
When Redis is loaded, it recognizes that the AOF file begins with a Redis string, loads the prefixed RDB file, and then continues loading the AOF tail.
Now that the AOF configuration file is over, let’s look at the pros and cons of AOF and how to choose between the two methods of persistence. If you have different opinions or better idea, welcome to contact AH Q, add AH Q can join the technical exchange group to participate in the discussion!