“This is the 14th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

We have already explained the configuration of Redis, so let’s put these concepts together and talk about how master/slave replication works.

The operation of the system depends on three main mechanisms

  • When a Master instance is properly connected to a Replica instance, the master sends a series of command streams to keep the replica updated so that changes in its own data set are copied to the replica, including client writes, key expiration or evictions, etc.
  • When the connection between the master and replica is disconnected, the replica reconnects to the master because of a network problem or the replica realizes that the connection has timed out. This means that it will attempt to retrieve only command streams that were lost during the disconnection.
  • If partial resynchronization cannot be performed, the Replica requests full resynchronization. This involves a more complex process, for example the master needs to create a snapshot of all the data, send it to replica, and then continue to send the command stream to Replica as the data set changes.

How does Redis copy work

Each Redis master has a Replication ID: a large pseudo-random string that marks a given data set. Each master also holds an offset. The offset increases by the number of bytes sent by the master to the replica when it sends the replica stream to the replica. The purpose is to update the replica state when new operations are performed to modify the replica set.

The Replication offset increases even when no replica is connected to the master, so basically for each pair of Replication ids given, the offset identifies the exact version of a master data set.

When replica connects to the Master, it uses the PSYNC command to send the old Master Replication ID it recorded and the offsets it has processed so far. In this way, the master can send only the increment required by replica. However, if there is not enough backlog in the master buffer or if the replica references a replication ID that the master does not know about, a full resynchronization is performed instead: In this case, Replica gets a complete copy of the dataset and starts from scratch.

Speaking of which, what is full synchronization and what is incremental synchronization? We will explain this below. If you have different opinions or better idea, welcome to contact AH Q, add AH Q can join the technical exchange group to participate in the discussion!