Redis persistence operation

  Redis is an in-memory database. If the data in the memory is not stored on the disk, all the data will be lost if the power is cut off. Therefore, Redis provides the persistence function. There are two ways of Redis persistence: RDB and AOF. Next, let’s talk about these two methods

RDB (Redis DataBase)

What is RDB

Insert picture description here
It is probably the above picture. In the specified time interval, the snapshot of the data set in the memory is written to the disk. When it is restored, the snapshot file is directly read into the memory.
Redis will create (fork) a sub-process for persistence. Write the data to a temporary file, and after the end of the belt persistence, replace the last persisted file with this temporary file. During the whole process, the main process does not perform any IO operations, ensuring extremely high performance. If large-scale data recovery is required, and the integrity of the data is not high, the RDB method is more efficient than the AOF method. But RDB also has a shortcoming. If it goes down during the last persistence, data may be lost!

The default save file for RDB is dump.rdb
Insert picture description here

Trigger mechanism
  1. When the save rules are met, the rdb rules will be triggered automatically
  2. Execute fluxhall command
  3. Exit Redis
advantage
  1. Suitable for large-scale data recovery
  2. Low data integrity
Disadvantage
  1. It takes a certain time interval to operate. If the machine is down, the last modified data will be gone
  2. It will take up a certain amount of space when forking the process

AOF (Append Only File)

What is AOF

Insert picture description here
As shown in the figure above: Record each write operation in the form of a log, record all the instructions of the Redis execution process (read operations are not recorded), only allow additional files and cannot rewrite the files. Redis will read the files to rebuild the data at the beginning of the startup. Is to re-execute all the records to restore the data!

The default save file of AOF is appendonly.
Insert picture description here
AOF is not enabled by default. If you want to enable it, you need to manually modify the configuration file and change the value of appendonly to yes
Insert picture description here

Detailed AOF configuration
# 配置是否开启,默认是不开启
appendfsync no
# 持久化文件名
appendfilename "appendonly.aof"
# 同步策略
# 始终同步,消耗性能
appendfsync always
# 每秒进行一次
appendfsync everysec
# 不同步,
appendfsync no
# 是否重写,默认为no可以保证数据的安全性
no-appendfsync-on-rewrite no
# 重写的基础准则,百分比
auto-aof-rewrite-percentage 100
# 重写的最小大小
auto-aof-rewrite-min-size 64mb
# redis启动时候加载.aof文件如果末尾文件不完整的话,是否截取掉,如果是yes,自动截取正常启动,如果设置为no则会报错
aof-load-truncated yes
advantage
  1. Every modification is synchronized, the integrity of the file is better
  2. Synchronize once per second, only one second of data will be lost
  3. Never synchronize, the most efficient
Disadvantage
  1. The data file is much larger than rdb, and the repair speed is slower
  2. Running efficiency is slower than rdb

Guess you like

Origin blog.csdn.net/weixin_45481406/article/details/109499746