Redis are in memory to read and write, so its performance is higher, but the data in memory can be lost as server restart, in order to ensure that data is not lost, we need to store data in memory to disk, so that when a Redis restart from the disk to recover the original data, and the whole process is called Redis persistence.
1. Several ways to persist
Redis persists in three ways:
- Snapshot mode (Redis DataBase) Writes memory data ata certain point in time to the disk in binary mode.
- File Append (AOF, Append Only File) records all operation commands and appends them to files in text form.
- Mixed persistence is a new method added after Redis 4.0. Mixed persistence combines the advantages of RDB and AOF. When writing, the current data is first written into the beginning of the file in the form of RDB, and then the subsequent operation commands are stored in the file in the format of AOF. This ensures the speed of Redis restart and reduces the risk of data loss.
Since each persistence scheme has a specific usage scenario, let’s start with RDB persistence.
2. Introduction of RDB
Redis DataBase (RDB) is a process of writing a Snapshot of a memory ata certain time to disks in binary mode.
3. Persistent triggering
There are two types of RDB persistence triggers: manual and automatic.
1) Manual trigger
There are two operations to trigger persistence manually: save and BGSave. The main difference between them is whether to block the Redis main thread.
1) save command
Executing the save command on the client will trigger Redis persistence, but at the same time, Redis will block until the RDB persistence is complete and will not respond to the command sent by other clients. Therefore, be careful to use it in the production environment.
Use the save command as follows:
save
dump.rdb
save
save
(2) bgsave command
The main difference between bgSave and save is that the bgSave command forks () a child process for persistence, and blocks only briefly when the child process is created. The main Redis process can then respond to requests from other clients, which makes bgSave a better choice than the save command, which blocks the entire process. The bgsave command is used, as shown below:
bgsave
2) Automatic trigger
How to trigger RDB persistence automatically? RDB automatic persistence mainly comes from the following situations.
1) save m n
Save m n means that if n keys change within m seconds, persistence will be triggered automatically. The parameters m and n can be found in the Redis configuration file, for example, save 60 1 indicates that RDB persistence is triggered if at least one key changes within 60 seconds. Automatic trigger persistence is essentially a bgsave command automatically executed by Redis if the trigger conditions are met. Note: When multiple save m n commands are set, persistence is triggered if any of the criteria is met. For example, we set the following two save m n commands:
- save 60 10
- save 600 1
If the Redis key changes 10 times in 60 seconds, persistence will be triggered. If the Redis key changes less than 10 times in 60 seconds, Redis determines whether the Redis key has been changed at least once in 600 seconds. If so, persistence is triggered.
(2) flushall
The flushall command is used to flush the Redis database. Use it with caution in the production environment. After the flushall command is run in Redis, automatic persistence is triggered to flush RDB files. The execution result is as follows:
③ Master/slave synchronization is triggered
In Redis master-slave replication, when the slave node performs a full copy operation, the master node executes the BGsave command and sends the RDB file to the slave node, which automatically triggers Redis persistence.
4. Configuration description
Setting the RDB configuration properly can ensure the efficient and stable operation of Redis. What are the configuration items of RDB?
The RDB configuration parameters can be found in the Redis configuration file as follows:
# RDB save conditions
save 900 1
save 300 10
save 60 10000
Whether to stop persisting data to disk after # bgSave fails: yes: stop persisting data to disk, no: ignore the error and continue writing to the file.
stop-writes-on-bgsave-error yes
# RDB file compression
rdbcompression yes
# Enable RDB file check when writing and reading files, check whether there is any damage, if damage is found during startup, stop startup.
rdbchecksum yes
# RDB file name
dbfilename dump.rdb
# RDB file directory
dir ./
Copy the code
Important parameters are listed as follows:
① The save parameter is used to set the RDB persistence condition. When the condition is met, the data will be persisted to the hard disk. The default configuration is as follows:
- Save 900 1: If at least one key value changes within 900 seconds, data is persisted to hard disks.
- Save 300 10: If at least 10 key values change within 300 seconds, data is persisted to hard disks.
- Save 60 10000: If at least 10000 key values change within 60 seconds, data is persisted to hard disks.
(2) rdbcompression parameters
The default value is yes to enable RDB file compression. Redis uses the LZF algorithm to compress files. If you do not want to consume CPU performance for file compression, you can disable this feature, which has the disadvantage of requiring more disk space to save files.
(3) rdbchecksum parameters
Its default value is yes to enable RDB file checking when files are written and read, to check for damage, and to stop the startup if damage is found during startup.
5. Configure query
You can run commands in Redis to query current configuration parameters. The command is in the format of config get XXX. For example, to obtain the storage name of the RDB file, run the config get dbfilename command. The command output is as follows:
config get dir
6. Configure the Settings
You can configure the RDB in either of the following 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: Manually modifying the Redis configuration file takes effect globally, even if the Redis server is restarted, setting parameters will not be lost, while using the command to modify the Redis configuration file will be lost after the Redis server is restarted. However, if you manually modify the Redis configuration file, you need to restart the Redis server immediately. However, you do not need to restart the Redis server by using the command.
Tip: The Redis configuration file is located in the root path of the Redis installation directory. The default name is redis.conf.
7. Restore the RDB file
When the Redis server starts, if the RDB file dump. RDB exists in the root directory of Redis, Redis automatically loads the RDB file to recover persistent data. If there is no dump. RDB file in the root directory, move the dump. RDB file to the root directory of Redis. Check whether the RDB file is loaded when Redis starts, it will show whether the RDB file is loaded, we run the Redis startup command: SRC /redis-server redis.conf, as shown below:
Tip: The Redis server blocks the load of the RDB file until the load is complete.
8. RDB advantages and disadvantages
1) Advantages of RDB
- The content of RDB is binary data, which occupies less memory, is more compact, and is more suitable for backup files.
- RDB is useful for disaster recovery because it is a compact file that can be quickly transferred to a remote server for Redis service recovery.
- RDB can improve the speed of Redis, because the main Redis process will fork() a sub-process to persist data to disk, the main Redis process does not perform disk I/O operations;
- RDB files can be restarted faster than AOF files.
2) Disadvantages of RDB
- Because the RDB can only store data for a certain period of time, if the Redis service is terminated unexpectedly, the Redis data will be lost for a period of time.
- The RDB needs to fork() frequently to persist it on disk using child processes. If the dataset is large, fork() can be time-consuming, and if the dataset is large and CPU performance is poor, it can cause Redis to stop serving the client for milliseconds or even a second.
9. Disable persistence
Disabling persistence can improve the execution efficiency of Redis. If you are not sensitive to data loss, you can disable Redis persistence by running the config set save “” command when connecting to the client, as shown in the following figure:
Nodule 10.
From this article, we can know that RDB persistence can be divided into manual trigger and automatic trigger two ways. Its advantage is that it stores small files and recovers data quickly when Redis starts, but its disadvantage is that it has the risk of data loss. RDB file recovery is also very simple, just put the RDB file in the root directory of Redis, when Redis starts, the data will be automatically loaded and restored.
11. There is
If the CPU usage of the Redis server is too high, what is the possible cause? Feel free to leave your answers in the comments section.
12. Reference & Acknowledgements
Redis. IO/switchable viewer/pers…
Blog.csdn.net/qq_36318234…
www.cnblogs.com/ysocean/p/9…
www.cnblogs.com/wdliu/p/937…
Follow the qr code below and subscribe for more exciting content.