This article will introduce you to the snapshot method (RDB) in Redis persistence. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Redis reads and writes in memory, so its performance is high, but the data in memory will be lost as the server restarts. In order to ensure that the data Without losing it, we need to store the data in the memory to the disk so that the original data can be restored from the disk when Redis restarts. The whole process is called Redis persistence.
Redis persistence is also one of the main differences between Redis and Memcached, because Memcached does not have persistence functionality.
Redis persistence has the following three methods:
Because each persistence solution has specific usage scenarios, let’s start with RDB persistence.
RDB (Redis DataBase) is the process of writing a memory snapshot (Snapshot) at a certain moment to disk in binary form.
There are two types of persistence triggering methods for RDB: one is manual triggering, and the other is automatic triggering.
There are two manual trigger persistence operations: save
and bgsave
. Their main difference is reflected in: whether to block or not. Redis main thread execution.
Executing the save
command in the client will trigger the persistence of Redis, but at the same time it will also make Redis in a blocking state until the RDB is persisted. Completed, it will respond to commands sent by other clients, so must be used with caution in a production environment.
save
The command is used as follows:
As can be seen from the picture, after executing the save
command , the modification time of the persistence file dump.rdb
changes, which means save
successfully triggered RDB persistence.
save
The command execution process is as shown below:
save command is that
bgsave will fork() a child process to perform persistence. In the whole process, only the fork() child process Sometimes there is a short blocking. After the child process is created, the main process of Redis can respond to the requests of other clients. Compared with the
save command that blocks the entire process, obviously
bgsave command is more suitable for us to use.
bgsave The command is used as shown in the figure below:
The execution process is as shown in the figure below:
2) Automatic triggering
① save m n
means that if n keys change within m seconds, persistence will be automatically triggered. The parameters m and n can be found in the Redis configuration file. For example,
save 60 1
indicates that if at least one key changes within 60 seconds, RDB persistence will be triggered. Automatically trigger persistence. The essence is that Redis will automatically execute the
bgsave
command once if the set trigger conditions are met. Note: When setting multiple save m n commands, persistence will be triggered if any condition is met.
For example, we set up the following two save m n commands:
If the Redis key value changes 10 times within 60s, persistence will be triggered; if Redis key value changes within 60s If the key value has changed less than 10 times, Redis will determine whether the key value of Redis has been modified at least once within 600s. If so, persistence will be triggered.
flushall
command is used to clear the Redis database. It must be used with caution in a production environment. When Redis executes the flushall
command, Automatic persistence will be triggered and the RDB file will be cleared.
The execution results are shown in the figure below:
In Redis master-slave replication, when the slave node performs a full replication operation , the master node will execute the bgsave
command and send the RDB file to the slave node. This process will automatically trigger Redis persistence.
Properly setting the RDB configuration can ensure the efficient and stable operation of Redis. Let’s take a look at the configuration items of RDB?
RDB configuration parameters can be found in the Redis configuration file. The specific contents are as follows:
# RDB 保存的条件 save 900 1 save 300 10 save 60 10000 # bgsave 失败之后,是否停止持久化数据到磁盘,yes 表示停止持久化,no 表示忽略错误继续写文件。 stop-writes-on-bgsave-error yes # RDB 文件压缩 rdbcompression yes # 写入文件和读取文件时是否开启 RDB 文件检查,检查是否有无损坏,如果在启动是检查发现损坏,则停止启动。 rdbchecksum yes # RDB 文件名 dbfilename dump.rdb # RDB 文件目录 dir ./
The more important parameters are as follows:
① save parameter
It is used to configure the parameters that trigger RDB persistence conditions. When the save conditions are met, the data will be persisted to the hard disk.
The default configuration description is as follows:
② rdbcompression parameter
The default value is yes
which means turning on RDB file compression, Redis will use the LZF algorithm for compression. If you don't want to consume CPU performance for file compression, you can set it to turn off this function. The disadvantage of this is that it requires more disk space to save files.
③ rdbchecksum parameter
Its default value is yes
, which indicates whether to enable RDB file check when writing and reading files, and check whether there is any damage. If If damage is found during startup, the startup will be stopped.
You can use commands to query the current configuration parameters in Redis. The format of the query command is: config get xxx
. For example, if you want to get the storage name setting of the RDB file, you can use config get dbfilename
. The execution effect is as shown in the figure below:
To query the file directory of RDB, you can use the command config get dir
. The execution effect is as shown in the figure below:
Set the RDB configuration in the following two ways:
config set dir "/usr/data"
is used to modify the RDB storage directory. Note: The method of manually modifying the Redis configuration file is globally effective, that is, restarting the Redis server setting parameters will not be lost, and using the command modification method, restart the Redis Then it will be lost. However, if you want to manually modify the Redis configuration file to take effect immediately, you need to restart the Redis server, but the command method does not require restarting the Redis server.
Tips: The Redis configuration file is located in the root path of the Redis installation directory, and the default name is redis.conf.
When the Redis server starts, if the RDB file dump.rdb exists in the Redis root directory, Redis will automatically load the RDB file to restore the persistent data.
If there is no dump.rdb file in the root directory, please move the dump.rdb file to the root directory of Redis first.
Verify whether the RDB file is loaded
Redis has log information during startup, which will show whether the RDB file is loaded. We execute the Redis startup command: src/redis-server redis. conf
, as shown in the figure below:
It can be seen from the log that the Redis service has loaded the RDB file normally when it started.
Tips: While the Redis server is loading the RDB file, it will be blocked until the loading work is completed.
Disabling persistence can improve the execution efficiency of Redis. If you are not sensitive to data loss, you can execute it while connecting to the clientconfig set save ""
command can disable the persistence of Redis, as shown in the following figure:
Through this article we can get As you know, RDB persistence is divided into two methods: manual triggering and automatic triggering. Its advantage is that the storage file is small and data recovery is faster when Redis starts. The disadvantage is that there is a risk of losing data. Restoring RDB files is also very simple. You only need to put the RDB files in the root directory of Redis, and the data will be automatically loaded and restored when Redis starts.
If the Redis server CPU usage is too high, what may be the cause? You are welcome to write your answers in the comment area.
https://redis.io/topics/persistence
https://blog.csdn.net/ qq_36318234/article/details/79994133
https://www.cnblogs.com/ysocean/p/9114268.html
https://www.cnblogs. com/wdliu/p/9377278.html
This article is reproduced from: https://segmentfault.com/a/1190000021036574
For more redis knowledge, please pay attention to redis introduction Tutorial column.
The above is the detailed content of Learn more about the snapshot method (RDB) in Redis persistence. For more information, please follow other related articles on the PHP Chinese website!