I. Overview of Redis
Redis is an open source, high-performance key-value pair database that is widely used in Web applications, caching, queues, counters, real-time messaging, and other scenarios with the following characteristics:
- Redis databases are based on in-memory storage and are therefore very fast to read and write to.
- Redis provides support for a variety of data structures, including strings, hash tables, lists, collections, etc., which is highly scalable.
- Redis can write data to disk for persistent storage to ensure that data is not lost.
- Redis supports advanced features such as transactions, publish/subscribe, and more.
In Redis, data can be divided into two categories, in-memory data and disk data. In-memory data refers to the data stored in memory in Redis, while disk data refers to the persistence mechanism that writes the data in memory to a file on disk to ensure that the data remains after the server is restarted.Redis provides two different methods of persistence, RDB, which is a snapshot backup mechanism, and AOF, which is a mechanism for appending write operations.
Redis persistent storage methods
Redis provides two types of persistent storage, RDB and AOF.
1. RDB
RDB is a snapshot backup mechanism provided by Redis that periodically writes in-memory data to disk in the form of snapshots. When you need to restore data, Redis simply reads the most recently saved snapshot file from disk and restores it.RDB stores Redis data at a certain point in time, so it is very fast to restore. However, since the data is only backed up at regular intervals, some data may be lost.
The RDB is stored as follows:
1) Redis creates a child process in memory that is responsible for writing data from memory to disk.
2) The Redis master process sends a dump command to the child process, commanding the child process to write the data in memory to disk.
3) When a child process writes data, it writes the data in memory to a temporary file before renaming the temporary file to an RDB file.
4) When it's time to restore data, Redis simply reads the most recently saved RDB file and loads it into memory.
RDB stores data in a format similar to a binary representation, and the storage includes information such as data type, key name, expiration time, etc. Its advantage is that it is fast and controllable, and its disadvantage is that some of the data may be lost.
2. AOF
AOF (Append Only File) is another kind of persistent storage provided by Redis, which realizes data persistence by recording all write operations. When you need to recover data, Redis only needs to execute the operations in the AOF file in order. the AOF file records all the write operations of Redis, so the accuracy and integrity of data recovery is higher. However, since each write operation is appended to the AOF file, the size of the AOF file is much larger than the RDB file.
AOF is stored as follows:
1) Redis creates a buffer in memory to store data related to write operations.
2) When a write operation is written to the buffer, Redis asynchronously appends it to the AOF file.
(3) When it is necessary to restore the data, Redis only needs to read the AOF file and perform the write operations in it in order.
AOF files are stored in text format, and each write operation is recorded in the file as a command. You can set different AOF policies to control the size of the AOF file and the frequency of flushing the disk.The advantage of AOF storage is good data integrity, the disadvantage is the relatively slow recovery speed, and the size of the AOF file may be very large.
Third, the advantages and disadvantages of Redis persistent storage methods
1. Advantages and disadvantages of RDB
The RDB stores a snapshot of Redis data at a given point in time. When Redis receives a SAVE or BGSAVE command (the BGSAVE command allows for asynchronous backups), it traverses the in-memory data in a thread and writes all the data and its corresponding key names to an RDB file, generating a snapshot file of the data.The advantages and disadvantages of RDB are as follows:
Pros:
- An RDB file is a binary file that is relatively small in size and takes up few memory resources.
- Data recovery is fast because only the most recently saved RDB file needs to be read during recovery.
- Ideal for use in more frequent scenarios such as backups and restores.
Drawbacks:
- Since the RDB is backed up at intervals, there may be a problem with some of the data being lost.
- When data volumes are large, performing RDB backups may have a performance impact.
2. Advantages and disadvantages of AOF
AOF records all write operations in text format in an AOF file, which ensures the integrity and accuracy of the data. When Redis restarts, you only need to execute the commands in the AOF file to recover the data.The advantages and disadvantages of AOF are as follows:
Pros:
- Data integrity is good because all write operations are recorded.
- Write-before-read, where commands in the write buffer are written to disk asynchronously, improves Redis performance.
- AOF backups are more stable and reliable than RDB backups when the data volume is large.
Drawbacks:
- AOF files are stored in text format, so they are relatively large and take up more memory resources.
- Data recovery is relatively slow because all the commands in the AOF file need to be executed sequentially for data recovery.
- In some of the more specialized scenarios, there may be problems such as confusion of data.
Comparison of RDB and AOF
The following table compares the advantages and disadvantages of RDB and AOF in terms of reliability, performance, and storage space:
specificities | RDB | AOF |
---|---|---|
Data reliability | Backing up data at some point in time, data may be lost. It is the final consistency model that is suitable for high-speed write, low-speed query scenarios | Records all write operations with good data integrity. Is a strong consistency model suitable for frequent read and write scenarios |
storage space | RDB files are smaller in size and take up less memory resources. | AOF files are stored in text format and are relatively large. |
performance comparison | RDB backup is fast; data recovery is fast. Suitable for backup and recovery of frequent scenarios. | AOF writes to the cache faster and with better performance; data recovery is relatively slow. |
Usage Scenarios | It is suitable for scenarios where regular backups are required and data recovery speed is required to be fast. | It is suitable for scenarios that require high data integrity and frequent reading and writing. |
V. Summary
Redis provides two persistent storage methods, RDB and AOF, each with its own advantages and disadvantages, so you need to choose according to the actual situation.
If the frequency of reading and writing in the system is relatively balanced and the requirements for data integrity are relatively high, the AOF method can be used; if the amount of data is large and needs to be backed up within a certain time interval, it is recommended to use the RDB method for backup.
At the same time, you can also set different policies to control the size of the AOF file and RDB file, swipe frequency and other parameters, so as to better ensure the performance and reliability of the Redis database.
This article on Redis persistence RDB and AOF principles and advantages and disadvantages of the article is introduced to this, more related Redis persistence RDB and AOF content, please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!