An RDB in memory is saved as a file for the RDB to persist. RDB file when a binary file is being opened.
One-load and storage
The file is loaded at server startup (Rdbload ()) because the AOF update frequency is higher than the RDB, so if the AOF persistence feature is turned on, the server will first restore the db from AoF, and only aof shut down to restore the database from the RDB.
The storage of the file is implemented by the Rdbsave ()/RDB.C, which can be triggered by the Save/bgsave command, which is processed directly by Redis server and cannot handle other commands, which start the child process for processing, while the server can process the other commands, but When the Bgsave executes, the Save command can no longer be executed, the execution of Bgsave is also not possible, and finally bgrewriteaof can not be executed at the same time and Bgsave.
Two-cycle storage
Save 900 1
Save 300 10
Save 60 10000
The above three commands represent one of the satisfying conditions that will trigger Bgsave:
1. At least 1 updates in 900 seconds
2. Update at least 10 times in 300 seconds
3. At least 10,000 updates in 60 seconds
These times and the number of updates are set, stored in
structSaveparam {//How many seconds?time_t seconds; //How many modifications occurred intchanges;};structredisserver{ ...structSaveparam *saveparams;/*Save points Array for RDB*/
...
The number of times the database has been modified since last SAVE was executed
Long long dirty; /* Changes to DB from the last Save */
//Last time the SAVE was completed
time_t Lastsave; /* Unix time of last successful save */
...};
The above dirty, on behalf of the last successful save/bgsave, how many times the database has been updated. Lastsave records the last time the Save/bgsave was successfully executed.
The Servercron () function performs a 100ms cycle, with the values in Saveparams and the dirty and Lastsave comparisons, to update the processing of the RDB.
The file structure of the three RDB
The Rdb file is a binary file, format customization, there is not much to say, examples are as follows:
[Redis Reading notes] Part two single-machine database RDB Persistence