Several ways of redis persistence — in-depth analysis of RDB

1. Several ways of persistence

Redis persistence has the following three methods:

Because each persistence scheme has specific usage scenarios, let's start with RDB persistence.

2. Introduction to RDB

RDB (redis database) is the process of writing a memory snapshot at a certain time to disk in binary mode.

3. Persistence trigger

There are two types of persistent triggering methods for RDB: manual triggering and automatic triggering.

1) Manual trigger

There are two operations to manually trigger persistence: save and bgsave. Their main difference is whether to block the execution of redis main thread.

① Save command

Executing the Save command on the client will trigger the persistence of redis, but it will also make redis in a blocked state. It will not respond to commands from other clients until RDB persistence is completed. Therefore, it must be used with caution in the production environment.

The Save command uses the following:

② Bgsave command

Bgsave (background save) means background save. The biggest difference between bgsave and the Save command is that bgsave will fork () a child process to perform persistence. In the whole process, only the fork () child process is temporarily blocked. 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 whole process, it is obvious that the bgsave command is more suitable for us. The bgsave command is used, as shown in the following figure:

2) Automatic trigger

Having finished the manual triggering of RDB, let's see how to automatically trigger RDB persistence? RDB automatic persistence mainly comes from the following situations.

① save m n

Save m n means that persistence is automatically triggered if n keys change in M seconds. Parameters m and N can be found in the redis configuration file. For example, save 60 1 indicates that RDB persistence will be triggered when at least one key changes within 60 seconds. The essence of automatically triggering persistence is that redis automatically executes the bgsave command once if the set trigger conditions are met through judgment. Note: when multiple save m n commands are set, persistence will be triggered if any condition is met. For example, we set the following two save m n commands:

If the redis key value changes 10 times within 60s, persistence will be triggered; If the key value of redis is changed less than 10 times within 60s, redis will judge whether the key value of redis has been modified at least once within 600s. If it is satisfied, persistence will be triggered.

② flushall

The flush command is used to empty the redis database. It must be used with caution in the production environment. After redis executes the flush command, it will trigger automatic persistence and empty the RDB file. The execution results are shown in the following figure:

③ Master slave synchronization trigger

In redis master-slave replication, when the slave node performs full replication, the master node will execute the bgsave command and send the RDB file to the slave node. This process will automatically trigger redis persistence.

4. Configuration description

Setting RDB configuration reasonably can ensure the efficient and stable operation of redis. Let's look at the configuration items of RDB?

RDB configuration parameters can be found in redis configuration file. The details 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 listed as follows: ① save parameter 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 is described as follows:

② The default value of rdbcompression parameter is yes, which means RDB file compression is enabled, and redis will use lzf algorithm for compression. If you do not want to consume CPU performance for file compression, you can set this function to off. The disadvantage is that you need more disk space to save files. ③ The default value of the rdbchecksum parameter is yes, which indicates whether the RDB file check is enabled when writing and reading files, and whether there is damage. If damage is found during startup, the startup will be stopped.

5. Configuration query

In redis, you can use the command to query the current configuration parameters. The format of the query command is: config get XXX. For example, if you want to get the storage name setting of RDB files, you can use config get dbfilename. The execution effect is shown in the following figure:

6. Configuration settings

RDB configuration can be set in the following two ways:

Note: the method of manually modifying redis configuration files takes effect globally, that is, the setting parameters of redis server will not be lost after restarting. However, the method of modifying with commands will be lost after redis restarts. However, if you manually modify the redis configuration file, you need to restart the redis server if it wants to take effect immediately, but the command method does not need to restart the redis server.

7. RDB file recovery

When the redis server starts, if there is an RDB file dump. In the redis root directory RDB, redis will automatically load RDB files to recover persistent data. If the root directory does not have dump RDB file, please dump Move the RDB file to the root directory of redis. Verify whether the RDB file is loaded. Redis has log information during startup, and will display whether the RDB file is loaded. We execute the redis startup command: Src / redis server redis Conf, as shown in the figure below:

8. Advantages and disadvantages of RDB

1) RDB benefits

2) RDB disadvantages

9. Disable persistence

Disabling persistence can improve the execution efficiency of redis. If you are not sensitive to data loss, you can disable the persistence of redis by executing the "config set save" command when connecting to the client, as shown in the following figure:

10. Summary

We can learn from this article that RDB persistence is divided into manual triggering and automatic triggering. Its advantages are small storage files, fast data recovery when redis is started, and its disadvantages are the risk of data loss. RDB file recovery is also very simple. You only need to put the RDB file in the root directory of redis, and the data will be automatically loaded and recovered when redis starts.

11. Thinking questions

If the CPU usage of redis server is too high, what may be the reason? You are welcome to write down your answers in the comments area.

12. Reference & acknowledgment

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>