1. RDB (Redis Database)

The process of taking a snapshot of the current process and saving it to disk.

1.1 Triggering Mechanism

Triggering RDB persistence can be manually triggered or automatically triggered.

  • Manual trigger corresponds respectivelysavebgsaveThe command
    1. Save: block the current Redis server until the RDB process is complete, causing a long block for large memory instances.
    2. Bgsave: The Redis process forks to create a child process. RDB persistence is the responsibility of the child process and ends automatically.
  • Automatic trigger
    1. The commandsave m nBgsave is automatically triggered when the data set is modified for n times within m seconds.
    2. The slave node performs a full copy operation, and the master node automatically bgSave to generate RDB files and send them to the slave node.
    3. performdebug reloadThe save operation is automatically triggered when the Redis command is reloaded.
    4. It is executed by defaultshutdownCommand, bgSave is automatically executed if AOF persistence is not enabled.

1.2 Process Description

1.3 the advantages and disadvantages

Advantages:

  • The RDB is a compact binary file that represents a snapshot of Redis data at a point in time. Ideal for backup, full copy, and other scenarios. For example, perform bgSave backups every 6 hours and copy RDB files to a remote machine or file system (such as HDFS) for disaster recovery.
  • Redis loads RDB recovery data much faster than AOF.

Disadvantages:

  • RDB data cannot be persisted in real time/second level. Because bgSave forks every time it runs, it is a heavyweight operation that is expensive to execute frequently.
  • RDB files are saved in a specific binary format. During the Redis version evolution, multiple RDB versions are available. However, the old Redis service may not be compatible with the new RDB format.

Redis provides AOF persistence to solve the problem that RDB is not suitable for real-time persistence

2. AOF (Append Only File)

Each write command is recorded in an independent log, and the command in the AOF file is executed again when the system restarts to recover data.

Before enabling the AOF function, set the following configuration: appendonly yes. This function is disabled by default. The AOF file is set through the appendfilename configuration and the default filename is appendone.aof. The saving path is the same as the RDB persistence mode. Specified by dir configuration. AOF workflow operations: command write (append), file synchronization (sync), file rewrite (rewrite), restart load (load).

2.1 Process Description

  1. All write commands are appended to the aOF_buf (buffer).
  2. The AOF buffer synchronizes data to disks based on the corresponding policy.
  3. As AOF files become larger, they need to be rewritten periodically to achieve compression.
  4. When the Redis service restarts, the AOF file can be loaded for data recovery.


Reference:

  • Redis Development and Operation