In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail what persistence methods there are in Redis, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
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 append method (AOF, Append Only File), record all operation commands and append 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 10
Save 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:
After the condition save 9001 save 30010 save 60 10000 # 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 # turn on RDB file check 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 at least one key value changes within 900s, the data will be persisted to the hard disk
Save 30010: 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 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:
Config set dir "/ usr/data"
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) advantages of RDB
The content of RDB is binary data, which takes up less memory and is more compact, so it 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 faster for Redis service recovery.
RDB can greatly improve the running speed of Redis, because each persistence, the Redis main process will fork () a child process, and the data will be persisted to disk. The Redis main process will not perform operations such as disk Ibank O.
RDB files can be restarted faster than AOF files.
2) shortcomings of RDB
Because RDB can only save data at a certain time interval, if the Redis service is terminated unexpectedly, the Redis data will be lost for a period of time.
RDB requires 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:
So much for sharing about the persistence methods in Redis. I hope the above content can be helpful to you and you can learn more. If you think the article is good, you can share it for more people to see.
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: 221
*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.