Redis has two types of persistence
As we all know, Redis provides two types of persistence: AOF and RDB. Here is a brief review
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.
- The RDB can be triggered manually or periodically based 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 the point in time.
- Redis provides foreground RDB persistence commands
SAVE
And background RDB persistence commandsBGSAVE
In the foreground, other Redis commands are blocked, while in the background, Redis can continue processing client command requests. - RDB binary files, stored key-value data, using compressed custom encoding, with verification. through
od
Commands can be converted to be readable. - In the case of primary/secondary replication, the RDB file is used for the initial full replication.
AOF persistence
- AOF persistence, for short
Appen Only File
, which means appending persistence in which write commands are held instead of data. - AOF persistence is divided into three steps: command addition, file writing, and file synchronization.
- Command appending: The Redis server appends each write command to the server state in AOF format
aof_buf
End of buffer. - The file is written to: Redis, which is called before ending an event loop
flushAppendOnlyFile
Function,aof_buf
The contents of the buffer are written to the AOF file. - File synchronization: File synchronization
sync
This refers to whether a file is directly synchronized to disk when written to the operating system buffer. You can configure immediate synchronization, synchronization per second, or not active synchronization but controlled by the operating system. About file I/O buffering:www.litreily.top/2018/10/25/… - Redis preferentially uses AOF files to recover data.
- AOF files are larger than RDB files because they store commands and are not compressed.
- AOF files can be adopted periodically
BGREWRITEAOF
Rewrite, reduce repeated commands, invalid commands, merge commands, etc. - AOF file support background rewrite, using
fork
Formal implementation of child processes. The child process has a copy of the server process’s data, which is secured by avoiding locking. In addition, the AOF rewrite buffer was used to resolve data inconsistencies.
The pros and cons of the two types of persistence
The advantages of RDB
- The file size is small, suitable for copying for cold backup
- Compared with AOF, backup recovery is faster
The disadvantage of RDB
- Lost data
- Fork the child process to do it
BGSAVE
, which consumes certain memory resources
The advantages of AOF
- Less data loss
- Increased write buffer, no addressing, fast
- Append-only, also does not need to do disk addressing, high efficiency
The disadvantage of AOF
- Large file size
- AOF has to do a write every time
aof_buf
When AOF persistence is enabled, the QPS will decrease slightly
Why does Redis need two types of persistence?
From the review above, we can see that RDB is clearly different from AOF persistence.
- Stored content: THE RDB stores data at a point in time; AOF stores the write commands executed.
- File size: RDB file is small; The AOF file is large.
- Write mode: RDB can use the foreground/background write mode; AOF saves the command to a buffer each time a write command is executed, and can be rewritten periodically.
- Data loss: the RDB loses all data between the outage and the last RDB synchronization; AOF does not lose or lose 1s or seconds of data depending on the flush mode configured for the I/O buffer.
Based on these comparisons, it can be seen that RDB persistence is more suitable for storing data at a point in time and copying data to other places in the case of primary/secondary replication or full remote DISASTER recovery, while AOF persistence is more suitable for local backup and fault recovery in the case of Reids failure and restart due to less data loss. This is why I understand why Redis needs two types of persistence.
reference
- Redis Design and Implementation, by Huang Jianhong.
- www.litreily.top/2018/10/25/…