Redis master-slave synchronization principle - SYNC

  Like the reason for MySQL master-slave replication, although Redis is extremely fast in reading and writing, it also produces a situation where the reading pressure is particularly high. In order to share the reading pressure, Redis supports master-slave replication, and the master-slave structure of Redis can use one master and multiple slaves or a cascade structure.

 

Redis master-slave replication can be divided into full synchronization and incremental synchronization according to whether it is full.

1 Full synchronization

  Redis full replication generally occurs in the Slave initialization phase. At this time, the Slave needs to copy all the data on the Master. Specific steps are as follows: 
  1) The slave server connects to the master server and sends the SYNC command; 
  2) After the master server receives the SYNC name, it starts to execute the BGSAVE command to generate the RDB file and uses the buffer to record all the write commands executed thereafter; 
  3) After the master server BGSAVE is executed, it sends snapshot files to all slave servers, and continues to record the executed write commands during the sending period; 
  4) After receiving the snapshot file from the server, discard all old data and load the received snapshot; 
  5) After the master server snapshot is sent, it starts to send the write command in the buffer to the slave server; 
  6) The slave server completes the loading of the snapshot, starts to receive command requests, and executes the write command from the master server buffer; 
 
  After completing the above steps, all operations of data initialization from the server are completed, and the slave server can receive read requests from users at this time.

2 Incremental synchronization

  Redis incremental replication refers to the process of synchronizing the write operations from the master server to the slave server when the slave starts to work normally after initialization. 
The process of incremental replication is mainly that every time the master server executes a write command, it sends the same write command to the slave server, and the slave server receives and executes the received write command.

3 Redis master-slave synchronization strategy

  When the master and slave are just connected, full synchronization is performed; after full synchronization is completed, incremental synchronization is performed. Of course, the slave can initiate a full sync at any time if needed. The redis strategy is that, in any case, incremental synchronization will be attempted first, and if unsuccessful, the slave will be required to perform full synchronization.

4 other

After Redis 2.8, PSYNC is provided to optimize the efficiency of disconnection and reconnection 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326001018&siteId=291194637