In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Today's editor to share with you is about Redis persistence snapshot way (RDB) introduction, many people do not understand, today editor in order to let you know more about Redis persistence, so to sum up the following content, let's look down. I'm sure you'll get something.
Redis reads and writes in memory, so its performance is high, but the data in memory will be lost with the restart of the server. In order to ensure that the data in memory is not lost, we need to store the data in memory to disk so that when Redis restarts, it can recover the original data from disk, and 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 capabilities.
1. Several ways of persistence
Redis persistence has the following three ways:
Snapshot mode (RDB, Redis DataBase) writes memory data at a certain time to disk in a binary way; file appending mode (AOF, Append Only File) records all operation commands and appends them to the file in the form of text Mixed persistence is a new way after Redis 4.0. mixed persistence combines the advantages of RDB and AOF. When writing, the current data is written to the beginning of the file in the form of RDB, and then the subsequent operation commands are stored in the file in AOF format, which not only ensures the speed of Redis restart, but also simplifies the risk of data loss.
Since each persistence scheme has a specific usage scenario, let's start with RDB persistence.
Introduction to 2.RDB
RDB (Redis DataBase) is the process of writing a memory snapshot (Snapshot) at a certain time to disk in a binary way.
3. Persistence trigger
There are two types of persistent triggers for RDB: one is manual trigger, the other is automatic trigger.
1) manually triggered
There are two operations that trigger persistence manually: save and bgsave. The main difference between them is whether or not to block the execution of the main thread of Redis.
① save command
Executing the save command in the client will trigger the persistence of the Redis, but it will also make the Redis in a blocking state, and will not respond to commands from other clients until the RDB persistence is completed, so you must use it carefully in the production environment.
The save command is used as follows:
As you can see from the picture, when the save command is executed, the modification time of the persistence file dump.rdb changes, which means that save successfully triggers RDB persistence.
The save command execution process is shown in the following figure:
② bgsave command
Bgsave (background save) means to save in the background, the biggest difference between it and the save command is that bgsave will fork () a child process to perform persistence, the whole process only in the fork () child process when there is a temporary block, when the child process is created, the main process of Redis can respond to requests from other clients, compared to the entire process blocking save command, obviously the bgsave command is more suitable for us to use.
The bgsave command is used, as shown in the following figure:
Bgsave executes the process, as shown in the following figure:
2) automatic trigger
After talking about the manual trigger method of RDB, let's take a look at 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 within m seconds.
The parameters m and n can be found in the Redis configuration file. For example, save 601 indicates that RDB persistence will be triggered if at least one key changes within 60 seconds.
Automatically trigger persistence, in essence, Redis through judgment, if the set trigger conditions are met, automatically execute a bgsave command.
Note: when multiple save m n commands are set, persistence will be triggered if any of the conditions are met.
For example, we set up the following two save m n commands:
Save 60 10save 600 1
Persistence will be triggered if the Redis key value is changed 10 times within 60s; if the Redis key value is changed less than 10 times within 60s, then Redis will determine whether the Redis key value has been modified at least once within 600 seconds, and if it is satisfied, persistence will be triggered.
② flushall
The flushall command is used to empty the Redis database and must be used with caution in the production environment. When Redis executes the flushall command, it will trigger automatic persistence and empty the RDB file.
The execution result is shown in the following figure:
③ master-slave synchronous trigger
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
A reasonable configuration of RDB can ensure the efficient and stable operation of Redis. Let's take a look at what are the configuration items of RDB?
RDB configuration parameters can be found in the configuration file of Redis, as shown below:
If the condition save 900 1save 300 10save 60 1000 bgsave saved by # RDB fails, whether to stop persisting data to disk, yes means to stop persistence, and no means to ignore the error and continue to write the file. Stop-writes-on-bgsave-error yes# RDB File Compression rdbcompression yes# whether to turn on RDB file inspection when writing and reading files, check for corruption, and stop startup if corruption is found during startup. Rdbchecksum yes# RDB file name dbfilename dump.rdb# RDB file directory dir. /
The more important parameters are listed as follows:
① save parameters
It is used to configure the parameters that trigger the RDB persistence condition, and the data will be persisted to the hard disk when the save condition is met.
The default configuration is described as follows:
Save 9001: means that if there is at least one key value change in 900s, the data will be persisted to the hard disk; save 30010: if at least 10 key values change in 300s, the data will be persisted to the hard disk; save 60 10000: means that if at least 10000 key values change in 60 seconds, the data will be persisted to the hard disk.
② rdbcompression parameters
Its default value is yes, which means that 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 feature to be turned off, which has the disadvantage of requiring more disk space to save the file.
③ rdbchecksum parameters
Its default value is yes, which means whether the RDB file check is turned on when writing and reading the file, check for corruption, and stop startup if it is found to be damaged during startup.
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 settings of the RDB file, you can use config get dbfilename. The execution effect is as follows:
To query the file directory of RDB, you can use the command config get dir. The execution effect is as follows:
6. Configure settin
You can configure RDB in the following two ways:
Manually modify the Redis configuration file; use command line settings, such as config set dir "/ usr/data", to modify the storage directory of RDB.
Note: the manual modification of the Redis configuration file takes effect globally, that is, the setting parameters for restarting the Redis server will not be lost, while using the command modification will be lost after the Redis restart. However, manually modifying the Redis configuration file requires a restart of the Redis server for it to take effect immediately, while the command does not require a restart of the Redis server.
Tip: Redis's 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 exists in the Redis root directory, dump.rdb,Redis will automatically load the RDB file to recover the persistent data.
If the root directory does not have a dump.rdb file, first move the dump.rdb file to the root directory of Redis.
Verify that the RDB file is loaded
Redis has log information during 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 log, the Redis service has loaded the RDB file normally when it starts.
Tip: the Redis server will remain blocked during the loading of the RDB file until the loading is complete. Advantages and disadvantages of 8.RDB 1) the advantages and disadvantages of RDB RDB are binary data, which takes up less memory and is 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 greatly improve the running speed of Redis, because each persistence, the Redis main process will fork () a child process to persist the data to disk, and the Redis main process will not perform operations such as disk Imando O; compared with AOF files, RDB files can be restarted faster. 2) the disadvantage of RDB is that RDB can only store data at a certain time interval. If the Redis service is terminated unexpectedly, Redis data will be lost for a certain period of time. RDB needs frequent fork () 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 may 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 executing the config set save "" command when connecting to the client, as shown in the following figure:
10. Summary
Through this article, we can know that RDB persistence can be divided into manual trigger and automatic trigger. Its advantage is that the storage file is small, the recovery of data is relatively fast when Redis starts, and the disadvantage is the risk of data loss. The recovery of RDB files is also simple, as long as you put the RDB file in the root directory of Redis, and the data will be loaded and restored automatically when Redis starts.
The above is a brief introduction to the snapshot method (RDB) in Redis persistence. Of course, you have to understand the differences in the detailed use of the above. If you want to know more, welcome to follow the industry information channel!
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
© 2024 shulou.com SLNews company. All rights reserved.