Redis persistence mechanism (detailed version)

1 Introduction

The persistence mechanism of Redis mainly includes RDB (Redis DataBase, snapshot) and AOF (Append Only File, additional file).

2.RDB

1.RDB

RDB is the default persistence method of Redis . It will generate a snapshot file of the data in memory within a specified time interval and store it on the disk. When generating a snapshot, you can choose two methods, one is to fork a child process to take a snapshot, and the other is to enable the RDB snapshot compression function, compress the snapshot, and then store it.

You can set the RDB persistence cycle and the name of the backup file in the Redis configuration file.

2 advantages

  1. The RDB snapshot file is very compact, and the data can be saved on the disk, occupying a relatively small space.

  2. The RDB file contains all the data, which can easily back up and restore Redis data , and when Redis starts, you can quickly restore the data by loading the RDB file, shortening the startup time of Redis.

3. Disadvantages

  1. RDB saves data at long intervals. If Redis crashes before saving the snapshot, all data modified after the snapshot will be lost.

  2. Generating RDB files consumes CPU and memory resources. When the amount of data is large, it may have a certain impact on the performance of the Redis server.

3.AOF

1.AOF

AOF is a log-like persistence method provided by Redis . When Redis persists data, every command written to Redis will be appended to the end of the AOF file, so that even if Redis crashes, AOF can be re-executed commands in the file to restore the data.

There are two modes for updating AOF files:

  1. every write

The AOF file will be updated every time a command is written. The advantage of this method is that it has high reliability and data is not easy to lose, but it may reduce the performance of Redis.

  1. regular updates

In a certain time interval, the write command of the Redis server is saved to the AOF file at one time. The advantage of this method is that the volume of the AOF file can be reduced when there are many write commands, but because the storage is not real-time data, so the reliability is not as good as every write.

You can set the update mode, update frequency and file name of AOF persistence in the Redis configuration file.

2 advantages

  1. AOF can achieve fine-grained data recovery , and only needs to restore from the latest persistence.

  2. When the AOF file is persisted, it will not block the execution of Redis commands , and the persistence can be achieved without affecting the user's use.

  3. AOF provides a variety of strategies for updating AOF files , which can be optimized according to business needs.

3. Disadvantages

  1. Every write command will update the AOF file, which may affect the performance of Redis .

  2. AOF files are usually larger than RDB files and take up more disk space , but the file size can be reduced by compressing the AOF file.

  3. If the update method is "write every time", the AOF file will become very large when the amount of data is large, and storage and backup will become complicated and time-consuming.

4. Summary

There will be an interview, if asked, it will be basically cold.
Have you learned in front of the screen?

Guess you like

Origin blog.csdn.net/m0_62600503/article/details/130867514