Three redis persistence methods

Reids are mainly two ways of persistence: RDB and AOF

  1. RDB (Redis DataBase): RDB saves snapshot data before a certain point in time.
  2. AOF (Append-Only File): All command line records are completely persistent and stored as AOF files in the format of the redis request protocol.
  3. Hybrid persistence (4.x): When performing AOF rewriting, the child process saves the data snapshot at the current time point in the RDB file format, and saves the parent process's accumulated commands in the AOF format.

 

One, RDB snapshot

There are two trigger methods

1. Through configuration parameters:

Snapshot generation is executed when the number of commands executed in a certain period exceeds the threshold.

save 900 1
save 300 10
save 60 10000
或
save '' #表示关闭RDB快照。注意:主从复制的主节点永远关不掉RDB,配置了也没用。

Indicates how many seconds are exceeded, how many commands have been executed, and snapshot generation will be executed if they are satisfied. For example: if more than 10,000 commands are executed within 60 seconds, the bottom layer calls the bgsave command to generate a snapshot. 

2. Manually execute the bgsave command or save command or SHUTDOWN to explicitly generate a snapshot

save: Use the main process to save, if the amount of data is large, the main process may be blocked. Will affect the redis service.

bgsave: fork a child process to save. Will not affect the redis service. As shown in the figure:

Advantages and disadvantages of RDB persistence

advantage:

  1. Good performance. Use the child process for persistence without affecting the service of the main process.
  2. High start-up efficiency. Parse RDB binary files directly at startup, which is more efficient than AOF.

Disadvantages:

  1. Data security is low . RDB is persisted at intervals. If a failure occurs during the period, data will be lost.
  2. During RDB persistence, a large number of write operations generate a large number of page faults, and the kernel keeps copyonwrite copying abnormal pages, resulting in performance degradation.

 

Second, AOF persistence

#redis.conf配置通过下面配置开启aof,默认是关闭的
appendonly yes

After the command is executed, it will be written to the aof buffer (in memory), and then through a certain strategy, and then written to the aof file

#redis.conf配置同步策略
appendfsync everysec

There are three aof synchronization strategies:

  • no means waiting for the operating system to synchronize the data cache to the disk (usually the longest synchronization period is 30S)
  • always means that fsync() is called to write data to disk after each update operation. The performance is very affected, and the production environment cannot be opened.
  • everysec synchronizes every second, the default value

AOF rewrite mechanism

As commands continue to be written to AOF, the file becomes larger and larger

There are two trigger methods

  1. Manual trigger: bgrewriteaof command
  2. Automatic trigger: auto-aof-rewrite-min-size and auto-aof-rewrite-percentage parameters determine the trigger timing
#redis.conf配置
auto-aof-rewrite-percentage 100 表示当前AOF文件空间和上次重写后的AOF文件空间的比值
auto-aof-rewrite-min-size 64mb #表示AOF文件重写时文件最小体积64MB

AOF advantages and disadvantages

advantage:

  1. Data Security. When the always strategy is used, zero data loss can be achieved.

Disadvantages:

  1. Start-up efficiency is low. When the aof file is large, the startup efficiency is lower than that of RDB

 

Three, hybrid persistence

Enable via aof-use-rdb-preamble yes configuration

When loading, it will first identify whether the AOF file starts with the REDIS string. If it is, load it according to the RDB format. After loading the RDB, continue to load the remaining part according to the AOF format.

The hybrid persistence scheme takes into account the speed of RDB and the security of AOF

Guess you like

Origin blog.csdn.net/sumengnan/article/details/113095458