In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you the example analysis of RDB persistence in Redis, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Introduction to RDB
RDB is a way for Redis to persist by writing a snapshot of the dataset currently in memory to disk, that is, the Snapshot snapshot (all key-value pairs in the database). When restoring, the snapshot file is read directly into memory.
2. Trigger mode
There are two ways to trigger RDB, which are automatic trigger and manual trigger.
①, automatic trigger
Under SNAPSHOTTING in the redis.conf configuration file, we introduced it in this article.
①, save: this is used to configure the RDB persistence condition that triggers Redis, that is, when the data in memory is saved to the hard disk. Such as "save m n". Indicates that bgsave is automatically triggered when there are n modifications to the dataset within m seconds (this command is described below, the command that manually triggers RDB persistence)
The default configuration is as follows:
Save 9001: if there is a change in the value of at least one key in 900s, save save 30010: if there is a change in the value of at least 10 key in 300s, save save 60 10000: it means that if there is a change in the value of at least 10000 key in 60 seconds, save it
Of course, if you only use Redis caching without persistence, you can comment out all save lines to disable the save feature. Deactivation can be achieved by directly using an empty string: save ""
②, stop-writes-on-bgsave-error: the default value is yes. Whether Redis stops receiving data when RDB is enabled and the last time the data is saved in the background fails. This will make the user realize that the data is not correctly persisted to disk, otherwise no one will notice that a disaster has occurred. If Redis is restarted, you can start receiving data again.
③, rdbcompression; default is yes. For snapshots stored on disk, you can set whether to compress storage. If so, redis uses the LZF algorithm for compression. If you don't want to consume CPU for compression, you can set this feature to off, but the snapshots stored on disk will be larger.
④, rdbchecksum: the default value is yes. "after storing the snapshot, we can also have redis use the CRC64 algorithm for data validation, but doing so increases performance consumption by about 10%, which can be turned off if you want to get the maximum performance improvement."
⑤, dbfilename: sets the file name of the snapshot. Default is dump.rdb.
⑥, dir: set the path where snapshot files are stored. This configuration item must be a directory, not a file name. The default is saved in the same directory as the current configuration file.
In other words, through the save method configured in the configuration file, RDB persistence will be carried out when the actual operation meets the configuration form, and the current memory snapshot will be saved in the directory of the dir configuration, and the file name is determined by the configured dbfilename.
②, manual trigger
There are two kinds of commands that manually trigger Redis for RDB persistence:
1 、 save
This command blocks the current Redis server, and during the execution of the save command, Redis cannot process other commands until the RDB process is complete.
Obviously, this command can cause long-term blocking for instances with large memory, which is a fatal flaw, and Redis provides a second way to solve this problem.
2 、 bgsave
When executing this command, Redis performs snapshot operations asynchronously in the background, and snapshots can also respond to client requests. The specific operation is that the Redis process executes the fork operation to create the child process, and the RDB persistence process is responsible for the child process, which ends automatically after completion. Blocking occurs only in the fork phase, usually for a short time.
Basically all RDB operations within Redis use the bgsave command.
Ps: executing the flushall command will also generate a dump.rdb file, but it is empty and meaningless
3. Restore data
Just move the backup file (dump.rdb) to the redis installation directory and start the service, and redis will automatically load the file data into memory. The Redis server will remain blocked during the loading of the RDB file until the loading is complete.
To get the installation directory of redis, you can use the config get dir command
4. Stop RDB persistence
In some cases, we just want to take advantage of Redis's caching, not Redis's persistence, so we'd better stop RDB persistence at this time. You can disable the save function by commenting out all the save lines in the configuration file redis.conf as mentioned above, or simply an empty string to deactivate: save ""
You can also use the command:
one
Redis-cli config set save ""
5. Advantages and disadvantages of RDB
①, advantage
1.RDB is a very compact file that holds the dataset of redis at some point in time. This kind of file is ideal for backup and disaster recovery.
two。 When the RDB file is generated, the fork main process will fork () a child process to handle all the save work, and the main process does not need to do any disk IO operation.
3.RDB recovers large datasets faster than AOF does.
②, disadvantage
1. RDB data cannot be persisted in real time / seconds. Because every time bgsave runs, it has to perform the fork operation to create a child process, which is a heavyweight operation (a copy of the data in memory is cloned, approximately 2 times the expansibility needs to be considered), and the cost of frequent execution is too high (affecting performance).
2. RDB files are saved in a specific binary format. During the evolution of Redis versions, there are multiple RDB versions of formats. There is a problem that the old version of Redis service is not compatible with the new version of RDB format (the version is not compatible).
3. Make a backup at regular intervals, so if redis accidentally down, all changes since the last snapshot will be lost (data is lost)
6. The principle of RDB automatic saving.
Redis has a server status structure:
one
two
three
four
five
six
seven
eight
nine
Struct redisService {
Struct saveparam * saveparams
Long long dirty
Time_t lastsave
}
①, first look at the array saveparam that records the save conditions, where each element is a saveparams structure:
one
two
three
four
five
six
Struct saveparam {
Time_t seconds
Int changes
}
We configured save in the redis.conf configuration file earlier:
one
two
three
Save 9001: means that if the value of at least one key changes within 900s, save it.
Save 30010: means that if the value of at least 10 key changes within 300 seconds, save
Save 60 10000: indicates that if the value of at least 10000 key changes within 60 seconds, save
Then the saveparam array in the server state will look like this:
②, dirty counters, and lastsave properties
The dirty counter records how many modifications (including writes, deletions, updates, etc.) have been made by the Redis server since the last successful execution of the save command or bgsave command.
The lastsave attribute is a timestamp that records the last time a save command or bgsave command was successfully executed.
With these two commands, when the server successfully performs a modify operation, then the dirty counter will be incremented by 1, and the lastsave property records the time when the last save or bgsave was executed, and the Redis server also has a periodic operation function severCron, which is executed every 100ms by default, which traverses and checks all the save conditions in the saveparams array, and executes the bgsave command as long as a condition is met.
After the execution is complete, the dirty counter is updated to 0, and the lastsave is updated to the completion time of the execution command.
The above is all the content of the article "sample Analysis of RDB persistence in Redis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.