Redis key Point (AOF)

Source: Internet
Author: User
Tags redis

What is AOF?

There is an option called AppendOnly in the Redis configuration file that can write yes or No. This option is responsible for whether to open the aof log switch. AOF log, you can simply understand the same thing as MySQL Binlog, the role is to record each write operation, You can use it to recover the database state when you encounter a power outage. But he is not bin, but text. A line, written very canonical. If you're a redis, you can also get human flesh. Recover data through it.

Extending Knowledge:

Redis has three types of landing files: Data Files-you can set their location and file name in the configuration, the default file name Dump.rdb log file-can also be configured in the configuration. Of course, when you're running in a daemon way, this value is not set to stdout, This setting will automatically be replaced with/dev/null aof file-The protagonist of our article, his role is for data recovery. In addition to setting whether to open, you can also set the way to write log. This is the total of three species, 1 is each write operation to ensure that the Fsync () to complete the , 2 is called once per second Fsync (), 3 is never called, let the operating system to synchronize themselves. Of course, set to different, efficiency will be different, your data loss risk is also different.

Run the process

Since it is log file, and is to be used for recovery, then we can move toe all think, this thing will be more and more big, whether your application is large or small, if the aof file only increase, the file will be infinitely grow, This is a problem that all binlog will encounter. And usually there is a rotate solution to this problem. It is the day that the log reaches a certain size or writes the logs to a new file every other time, and the old log file can be used for backup or otherwise.

And Redis's aof and rotate slightly different, he used a relatively simple way, is to give the current all the data a snapshot. Then write the next log on the basis of this snapshot.

The advantage of a snapshot is that you may have changed 100 of data (for example, multiple operations on a single piece of data) in a 10w operation. That's when you aof the 10w write operation record into 100 records. The equivalent of merging all the previous executions. The original is very big aof smaller.

The command to perform this operation is:bgrewriteaof (background rewrite append only file)

What does this snapshot look like? Basic and General writing log is no different, but also a piece of writing records. For example, there are now 3 string data a=>1,b=>2,c=>3 in Redis. The basic content of this snapshot is to write to the a=>1, write to B=>2, write to C=>3.

At this time to use aof for recovery, as long as the first several, you can restore the current state, and then perform the write operation, you can completely reproduce the data.

Internal Implementation

We probably know what happens when we perform bgrewriteaof , and here's how Redis is implemented. The following steps: fork! Redis produces child processes through fork. The subprocess performs traversal operations on the current data, generating a write log of all current data, and writing the logs to a temporary file. (In fact, the subprocess wrote a temporary file, and then rename into another temporary file. The parent-child process executes in parallel, and when the subprocess traverses and writes a temporary file, it receives the request, processes the request, writes the aof as usual, but then writes the new aof in a buffer. When the subprocess completes the traversal operation, the temporary file is completed, and it exits. When the WAIT3 function of the parent process receives a message that the child process exits, he appends all the aof that he now collects in the buffer to the temporary file. The final step, the temporary file Rename, renamed to Appendonly.aof, when the original aof file was overwritten. The whole process is complete.

If your aof file is slightly larger, you can perform bgrewriteaof on a terminal, and then immediately LS attached to the data directory of Redis several times, you can see that Sir became a temporary file, Temporary files are smaller than the original appendonly.aof, and then the temporary file disappears, and the original appendonly.aof smaller, in fact, temporary files rename became appendonly.aof. Overwrite the original large file. It looks like the temporary file is gone.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.