redis persistence (RDB and AOF) To be continued

1, redis Why should achieve persistence?

Avoid downtime, power outages and other scenarios lead to data loss after the process exits, if only redis data stored in the memory, then the process exits after the data is lost. Persistence mechanism to persist data to the hard disk memory, based on persistent data recovery after the restart redis.

2, redis persistent manner which

2.1 RDB, time lapse snapshot process data stored on the hard disk to a persistent way

2.1.1 How to trigger RDB persistence?

2.1.1.1 manual trigger

1. [not recommended] Redis Save command to save a synchronous operation, all data snapshot (snapshot) currently saved to the hard disk Redis instance in the form of RDB files. $ \ color {red} {This command will block the current Redis server until the process is complete RDB, for example relatively large memory for a long time can cause obstruction, online environment is not recommended. } $
Commands are as follows

redis 127.0.0.1:6379> SAVE 
OK

2. [Recommended] In order to solve the problem of blocking SAVE command redis instance, redis provide another command: BGSAVE.
Redis BGSAVE command asynchronously in the background to save the current data in the database to disk.

$ \ color {red} {BGSAVE command after executing immediately return OK, then Redis fork a new child process, the original Redis process (parent process) continue to process client requests, the child process is responsible for saving data to disk, and then drop out. } $
Command is as follows:

redis> BGSAVE
Background saving started

Obviously BGSAVE SAVE command against blocking problems do optimization. Thus all internal Redis involved
and RDB are used BGSAVE operating manner, and the SAVE command is deprecated.

2.1.1.2 automatically trigger

REDIS automatically triggers RDB place orders in the following scenarios:

  • Use the save configuration, such as "save mn". It indicates the presence of n times within m seconds modification data set
    , the automatic trigger bgsave.
  • If the full amount from the node to perform the copy operation is performed automatically bgsave master node generates and sends documents from the RDB node, for more details see Section 6.3 describes the principles copy.
  • Executing debug reload command to reload Redis, it will automatically trigger the save operation.
  • When the shutdown command by default, if not open AOF persistence function is
    performed automatically bgsave.

2.1.2 BGSAVE process flow

BGSAVE command processing
1. bgsave command execution, the Redis parent process determines whether there is currently executing into sub-
processes, such as RDB / AOF child process, if there is a direct return bgsave command.

2. The parent process fork operation to create a child process, the parent process fork operation will block, pass
through the info stats command to view latest_fork_usec options, you can get a fork consumption recently operated
upon in microseconds.

3. After the completion of the parent process fork, bgsave command returns "Background saving started" information
and will not clog the parent process, you can continue to respond to other commands.

4. Create a child process RDB files, memory generates a temporary snapshot file according to the parent process, after the completion of
the original files atom replaced. Lastsave execute command can get the last generation of RDB
time, the corresponding info statistics rdb_last_save_time options.

The process sends a signal to indicate the completion of the parent process, the parent process to update statistics, see the specific
rdb_ * info related options under Persistence.

2.1.3 RDB log file where?

Dir parameters stored in the specified directory.
RDB change log file storage directory command:

config set dir {newDir}

RDB command to change the log file name:

config set dbfilename {newFileName}

$ \ color {red} {bad situation encountered when a disk or hard disk is full and the like, may be {newDir} online config set dir
modify the file path to the available disk path, after performing handover bgsave disk, the same applies
to a persistent AOF of $ file}
$ \ Color Red {} {Redis defaults LZF algorithm to generate the RDB file to do the compression process, the compressed
file is much smaller than the memory size, enabled by default, you can config set rdbcompression {yes parameter | no} dynamic modify. $}
$ \ Color Red {{} while the CPU will consume RDB compression, but the file size can be greatly reduced, easy to the hard disk
or from a node to a network, turned recommendation line. } $

2.2 AOF, write command based on the log file logging persistent high real-time mode

3. Two comparison of the pros and cons of persistent way

Guess you like

Origin www.cnblogs.com/powerjiajun/p/11569460.html