Design and Implementation of Redis - (g) Redis persistence mechanism

RDB endurance of

       Since Redis is a memory database, so when the database is down, all data will be lost, in order to prevent loss of data, we use RDB database state will be saved as a file. First generation RDB command file, there are two: SAVE and BGSAVE

       SAVE command Redis server will block until the RDB file is created

       BGSAVE not block the server, create a child process to save

       RDB after the file is generated when Redis starts, it will automatically load the file, without manual command

       Note: AOF higher priority than RDB, it will default to using AOF restore the database, in addition to not allow repetition of the conduct BGSAVE or SAVE, will block until loading RDB file

       We can set the timing condition saved automatically performed BGSAVE command stored in the saveparam redisServer in the format, as long as one condition is satisfied will be executed SAVE

       In addition, the server holds property for the Dirty how many times modified since the last generation RDB files recorded on the time of the first performance on the SAVE command lastSave record

RDB files saved it actually look like?

Wherein contents of each database is as follows:

When reading SELECTDB equivalent of telling Redis database select which DB, will return the following data into it. And specific structure of key-value pairs as shown below:

AOF persistence

       Append Only File ~ for persistence by saving the execution of the write command Redis to Redis's request to record the agreement in plain text.

       Append command: each execute a command, a command will be added to the server buffer AOF

File write and synchronization:

       Redis server is actually a loop, continue to accept the client's request (that is, one event), there may be a request during a write command, the buffer is written to the AOF, each event is completed or not AOF buffer is written to the file it? There are three strategies: always always written, everysec per second (the default), no never

       How to recover it from the database AOF file?

             Create a pseudo-client, executed one by one AOF command

       AOF rewrite file

              AOF decreases redundant file, there is provided BGREWRITEAOF command, this command will read the current state of the database and generate a command, in order to avoid blocking the main thread, also used in the manner of the child thread. This presents a problem, the main thread of execution is still barabara, the child thread to read all of the state of the database command will generate inconsistent problems, how to solve it?

       AOF rewrite provide a buffer

When the child process AOF rewrite is completed, it will signal the parent process, the parent process after receiving AOF rewrite data buffer write-once AOF file (this time blocking the parent process).

Published 47 original articles · won praise 8 · views 30000 +

Guess you like

Origin blog.csdn.net/nanchengyu/article/details/89338031