10.1 Rdb file creation and onboarding
There are two Redis commands that can be used to generate an RDB file, one for save and the other for Bgsave.
The Save command blocks the Redis server process until the Rdb file is created.
The Bgsave command will derive a Zi Jin Chengguan responsible for creating the Rdb file, and the server process continues to process the command request.
The loading of an RDB file is performed automatically when the server is started, because the update frequency of the AoF file is usually higher than the update frequency of the Rdb file, therefore:
① if the server has AOF persistence enabled, the server takes precedence over the aof file to restore the database state;
② the server uses an RDB file to restore the database state only if the AOF persistence feature is turned off.
The process by which the server determines which file to use to restore the database state is as follows:
10.1.2 server state when the Bgsave command executes
int Rdbsave (char *filename) {//save command execution function char tmpfile[256]; FILE *FP; Rio Rdb; int error; snprintf (tmpfile,256, "Temp-%d.rdb", (int) getpid ()); fp = fopen (Tmpfile, "w"); if (!FP) {Redislog (redis_warning, "Failed opening. Rdb for saving:%s", Strerror (errno)); return redis_err; } rioinitwithfile (&RDB,FP); if (Rdbsaverio (&rdb,&error) = = Redis_err) {errno = error; Goto Werr; }/* Make sure data won't remain on the OS ' s output buffers */if (fflush (fp) = = EOF) goto Werr; if (Fsync (Fileno (fp)) = =-1) goto Werr; if (fclose (fp) = = EOF) goto Werr; /* Use RENAME to make sure the db file is changed atomically only * If the Generate DB file is OK. */if (rename (tmpfile,filename) = =-1) {Redislog (redis_warning, "Error Moving temp DB file on the final Destinat Ion:%s ", Strerror (errno)); Unlink (tmpfile); return redis_err; } redislog (Redis_notice, "DB saved on disk"); Server.dirty = 0; Server.lastsave = time (NULL); Server.lastbgsave_status = REDIS_OK; Return Redis_ok;werr:redislog (redis_warning, "Write Error saving DB on disk:%s", Strerror (errno)); Fclose (FP); Unlink (tmpfile); return redis_err;}
during the execution of the Bgsave command, the SAVE and Bgsave commands are rejected by the server.
Bgsave and bgrewriteaof cannot be executed at the same time:
① If Bgsave is executing, the bgrewriteaof command is deferred until the Bgsave command is executed.
② If the bgrewriteaof command is executing, the Bgsave command sent by the client is rejected by the server.
10.2 Automatic interval saving
If you provide the following configuration to the server:
Save 900 1
Save 300 10
Save 60 10000
So as long as any one is satisfied, the Bgsave command will be executed:
The server changed the database at least 1 times within 900 seconds.
The server changed the database at least 10 times within 300 seconds.
The server changed the database at least 10,000 times within 60 seconds.
Servercron is performed every 100 milliseconds by default, and one of the tasks is to check the settings of the Save option
Whether the save condition has been met.
Save conditions in the server state
RDB file structure:
RDB File database structure:
Key_value_pairs structure:
10.5 Key Reviews
The Ⅰrdb file is used to save and restore all key-value pairs of data in all databases of the Redis server.
The Ⅱsave command executes the save operation directly by the server process, and all the commands block the server.
The Ⅲbgsave command performs a save operation from the Forbidden City, so the command does not block the server.
all save conditions set with the Save option are saved in the Ⅳ server state, and when any of the save conditions are met, The server will automatically hold the Bgsave command.
The Ⅴrdb file is a compressed binary file that consists of multiple parts.
Ⅵ for different types of key-value pairs, RDB files use different ways to save them.
Redis Learning----10.RDB persistence