Redis is an in-memory key-value database. All data is stored in memory, but what happens to the in-memory data when the server is unexpectedly interrupted? Of course redis provides two types of persistence: RDB AOF so let's talk about how they work. RDB. This form of persistence produces a compressed binary (dump.rdb). How is this file generated? There are also two commands, one is save, which blocks the server process and the other is BGSave, which spawns a child process and of course the actual work is done by the rdb.c/rdbSave function and then, with the RDB file, how do you recover the data? This is done automatically when the server starts. Again, this process blocks. Who executes the BGsave command when? The bgSave RDB file can be automatically executed by the serverCron function by configuring the save option. Next, we'll talk about AOF. This form of persistence generates an appendone.aof file that holds the write command executed by the server. How is it done? After the server executes a write command, it appends to the AOF buffer in protocol format writes the contents of the AOF buffer to an AOF file and then synchronizes the AOF file for the last time how do YOU recover the data? Create a dummy client. You take a write command out of an AOF file and you analyze it and you read it and you execute the read write command and you repeat it until you read all the commands and then there's a problem, as time goes by, the AOF file gets bigger and bigger, how do you get smaller? Bgrewriteaof command during aOF rewrite, the server will continue to process the command. How to resolve this inconsistency? Execute client's command to append to the AOF buffer append to the AOF rewrite buffer bgrewriteAOF and after that, write the contents of the AOF rewrite buffer to the new AOF file, complete the substitution, this step is blocking what's the priority of the two ways? Cause: AOF files are updated more frequently than RDB files. Therefore, if the AOF function is enabled, the server uses AOF files first to restore the database, and then RDB files to restore the database. The specific configuration of these two, please consult the relevant documents, here is not to talk about.Copy the code