The difference between Redis persistence mechanism RDB and AOF

Redis supports two ways of persistence, one is RDB and the other is AOF. One of them can be used alone or the two can be used in combination.

 

Online summary

RDB
memory snapshot, can be set in redis

save 900 1 (1 redis operation in 900s will do a persistence)
save 300 10 (10 redis operations in 300s will do a persistence)
save 60 10000 (10,000 redis operations in 60s will do a persistence)
    
    Benefits: Performance is better than AOF. There are many
    disadvantages: there may be data loss. For example: 11:05 minutes to persist once, if redis dies at 11:04, then the four minutes of
    data will be lost

AOF (Append only file)
appends all redis changes (addition, deletion, modification) operations to the log file.
    Advantages: relatively safe, even if redis is down, the original data can be quickly restored.
    Disadvantages: will affect the performance of redis

 One, RDB

The persistence of the RDB method is accomplished through snapshotting. When certain conditions are met, Redis will automatically take a snapshot of all data in the memory and store it on the hard disk . The conditions for taking a snapshot can be customized by the user in the configuration file, and consist of two parameters: time and the number of changed keys . A snapshot will be taken when the number of keys changed within the specified time is greater than the specified value . RDB is the default persistence method adopted by Redis, and 3 conditions have been preset in the configuration file:

save 900 1

save 300 10

save 60 10000

The save parameter specifies the snapshot conditions, there can be multiple conditions, and the relationship between the conditions is "or".

As mentioned above, save 900 1 means to take a snapshot if at least one key is changed within 15 minutes (900 seconds). If you want to disable automatic snapshots, you only need to delete some save parameters.

By default, Redis will store the snapshot file in the dump.rdb file in the current directory. You can configure the storage path and file name of the snapshot file by configuring the two parameters dir and dbfilename.

The process of implementing snapshot mechanism is as follows:

  (1) Redis uses the fork function to make a copy (child process) of the current process (parent process);

  (2) The parent process continues to receive and process the commands sent by the client, and the child process starts to write the data in the memory to the temporary file in the hard disk;

  (3) When the child process finishes writing all the data, it will replace the old RDB file temporary file with the file, and the snapshot operation is now complete.

When the fork is executed, the operating system (Unix-like operating system) will use the copy-on-write strategy, that is, when the fork function occurs, the parent and child processes share the same memory data, and when the parent process wants to change a piece of data (Such as executing a write command), the operating system will make a copy of the piece of data to ensure that the data of the child process is not affected, so the new RDB file stores the memory data at the moment the fork is executed. In addition to automatic snapshots, you can also manually send SAVE or BGSAVE commands to make Redis execute snapshots. The difference between the two commands is that the former is the main process for snapshot operations, which will block other requests, and the latter will perform snapshot operations through fork subprocesses.

Persistence is achieved through RDB. Once Redis exits abnormally, all data changed after the last snapshot will be lost. We need to set the automatic saving time of the snapshot according to the requirements.

2. AOF

By default, Redis does not enable AOF (append only file) persistence. You can pass the appendonly parameter

Turn on:

appendonly yes

After opening AOF each perform a persistent change in the data Redis commands, Redis will be the command written to the hard disk AOF file. [Only for writing data instructions]    The save location of the AOF file is the same as the location of the RDB file, both are set by the dir parameter, the default file name is appendonly.aof, which can be modified by the appendfilename parameter: appendfilename appendonly.aof

As more and more commands are executed, the size of the AOF file will become larger and larger. Even though the actual data in the memory may not be much, Redis will automatically rewrite the AOF file whenever certain conditions are met.

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

The meaning of the auto-aof-rewrite-percentage parameter is that when the current AOF file size exceeds what percentage of the AOF file size at the time of the last rewrite, it will be rewritten again. If it has not been rewritten before, it will be rewritten at startup Based on the size of the AOF file. The auto-aof-rewrite-min-size parameter limits the minimum AOF file size that is allowed to be rewritten. Usually, when the AOF file is small, even if there are many redundant commands in it, we don't care too much. In addition to letting Redis automatically perform rewriting, we can also actively use the BGREWRITEAOF command to manually perform AOF rewriting

It should be noted that although the AOF will record the command in the AOF file every time the operation of changing the database content is executed, in fact, due to the caching mechanism of the operating system, the data is not actually written to the hard disk, but entered into the system Hard disk cache. By default, the system will perform a synchronization operation every 30 seconds to actually write the contents of the hard disk cache to the hard disk. During these 30 seconds, if the system exits abnormally, the data in the hard disk cache will be lost. Generally speaking, applications that enable AOF persistence cannot tolerate such losses. This requires Redis to actively request the system to synchronize the cached content to the hard disk after writing the AOF file. In Redis, we can set the synchronization timing through the appendfsync parameter:

# appendfsync always

appendfsync everysec

# appendfsync no

By default, Redis uses the everysec rule, that is, a synchronization operation is performed every second. always means that synchronization will be performed every time a write is performed, which is the safest and slowest way. no means that the synchronization operation is not actively performed, but is completely handed over to the operating system (that is, once every 30 seconds), which is the fastest but least secure way. In general, it is sufficient to use the default value of everysec, which takes into account both performance and security.

Redis allows AOF and RDB to be turned on at the same time, which not only ensures data security but also makes it easy to perform backups and other operations. At this time, after restarting Redis, Redis will use the AOF file to recover data, because the persistence of the AOF method may lose less data.



 

Guess you like

Origin blog.csdn.net/qq_27828675/article/details/102471724