Redis persistence configuration, RDB and AOF configuration instructions

Article directory

    • I. Overview
    • 2. RDB persistence configuration
    • 3. AOF persistence mode configuration

If your understanding of Redis is not deep enough, please pay attention to this column. This column includes Redis installation, Redis configuration file description, Redis command and data type description.

1. Overview

  • Redis persistence refers to the process of storing Redis data on disk so that the data can be restored after the Redis server is restarted. Redis provides two main persistence methods:

  • RDB persistence (Redis Database)

    • RDB persistence is to save Redis data to a binary file on disk in the form of a snapshot.
    • An RDB file is a snapshot of Redis data at a certain point in time, containing the status ofall data.
    • RDB persistence is suitable for scenarios where data needs to be backed up or full recovery required at a specific point in time.
    • RDB persistence is triggered manually using the SAVE or BGSAVE command, or it can be triggered automatically and periodically through configuration (see Redis command usage).
  • AOF persistence (Append-Only File)

    • AOF persistence records Redis write operations to files on the disk in the form of log append.
    • The AOF file is a text file that records all commands and parameters for write operations.
    • By re-executing the commands in the AOF file, you can restore the state of the Redis data.
    • AOF persistence is suitable for scenarios that require real-time recording of data changes and ensure maximum data security.
    • AOF persistence can record commands in real time through configuration, or rewrite according to a certain strategy (AOF Rewrite) to compress the file size.
  • Redis also provides a hybrid persistence method, that is, using RDB persistence and AOF persistence at the same time. This method can take into account fast recovery and data security.

  • Redis also supports a non-persistent mode, which means that the data is not persisted to disk but only kept in memory. This mode is suitable for scenarios that do not have high requirements for data persistence or are only used for caching.

2. RDB persistence configuration

  • Persistence principle

    • RDB persistence is a snapshot persistence method of Redis. It writes the in-memory data snapshot to a disk file so that the data can be restored from the disk file when needed.
    • The advantages are higher performance, fast recovery speed, and data compression.
    • The disadvantage is that the real-time performance is poor, after all, each snapshot has a certain time interval.
  • Basic syntax for configuration: save

    • indicates how often the interval is triggered
    • indicates the number of changes to trigger once
    • Both conditions must be met at the same time to trigger RDB persistence.
  • Turn on RDB persistence

    • Add a configuration similar to the following in the redis.conf configuration file to turn on Redis’s RDB persistence. For more Redis configuration, please see here.
    # This means that RDB persistence is triggered when the time exceeds 900 seconds and at least 1 key changes.
    save 900 1
    # This means that RDB persistence is triggered when the time exceeds 300 seconds and at least 10 keys change.
    save 300 10
    # This means that RDB persistence is triggered when the time exceeds 60 seconds and at least 10,000 keys change.
    save 60 10000
    
  • Turn off RDB persistence

    • Set save to a null value, or delete the save configuration directly, as follows:
    save ""
    
  • Persist other configurations

    • These configurations are also critical and must be configured accurately.
    • If the persistent file directory does not exist, it needs to be created manually.
    # RDB persistence file name
    dbfilenamedum.rdb
    # Persistent file directory
    dir /var/lib/redis/6379
    
    # Whether to enable compression
    rdbcompression yes
    # RDB file check digit
    rdbchecksum yes
    
  • Check RDB file contents

    redis-check-rdb dump.rdb
    

    [root@yiqifu-redis 6379]# redis-check-rdb dump.rdb
    [offset 0] Checking RDB file dump.rdb
    [offset 26] AUX FIELD redis-ver = 6.0.6’
    [offset 40] AUX FIELD redis-bits = 64’
    [offset 52] AUX FIELD ctime = 1697161255’
    [offset 67] AUX FIELD used-mem = 865296’
    [offset 83] AUX FIELD aof-preamble = 0’
    [offset 85] Selecting DB ID 0
    [offset 125] Checksum OK
    [offset 125] \o/ RDB looks OK! \o/
    [info] 3 keys read
    [info] 0 expires
    [info] 0 already expired

3. AOF persistence mode configuration

  • Persistence principle

    • The write operation is appended to a log file to record all write commands received by the Redis server so that the data can be recovered when needed.
    • The advantage is high real-time performance, because it records all commands in real time.
    • Disadvantages: Real-time recording will occupy a lot of space, and simple AOF recovery speed is slow. So it is generally used in conjunction with RDB.
  • Persistence configuration

    • AOF persistence configuration uses appendonly. When the value is yes, AOF persistence is enabled. When the value is no, AOF persistence is disabled.
  • Turn on AOF persistence (off by default)

    • Add a configuration similar to the following in the redis.conf configuration file to turn on Redis’ AOF persistence. For more Redis configuration, please see here.
    appendonly yes
    
  • Turn off AOF persistence

    appendonly no
    
  • Persist other configurations

    • These configurations are also critical and must be configured accurately.
    • If the persistent file directory does not exist, it needs to be created manually.
    # AOF persistent file name
    appendfilename "appendonly.aof"
    # Persistent file directory
    dir /var/lib/redis/6379
    
    # AOF persistence level (speed of writing to disk)
    # Can be configured as always=always, everysec=every second, no=flush is determined based on the system buffer size (4k).
    appendfsync everysec
    # When performing RDB persistence, whether to continue to perform AOF persistence, the default is not to perform
    no-appendfsync-on-rewrite no
    # Whether to enable AOF and RDB mixed mode
    aof-use-rdb-preamble yes
    
    # AOF persistence automatically rewrites the trigger condition configuration. Rewriting will compress the AOF file. If the mixed mode is enabled, the RDB solution will be added.
    # The condition for each subsequent triggering of rewriting is that after the previous triggering of rewriting, the rewriting is triggered when it exceeds 100%.
    auto-aof-rewrite-percentage 100
    # After booting, when the aof file reaches 64MB, rewriting will be triggered.
    auto-aof-rewrite-min-size 64mb
    
    
  • Check AOF file content

redis-check-aof appendonly.aof

[root@yiqifu-redis 6379]# redis-check-aof appendonly.aof
AOF analyzed: size=120, ok_up_to=120, diff=0
AOF is valid

  • AOF persistent old version file content analysis (can be viewed through the cat appendonly.aof command before compression)

    • *Number: Indicates how many lines the current command should take. For example, * means taking 2 lines, *3 means taking 3 lines.
    • $Number: Indicates how many small bytes are to be fetched next. For example, $6 means taking 6 bytes, $3 means taking 3 bytes.
    • Install *n and $n and take out a complete command.
    *2
    $6
    SELECT
    $1
    0
    *3
    $3
    set
    $3
    aaa
    $3
    111
    *3
    $3
    set
    $3
    bbb
    $3
    222