In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "what are the two persistence methods in Redis". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the two persistence methods in Redis.
Two persistence modes of Redis
As we all know, there are two kinds of AOF,RDB persistence in Redis, so let's briefly review it first.
RDB persistence
RDB persistence is to save the state of the database at the current point in time to disk, also known as snapshot persistence.
RDB can be triggered manually or executed periodically depending on the server configuration.
The file generated by RDB is a compressed binary file through which the database can be restored to the state at that point in time.
Redis provides the foreground RDB persistence command SAVE and the background RDB persistence command BGSAVE. Other Redis commands will be blocked when executed in the foreground, and Redis can continue to process client command requests when executed in the background.
In the RDB binary file, what is saved is the key-value pair data, using compressed custom coding with check. It can be converted to readable through the od command.
For master-slave replication, the RDB file is used for the initialization of full replication.
[related recommendation: Redis video tutorial]
AOF persistence
AOF persistence, whose full name is Appen Only File, means additional persistence, in which write commands, not data, are saved.
The process of AOF persistence is divided into three steps: command appending, file writing and file synchronization.
Command append: every time the Redis server executes a write command, it appends the write command to the end of the aof_buf buffer in the server state in AOF protocol format.
File write: in Redis, before each event loop ends, the flushAppendOnlyFile function is called to write the contents of the aof_buf buffer to the AOF file.
File synchronization: synchronization sync refers to whether a file is synchronized directly to disk when it is written to the operating system buffer. Through configuration, you can choose to synchronize immediately, synchronize per second, and be controlled by the operating system without active synchronization. About the file I am O buffer: https://www.litreily.top/2018/10/25/io-cache/
Redis gives priority to the use of AOF files to recover data.
AOF files are larger than RDB files because they store commands and are not compressed.
AOF files can be rewritten with BGREWRITEAOF periodically to reduce repeated commands, invalid commands, merge commands, and so on.
The AOF file supports background rewriting and is implemented in the form of a fork child process. The child process has a copy of the data of the server process, and then avoids the use of locks to ensure data security. In addition, AOF rewrite buffer is used to solve the data inconsistency.
Advantages and disadvantages of two kinds of persistence advantages and disadvantages of RDB
The file is small and suitable for copying and cold backup.
Backup recovery is faster than AOF
Shortcomings of RDB
Lose a lot of data
The BGSAVE child process consumes some memory resources.
Advantages of AOF
Less data loss
Added write buffer, no addressing, high speed
Append-only, no need to do disk addressing, high efficiency
Shortcomings of AOF
The file size is large
AOF needs to write to aof_buf every time. When AOF persistence is enabled, QPS will be slightly reduced.
Why does Redis need two kinds of persistence?
From the above review, we can see that there is a significant difference between RDB and AOF persistence.
What is stored: RDB stores data at a certain point in time; AOF stores write commands executed.
File size: RDB files are smaller; AOF files are larger.
Writing mode: RDB can use foreground / background writing mode; AOF uses the way that every time a write command is executed, the command is stored in the buffer, and it can be rewritten regularly.
Data loss: RDB loses all data from the downtime to the last RDB synchronization; AOF does not lose or lose data for 1 second or several seconds according to the flushing method configured by the Icano buffer.
According to these comparisons, you can see that RDB persistence is more suitable for saving data at a point in time. When master-slave replication or full data remote disaster recovery occurs, copy it to other places. AOF persistence is more suitable as a local backup because of less data loss, and as a failure recovery when Reids hangs up and restarts. This is why I understand why Redis needs two ways of persistence.
At this point, I believe you have a deeper understanding of "what are the two persistence methods in Redis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.