This is the 11th day of my participation in the August Wenwen Challenge.More challenges in August

Why RDB persistence

Because Redis is an in-memory database, it stores its database state in memory, so if you do not want to save the database state stored in memory to disk, then once the server exits, the database state in the server will also disappear.

To solve this problem, Redis provides RDB persistence, which can save Redis database state in memory to disk to avoid accidental database loss.

RDB persistence, which can be performed either manually or periodically depending on server configuration options, saves database state at a point in time to an RDB file. The RDB file generated by the RDB persistence function is a compressed binary file that restores the database state when the RDB file is generated.

Two, RDB file creation and loading

There are two Redis commands that can be used to generate RDB files, one is SAVE and the other is BGSAVE.

  • The SAVE command blocks the Redis server process until the RDB file is created, and the server cannot process any command requests while the server process is blocked.
  • The BGSAVE command gives birth to a child process, which then creates the RDB file, and the server process (parent) continues to process the command requests.

The actual work of creating an RDB file is done by the rdb.c/redbSave function, which is called differently by the SAVE and BGSAVE commands, as can be seen from the following pseudocode:

def SAVE(a)Create the RDB filerdbSave(a)

def BGSAVE(a)Create a child process PID= fork()
    
    if pid == 0Signal_parnet () elif pid >0Handle_request_and_wait_signal ()else: # Handle error cases handle_fork_error()Copy the code

The loading of RDB files is automatically performed when the server starts, so Redis does not have a special command for loading RDB files. The RDB files will be automatically loaded whenever the server detects the existence of RDB files when the server starts.

Also note that since AOF files are usually updated more frequently than RDB files:

  • If AOF persistence is enabled on the server, AOF files are used to restore database state in preference.
  • The RDB file is used by the server to restore the database state only when AOF persistence is turned off.

The actual work of loading the RDB file is done by the rdb.c/rdbLoad function. The relationship between this function and the rdbSave function can be shown as follows:

2.1 Server status when the SAVE command is executed

The Redis server is blocked when the SAVE command is executed, so all requests sent by the client are blocked while the SAVE command is executing.

2.2 Server Status when the BGSAVE command is executed

The BGSAVE command is saved by the child process, so the Redis server can continue to process the client’s command requests while the child process creates the RDB file. However, during the BGSAVE command execution, The server handles the SAVE, BGSAVE, and BGREWRITEAOF commands differently than usual.

  • During BGSAVE command execution, SAVE commands sent by the client are rejected by the server. The server disallows the simultaneous execution of two commands to prevent the parent (server process) and child from executing two rdbSave calls at the same time, preventing race conditions.
  • During the BGSAVE command execution, BGSAVE commands sent by the client are rejected by the server.
  • The BGSAVE and BGREWRITEAOF commands cannot be executed at the same time:
  • If the BGSAVE command is being executed, the BGREWRITEAOF command sent by the client is delayed until the BGSAVE command is executed.
  • If the BGREWITEAOF command is running, the BGSAVE command sent by the client will be rejected by the server.

2.3 Server status when RDB files are loaded

The server blocks while loading the RDB file until the load is complete.

3. RDB automatic interval saving

Because the BGSAVE command can be executed without blocking the server process, Redis allows the user to have the server automatically execute the BGSAVE command once in a while by setting the save option for the server configuration. The user can set multiple save criteria with the Save option, but the server executes the BGSAVE command whenever any of the criteria is met.

For example, the following configuration:

# server is in900Within seconds, at least one save change has been made to the database900 1# server is in300Within seconds, the database is at least10Change to save300 10# server is in60Within seconds, the database is at least10000Change to save60 10000
Copy the code