Home  >  Article  >  Database  >  Learn more about the snapshot method (RDB) in Redis persistence

Learn more about the snapshot method (RDB) in Redis persistence

青灯夜游
青灯夜游forward
2020-03-31 10:23:252775browse

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.

Learn more about the snapshot method (RDB) in Redis persistence

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.

Learn more about the snapshot method (RDB) in Redis persistence
Redis persistence is also one of the main differences between Redis and Memcached, because Memcached does not have persistence functionality.

1. Several persistence methods

Redis persistence has the following three methods:

  • Snapshot method(RDB, Redis DataBase) writes the memory data at a certain moment to the disk in binary form;
  • file append method (AOF, Append Only File) records all operation commands and writes them as text Append to the file in the form;
  • Hybrid persistence method, a new method after Redis 4.0. Hybrid persistence combines the advantages of RDB and AOF. When writing, First, write the current data to the beginning of the file in the form of RDB, and then store the subsequent operation commands in the file in the AOF format. This can not only ensure the speed of Redis restart, but also reduce the risk of data loss.

Because each persistence solution 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 (Snapshot) at a certain moment to disk in binary form.

3. Persistence triggering

There are two types of persistence triggering methods for RDB: one is manual triggering, and the other is automatic triggering.

1) Manual trigger

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.

① save command

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:

Learn more about the snapshot method (RDB) in Redis persistence

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:

Learn more about the snapshot method (RDB) in Redis persistence

##② bgsave command

bgsave (background save ) means background saving. The biggest difference between it and the

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:

Learn more about the snapshot method (RDB) in Redis persistence

##bgsave

The execution process is as shown in the figure below:

Learn more about the snapshot method (RDB) in Redis persistence2) Automatic triggering

After talking about the manual triggering method of RDB, let’s look at how to automatically trigger RDB persistence?

RDB automatic persistence mainly comes from the following situations.


① save m n

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:

  • save 60 10
  • save 600 1

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

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:

Learn more about the snapshot method (RDB) in Redis persistence

③ Master-slave synchronization triggers

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.

4. Configuration instructions

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:

  • save 900 1: Indicates that if at least 1 key value changes within 900 seconds, the data will be persisted to the hard disk;
  • save 300 10: Indicates that if at least 10 key values ​​change within 300 seconds, the data will be persisted to the hard disk;
  • save 60 10000: Indicates that if at least 10,000 key values ​​change within 60 seconds, the data will be persisted to the hard drive.

② 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.

5. Configuration query

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:

Learn more about the snapshot method (RDB) in Redis persistence

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:

Learn more about the snapshot method (RDB) in Redis persistence

6. Configuration settings

Set the RDB configuration in the following two ways:

  • Manually modify the Redis configuration file;
  • Use command line settings, for example, use 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.

7.RDB file recovery

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:

Learn more about the snapshot method (RDB) in Redis persistence

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.

8.RDB advantages and disadvantages

1) RDB advantages

  • RDB content is binary data, takes up less memory, is more compact, and is more suitable as Backup file;
  • RDB is very useful for disaster recovery. It is a compact file that can be transferred to the remote server faster for Redis service recovery;
  • RDB can improve Redis to a greater extent The running speed is because the Redis main process will fork() a child process every time it is persisted to persist the data to the disk. The Redis main process will not perform operations such as disk I/O;
  • With AOF RDB files can be restarted faster than files in the format.

2) Disadvantages of RDB

  • Because RDB can only save data for a certain time interval, if the Redis service is accidentally terminated midway, the data for a period of time will be lost. Redis data;
  • RDB requires frequent fork() to persist it on disk using child processes. Fork() can be time-consuming if the data set is large, and can cause Redis to stop serving clients for a few milliseconds or even a second if the data set is large and CPU performance is poor.

9. Disable persistence

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:
Learn more about the snapshot method (RDB) in Redis persistence

10. Summary

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.

11. Thinking questions

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.

12. References & Acknowledgments

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete