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 commandsSAVEAnd background RDB persistence commandsBGSAVEIn 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. throughodCommands 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 shortAppen 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 formataof_bufEnd of buffer.
  • The file is written to: Redis, which is called before ending an event loopflushAppendOnlyFileFunction,aof_bufThe contents of the buffer are written to the AOF file.
  • File synchronization: File synchronizationsyncThis 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 periodicallyBGREWRITEAOFRewrite, reduce repeated commands, invalid commands, merge commands, etc.
  • AOF file support background rewrite, usingforkFormal 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

  1. The file size is small, suitable for copying for cold backup
  2. Compared with AOF, backup recovery is faster

The disadvantage of RDB

  1. Lost data
  2. Fork the child process to do itBGSAVE, which consumes certain memory resources

The advantages of AOF

  1. Less data loss
  2. Increased write buffer, no addressing, fast
  3. Append-only, also does not need to do disk addressing, high efficiency

The disadvantage of AOF

  1. Large file size
  2. AOF has to do a write every timeaof_bufWhen 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.

  1. Stored content: THE RDB stores data at a point in time; AOF stores the write commands executed.
  2. File size: RDB file is small; The AOF file is large.
  3. 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.
  4. 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

  1. Redis Design and Implementation, by Huang Jianhong.
  2. www.litreily.top/2018/10/25/…