How redis master-slave synchronization of data

Redis master-slave synchronization mechanism ensures that the data between the master and slave synchronization redis. Syndicate content according to how much can be divided into full synchronization and partial synchronization; in accordance with the timing synchronization can be divided into just started initialization of slave synchronization and data during normal operation of modified synchronization; This paper will process these two mechanisms for analysis.

After the whole backup process, when you start the slave, will send a SYNC message to its master, master slave receipt of this message, will likely start the daemon for backup, the backup data after the backup is completed it will be sent to the slave, the initial full synchronization mechanism when something like this:

(1) slave to start transmission after the synchronization instruction to the master SYNC, master handler receives calls syncCommand SYNC instruction after the command () sync;

(2) in the function syncCommand, you call the function rdbSaveBackground start a backup process for data synchronization, if you already have a backup process is running, it will not re-started.

(3) the backup process will execute the function rdbSave () to complete all the data will be saved for redis rdb file.

(4) In the event redis time function serverCron (redis time processing functions are functions that it would be timed to operate redis), the data will be processed backup, the backup process will check whether the function has been serverCron is finished, the backup if the backup process has been completed, the calling function backgroundSaveDoneHandler complete follow-up treatment.

(5) in the function backgroundSaveDoneHandler, first update master various states, for example, a backup success or failure, backup time, and so on. Then call the function updateSlavesWaitingBgsave, rdb backup data is sent to the slave wait.

(6) in the function updateSlavesWaitingBgsave, the slave will traverse all waiting for this backup, file backup rdb will send to each slave. In addition, this data is not sent immediately to put in the past, but will write events for each slave waiting for registration, and register write response function sendBulkToSlave events, that is, when slave can send data corresponding socket function is called sendBulkToSlave () the actual transmission rdb file operations are done in the function sendBulkToSlave.

Send rdb file (7) sendBulkToSlave function will backup to the slave.

The above-described function call shown in Figure 1:

Function call when a full backup of the master portion of FIG. 1 redis

Second, synchronization of data modification

Redis normal deployment in a generally master for write operation, a plurality of slave for a read operation, a data backup operation further periodic single location is a slave to complete, which can play the greatest degree of performance redis. Fully synchronous operation after the deployment is complete, each master \ slave program starts, first the first phase of initialization, after fully synchronous operation is completed, all subsequent write operations are performed on the master, all read operations are performed on the slave , so users need to write to all the slave spread in time to keep pace on the data maximum extent. Redis master-slave process of update operations (including write, delete, change operation) during normal operation synchronized as follows:

After (1) master receives a user's operation, the function calls the function to call a specific action function (refer to process another document " Redis command analysis process "), the first function performed by the operation function proc then determines whether the operation requires diffusion into each Slave, if necessary Propagate the calling function () to do this.

(2) propagate () function to complete an operation record to aof file or spread to other slave; and by calling feedAppendOnlyFile () operation recorded in the function to aof, the operation of diffusion to the respective slave by calling replicationFeedSlaves () in.

(3) function feedAppendOnlyFile () aof the main file save operation, the first conversion operation into an internal format of the protocol redis function, and stored as a string, then append to the string stored in the operation file aof .

(4) Function replicationFeedSlaves () operation mainly diffused to each of the slave; through each slave hanging himself in the following the function, in order to process two steps as follows for each slave: switching to the slave database this operation corresponding to the database (if the slave database id and id data inconsistencies when the current operation for this operation); commands and parameters are written to the cache reply slave protocol format in accordance with the redis. Called when the write command to switch the database addReply, it will call addReplyMultiBulkLen and addReplyBulk write command and parameters, and function addReplyMultiBulkLen addReplyBulk eventually will call the function addReply.

(5) in the function call prepareClientToWrite addReply () Set the slave socket write event handlers sendReplyToClient (set by function aeCreateFileEvent), so that once the socket slave transmission buffer space corresponding to the write data, i.e., calls are processed sendReplyToClient .

(6) Function sendReplyToClient () function is the main data to be transmitted by the slave socket sent out.

2, the relationship between the data synchronization function call redis during operation of FIG.

The figure represents the number of calls has relationships, meaningful numbers between siblings.

Guess you like

Origin www.cnblogs.com/lice-blog/p/11616364.html