Introduction to persistence
RDB
AOF
The difference between RDB and AOF
Persistence application scenarios
For the persistence function, it is actually very simple and not that complicated
##1. Introduction to persistence
All data in redis is stored in memory. If redis crashes, the data will be lost. Redis persistence is to save data on disk. The working mechanism that uses permanent storage media to save data processes and restore the saved data at a specific time is called persistence.
What is saved in the persistence process?
The first snapshot form stores data results and focuses on the data, which is the RDB that will be discussed below
The second type of operation process, the storage operation process, focuses on the data operation process, which is the AOF that will be discussed below
The following figure is the configuration information of redis.conf. After execution After saving, a dump.rdb file will be generated
Now we set a value and then save it in /usr/local/redis/data There will be a file dump6379.rdb
In fact, compared to other relational database recovery, this data recovery basically requires no operation. Just restart it
When you When executing save, instructions from other clients requesting redis will wait until the save instruction is completed. Because the save instruction is executed in a single thread, once the execution time is too long, it will directly cause other users to be unable to store data normally. So this command is abandoned by default. The bgsave introduced below will be used instead
When bgsave is executed in redis Will directly return a Background saving started
At this time we are looking at the log file. The bgsave command is optimized for the save blocking problem
以下配置是默认配置 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes
save [Time] [Amount of key changes]
That is to say, if 10 key values change in 300 seconds, bgsave will be executed in the background
AOF files store the process of executing command operations, and data recovery is also restored using the operation process.
Execute a redis command
appendonly yes|no
appendfsync always| everysec | no
Then use restart the redis service, you can use the usr/local/redis/data directory You can see the appendonly.aof file below
Then we execute a command on the redis client and check it out. You can see that the data will be stored in the file appendonly.aof.
Let's look at a case first. After we repeatedly set the name key, we opened the appendonly.aof file to view it. We can see that there are three operations, but these three operations are all modified by one key! Can't we only save the last key? With this question, we continue to look down
Also, the speed of data recovery will be faster.
It will also improve the efficiency of persistence.
hdel, srem
. There is also the problem mentioned in 3-5. Continuous operations on a keylpush list a lpush lsit b lpush list c
can be converted into lpush list a b c
. Command: bgrewriteaof
Following our questions 3-5, we execute the bgrewriteaof command on the command line and then view the appendonly.aof file
After execution, you will find that the file has become smaller and there is only one instruction in the file
##3-10 AOF automatic rewrite
Configuration:
Trigger comparison parameters:
When aof_current_size > auto-aof-rewrite-min-size 64mb will start rewriting
This picture comes from the Internet
## The above is all the content of redis persistence. 4. Summary
The above is the detailed content of Redis persistence full version. For more information, please follow other related articles on the PHP Chinese website!