Introduction to Redis on the aof persistence mechanism

First of all, aof is a persistence scheme for redis to record database write operations. It will faithfully record all write operations and store them in a .aof file in the format of the redis protocol. When restarting redis, redis can be based on .aof file to restore the dataset.

For example: First, we start the redis service and store some data. Here I randomly store four pieces of data, as shown in the following figure:


Then we clear all the data, use the command flush:


In this way, you can see that there is no more data now, so we can try to use aof to reply to these data, now shutdown+exit to close redis, check the usr/local/bin directory (the default installation path of linux, my redis is Installed on this path), you can find an appendonly.aof file. Obviously, this file is the aof file used to record database write operations. We use this file to reply to data. With the shutdown command, the rdb of redis will also start the persistence operation, so the dunp.rdb file is generated. In order to prevent rdb from playing the role of replying data, we use the rm -f filename command to manually delete this file.


Well, now it stands to reason that if you open redis again, you can see the recovered data, so can it be recovered? Let's start redis and take a look at the keys *:


You will find that there is nothing at all. Isn't aof bad to use? Of course not, that's because the aof mechanism faithfully records every write operation, including the flush you input. Therefore, in the aof file, your flushall The command will also be recorded, you can use the vim appendonly.aof command to check the aof file, you can find that the last record is flush, here we delete it manually (in actual situation, it is not recommended to manually modify the .aof file, in fact, no one goes back to execute it flushall command), then we exit redis, and then start it again, you will find that the data has been restored successfully for you:


The things to note here are:

1. First of all, redis closes aof by default. We need to modify the configuration file (.redis.conf). If it is only for learning purposes, it is recommended to copy another configuration file to modify, and directly start the copied configuration file when using redis. , open the configuration file and you can see:


There is an appendonly parameter below. The default value is no, which is to close aof. We set its value to yes, and then wq saves and exits;

2. In the above configuration file, the next line of the appendonly parameter (not shown in the screenshot) is the default name of the configuration aof file. The default name is appendonly.aof. It is recommended not to modify it here.

Then we are looking at the configuration file, which shows three persistence strategies of aof:


Among them, always is synchronous persistence, which means that as long as there is an operation, I will record it. This strategy can make the data set have better integrity, but the performance is relatively poor.

everysec is the default policy, I will record any changes within seconds

A no after the group is not recorded.

3. What should I do if the aof file is damaged? If there is an illegal statement or other damage in the content of an aof file, then redis will not start normally, because when redis starts, the aof file will be loaded by default. We can simulate it manually: first, I randomly Enter something:


At this time, when we start redis again, we will find that the service cannot be started:


So what to do, don't we need our data? Of course not, redis has a way to restore files, we can use the redis-check-aof --fix appendonly.aof command to repair the .aof file, this command will restore the .aof file. The illegal statements in the aof file are deleted, and after we execute them, we will find that redis can be started normally again, and the previous data are all in:


As you can see, redis can be successfully started, and the four records previously stored are all there.

4. Another point is that rdb and aof in redis can be used at the same time, but the aof file is loaded first by default.

5. Copy the aof file with data and save it to the corresponding directory (config get dir)

Finally, it should be noted that AOF adopts the method of file appending, and the file will become larger and larger. In order to avoid this situation, redis has a rewrite mechanism. When the size of the AOF file exceeds the set threshold, redis will It will start the content compression of the AOF file, and only retain the minimum instruction set that can restore the data. Use the command bgrewriteaof. The internal mechanism of this rewrite is that redis will record the size of the AOF file when it was rewritten last time. The default configuration is when AOF Triggered when the file size is double the size since the last rewrite and the file is larger than 64M (see configuration file).

Disadvantages relative to rdb:

1. For the same data set, the volume of the aof file is much larger than that of the rdb file.
2. According to the fsynccelve used, the speed of aof is slower than
the choice of rdb aof and rdb: the
official recommendation is to use two persistence methods at the same time


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325159583&siteId=291194637