Redis persistence mechanism principle & how to achieve

Redis persistence mechanism

Concept: The data in the memory is synchronized to the hard disk through the persistence mechanism (RDB, AOF) to ensure data persistence; when redis is restarted, the hard disk files are reloaded into the memory to achieve the purpose of data recovery.

Realization: Create a child process of fork separately, copy the database of the parent process to the memory of the child process, and then write it to a temporary file by the child process. After persistence is over, replace the last snapshot file with this temporary file, and then The child process exits and the memory is released.


RDB ( Redis DataBase ): rdb is the default persistence method of redis. According to a certain cycle strategy, the data in the memory is saved to the hard disk in a snapshot mode (binary file), that is, Snapshot snapshot storage. The corresponding dump.rdb data file will be generated. The save parameter in the configuration file determines the period of the snapshot

image

advantage:

1. Suitable for large-scale data recovery!

2. The integrity of the data is not high!

Disadvantages:

1. A certain time interval is required for process operation! If redis crashes unexpectedly, the last modified data will be gone!

2. When the fork process, it will occupy a certain content space!


AOF ( Append Only File ): Redis will append each received command to the end of the file through the write function, similar to MySQL binlog. When redis restarts, it will re-execute the write command saved in the file to rebuild the content of the entire database in memory.

mysql-binlog is the binary log of the MySQL database. It is used to record the SQL statements (except data query statements) that users operate on the database. You can use the mysqlbin command to view the contents of the binary log.

image

Tips:

  • When the two methods are turned on at the same time, data recovery redis will give priority to AOF recovery
  • If there is an error in the aof file, does redis fail to start at this time? We need to repair the aof file. Redis provides us with a tool redis-check-aof --fix

advantage:

1. Every modification is synchronized, and the integrity of the file will be even better!

appendfsync always # 每次修改都会 sync。消耗性能 

2. Synchronize once per second, you may lose one second of data

appendfsync everysec # 每秒执行一次 sync,可能会丢失这1s的数据! # 

3. Never synchronize, the most efficient!

appendfsync no # 不执行 sync,这个时候操作系统自己同步数据,速度最快!

Disadvantages:
1. Compared with data files, AOF is much larger than RDB, and the repair speed is also slower than RDB!

2. The operating efficiency of Aof is also slower than that of rdb, so our default configuration for redis is RDB persistence


Guess you like

Origin blog.csdn.net/weixin_45496190/article/details/108166320