In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 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 Redis persistence mechanism, 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 learn about it!
Redis stores the data in memory, and the data is lost when the process exits. The Redis persistence mechanism stores data in memory to disk, which can be read from disk files and loaded into memory when rebooted.
Redis supports two persistence mechanisms: full mirror RDB and incremental persistence AOF.
RDB is a snapshot of Redis that stores all unexpired key-value pairs in Redis.
Configure RDB in redis.conf:
Dbfilename dump.rdbdir / var/lib/redissave 900 1 save 300 10 save 60 10000 save "" stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes
Each time the redis process starts, it first checks whether the rdb file exists, and if so, loads the contents of the file into memory.
When redis automatically updates the RDB file, it will fork a child process to save the snapshot, and the main process can provide services normally during the save period.
We can also save snapshots through instructions:
Save: save snapshots in a blocked way, during which redis cannot process other requests
Bgsave: fork A child process completes the save work, and the normal service of redis will not be affected during the save period.
The lastsave command gets the latest RDB file creation timestamp, which can be used to check whether the save was successful.
As a database snapshot, RDB needs to write the entire database to a file each time it is created. This is a very time-consuming operation, so it is difficult to do it frequently, and a large amount of data may be lost in the event of an exception.
Redis provides an incremental persistence tool, AOF (Append Only ile), which AOF persists by recording all write instructions in the Redis database. Instructions are stored in the AOF file in the format of the Redis communication protocol.
When the Redis process starts, it checks whether the AOF file exists, and if so, executes the instructions in AOF to recover the data.
Each time Redis executes a write instruction, a log is added to the AOF file, but new records are not immediately written to disk (fsync) but are cached in the write buffer.
We can configure the policy of writing the data in the buffer to disk to avoid data loss.
Writing data in the buffer to disk is a time-consuming operation, frequently writing to disk can affect performance, but Redis crashes also lose less data, so we need to make a tradeoff according to the application scenario.
Redundant instructions may be recorded in AOF, and if we execute set instructions on the same key 100 times, there will be 100 records in the AOF file, but only the last set instruction can be retained to recover the data. AOF rewriting cleans up AOF files to clean up unnecessary instruction logs (such as deleting overwritten set directives) and reduces the size of AOF files.
Redis adopts the strategy of background rewriting, that is, a child process of fork writes the sorted AOF to a temporary file. Background rewrite operations can be triggered manually using BGREWRITEAOF.
In fact, AOF rewriting will not read the original AOF file, and the child process will take a copy of the current data and generate a new AOF file directly from that copy.
During the rewrite, the main process writes the new writes to the original AOF file and the AOF rewrite cache, and even if the rewrite fails, the original AOF file still saves the complete data. When the child process completes the AOF rewrite, it will send a signal to the main process. After receiving the signal, the main process will write the contents of the AOF rewrite cache to a new AOF file, and then overwrite the original file with the new AOF file.
Configure AOF in redis.conf:
Appendonly yes appendfilename appendonly.aof appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb
Redis records the size of the AOF file at startup or after the last rewrite, and triggers the rewrite if the new data reaches 100% of the original size (auto-aof-rewrite-percentage configuration).
The rewrite operation is performed only if the current AOF file size is larger than auto-aof-rewrite-min-size, otherwise the rewrite will not be performed even if the amount of new data exceeds the specified percentage. This avoids the problem that the original file is too small, which leads to frequent rewriting at the initial stage.
The above is all the content of the article "sample Analysis of Redis persistence Mechanism". 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.