In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what are the ways to persist Redis. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.
Redis reads and writes are in memory, so its performance is high, but the data in memory will be lost with the server restart, in order to ensure that the data is not lost, we need to store the data in memory to disk, so that Redis can recover the original data from disk when restarting, and the whole process is called Redis persistence.
Redis persistence is also one of the main differences between Redis and Memcached, as Memcached does not have persistence.
1. There are several ways to persist.
Redis persistence has three ways:
Snapshot mode (RDB, Redis DataBase) writes memory data at a certain time to disk in binary mode;
Append Only File (AOF), which records all operation commands and appends them to the file in the form of text;
Hybrid persistence mode, a new mode after Redis 4.0, hybrid persistence is a combination of the advantages of RDB and AOF. When writing, the current data is first written to 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 can not only ensure the speed of Redis restart, but also simplify the risk of data loss.
Since each persistence scheme has a specific usage scenario, let's start with RDB persistence.
2. Introduction to RDB
RDB (Redis DataBase) is the process of writing a snapshot of memory at a certain time to disk in binary form.
3. persistence trigger
There are two types of persistence triggers for RDBs: manual triggers and automatic triggers.
1) Manual trigger
There are two operations that trigger persistence manually: save and bgsave. The main difference between them is whether to block the execution of the Redis main thread.
The save command
Executing the save command in the client will trigger the persistence of Redis, but at the same time it will make Redis in a blocking state until the RDB persistence is completed, and will not respond to commands sent by other clients, so it must be used with caution in the production environment.
The save command is used as follows:
As can be seen from the picture, when the save command is executed, the modification time of the persistent file dump.rdb changes, which means that the successful save triggers the RDB persistence.
The save command execution process is shown in the following figure:
bgsave command
bgsave (background save) means background save. The biggest difference between bgsave and save command is that bgsave forks () a child process to perform persistence. During the whole process, there is only a short block in the fork() child process. After the child process is created, the main process of Redis can respond to requests from other clients. Compared to the save command that blocks the whole process, obviously bgsave command is more suitable for us to use.
The bgsave command is used, as shown in the following figure:
bgsave execution process, as shown in the following figure:
2) Automatic trigger
Having finished the manual trigger mode of RDB, let's see how to trigger RDB persistence automatically.
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.
The parameters m and n can be found in the Redis configuration file; for example, save 60 1 indicates that at least one key has changed within 60 seconds, triggering RDB persistence.
Automatic trigger persistence, the essence is that Redis judges, if the set trigger conditions are met, automatically executes a bgsave command.
Note: When multiple save m n commands are set, any one of these conditions triggers persistence.
For example, we set up the following two save m n commands:
save 60 10save 600 1
If there are 10 changes in Redis key within 60s, persistence will be triggered; if the number of changes in Redis key within 60s is less than 10, Redis will determine whether Redis key has been modified at least once within 600s, and persistence will be triggered if satisfied.
② flushall
Flushall command is used to empty Redis database. It must be used with caution in production environment. When Redis executes flushall command, it will trigger automatic persistence and empty RDB file.
The execution results are shown in the following figure:
③ Master-slave synchronous trigger
In Redis master-slave replication, when the slave performs a full replication operation, the master executes the bgsave command and sends the RDB file to the slave, which automatically triggers Redis persistence.
4. configuration instructions
Reasonable setting of RDB configuration can ensure efficient and stable operation of Redis. Let's take a look at the configuration items of RDB together.
The RDB configuration parameters can be found in the Redis configuration file, as follows:
# RDB save condition save 900 1save 300 10save 60 10000# bgSave failed, whether to stop persisting data to disk, yes means to stop persisting, no means to ignore errors and continue writing files. stop-writes-on-bgsave-error yes# RDB file compression rdbcompression yes#Whether to open RDB file checking when writing and reading files, check for corruption, if corruption is found during startup, stop startup. rdbchecksum yes# RDB filename dbfilename dump.rdb# RDB file directory dir ./
The most important parameters are listed below:
① Save parameter
It is used to configure the parameters that trigger RDB persistence conditions, which will persist the data to hard disk when the preservation conditions are met.
The default configuration is described as follows:
save 900 1: Save 300 10 means that if at least 10 key values change within 300 seconds, the data will be persisted to the hard disk;save 60 10000 means that if at least 10000 key values change within 60 seconds, the data will be persisted to the hard disk.
② rdbcompression parameter
Its default value is yes to enable RDB file compression, Redis will use the LZF algorithm for compression. If you don't want to consume CPU power for file compression, you can set it to turn off. The disadvantage is that it requires more disk space to save files.
③ rdbchecksum parameter
Its default value of yes indicates whether RDB file checking is enabled when writing files and reading files, checking for corruption, and stopping the startup if corruption is found during startup.
5. configuration query
In Redis, you can query the current configuration parameters using the command. 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 following figure:
To query the file directory of RDB, you can use the command config get dir. The execution effect is as shown in the following figure:
6. configuration settings
RDB configuration can be set in two ways:
● Manually modify Redis profiles;
● Use command-line settings, e.g. config set dir "/usr/data" is the storage directory used to modify the RDB.
Note: Manual modification of Redis configuration file is globally effective, that is, restart Redis server settings parameters will not be lost, and use command modification, after Redis restart will be lost. However, manually modifying the Redis configuration file requires restarting the Redis server to take effect immediately, while the command does not require restarting the Redis server.
Tip: The Redis configuration file is located at the root of the Redis installation directory and is called redis.conf by default.
7. RDB File Recovery
When Redis server starts, if RDB file dump.rdb exists in Redis root directory, Redis will automatically load RDB file to recover persistent data.
If there is no dump.rdb file in the root directory, first move the dump.rdb file to the root directory of Redis.
Verify that the RDB file is loaded
Redis has log information at startup, which shows whether the RDB file is loaded. We execute the Redis startup command: src/redis-server redis.conf, as shown in the following figure:
As you can see from the logs, the Redis service has loaded the RDB file properly at startup.
Tip: The Redis server blocks during the loading of the RDB file until the load is complete.
8. Advantages and disadvantages of RDB
1) Advantages of RDB
● The content of RDB is binary data, occupying less memory, more compact, and more suitable for backup files;
● RDB is very useful for disaster recovery. It is a compact file that can be transferred to a remote server for Redis service recovery faster.
● RDB can improve the running speed of Redis to a greater extent, because each time the main process of Redis will fork() a child process to persist data to disk, the main process of Redis will not perform disk I/O operations;
● RDB files can be restarted faster than AOF files.
2) Disadvantages of RDB
● Because RDB can only store data for a certain time interval, if Redis service is unexpectedly terminated midway, Redis data will be lost for a period of time;
RDBs often need to fork() to persist them on disk using child processes. Fork() can be time-consuming if the dataset is large, and can cause Redis to stop serving clients for milliseconds or even seconds if the dataset is large and CPU performance is poor.
9. Disable persistence
Disabling persistence can improve the execution efficiency of Redis. If it is not sensitive to data loss, you can disable persistence of Redis by executing config set save "" command in the case of connecting clients, as shown in the following figure:
10. summary
Through this article we can learn that RDB persistence is divided into manual trigger and automatic trigger two ways, its advantage is that the storage file is small, Redis starts to recover data faster, the disadvantage is the risk of losing data. The recovery of RDB files is also very simple, just need to put the RDB files into the root directory of Redis, Redis will automatically load and recover the data when it starts.
About the way Redis persistence is shared here, I hope the above content can be of some help to everyone and learn more knowledge. If you think the article is good, you can share it so that more people can see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un