An overview of the
- RDB persistence saves all database data contained by the server to the hard disk as binary files
- By loading the RDB file when the server is started, the server can restore the original database data according to the CONTENT of the RDB file
Disadvantages: Save all the data of the database, which consumes resources and time, so the server needs to create a new RDB file at intervals; If the server is unexpectedly shut down while waiting for the next RDB file creation, the user will lose data since the last RDB file creation
Create an RDB file
Common ways:
- The server executes the SAVE command sent by the client
- The server executes the BGSAVE command sent by the client
- The automatic save conditions set using the Save configuration option are met, and the server automatically executes BGSAVE
The command
-
The SAVE commandBy sending the SAVE command from the client to the server, the server creates a new RDB fileDuring the execution of the SAVE command, the Redis server will be blocked and cannot process the command request sent by the client. Only after the SAVE command is executed, the server will restart processing the command request sent by the client. If the RDB file already exists, the server will automatically replace the old RDB file with a new REB file
127.0.0.1:6379 > save OKCopy the code
-
The BGSAVE command also creates a new RDB file, different from the SAVE command: BGSAVE does not cause the Redis server to block. The Redis server can still take care of an asynchronous command sent by another client during BGSAVE execution
127.0.0.1:6379> bgsave Background saving started Copy the code
No blocking cause:
1. When the Redis server receives the BGSAVE command, it does not create the RDB file itself. Instead, it forks () a child process that creates the RDB file and continues to process the client’s command requests. 2. When the child process has created the RDB file and exits, it sends a signal to the parent process (the Redis server that handles command requests) that the RDB file has been created. 3. Finally, the Redis server (parent process) receives the RDB file created by the child process, and the BGSAVE execution is complete
- SAVE and BGSAVE are different
The command | SAVE | BGSAVE |
---|---|---|
type | synchronous | asynchronous |
Yes No Server is blocked | is | no |
The complexity of the | O(N) | O(N) |
advantages | No child processes need to be created, no extra memory is consumed, and resources can be pooled to create RDB files, so SAVE creates RDB files faster than BGSAVE | The Redis server can create an RDB file and still process the client’s command requests normally |
disadvantages | The server is blocked during execution | The need to create child processes consumes additional memory |
- Automatically create RDB filesConfigure options via save in the format:
save <seconds> <changes> Copy the code
If seconds have passed since the last RDB file was created, and all databases on the server have undergone a total of at least CHANGES (including additions, deletions, and updates), then the BGSAVE sample is executed:
save 900 1 save 300 10 save 60 10000 Copy the code
When one of the conditions is met, the server executes BGSAVE