Detailed explanation of full replication and partial replication of redis

full copy

The principle of full replication

The principle of redis full replication is to first synchronize the RDB file of the master to the slave, and during synchronization, the commands written by the master will also be recorded (there is a replication buffer inside the master, which will record the newly added writes by the master during synchronization Enter) , when the slave loads the RDB, it will synchronize the values ​​written by the master to the slave through the comparison of the offsets.

Take a look at a complete replication flow chart:

1. The slave will first send a psync command to the master. The first parameter of this command is runId, and the second parameter is the offset. Since it is the first replication, the slave does not know the runId of the master, nor does it know itself Offset, at this time, a question mark and -1 will be passed to tell the master node that it is the first synchronization.

2. When the master receives psync? When -1, you know that the slave is going to copy in full, and it will inform the slave of its runID and offset.

3. The slave will save the master information

4. The master will then generate an RDB (bgsave)

5. Send RDB to slave

6. The operation of copying the buffer record is also sent to the slave

7.slave clears all its old data

8. The slave will then load the RDB file and copy the buffer data to complete the synchronization.

The cost of full replication

1. The overhead of bgsave, each time bgsave needs to fork the child process, the overhead of memory and CPU is very large

2. RDB file network transfer time (network bandwidth)

3. Time to clear data from the node

4. Time to load RDB from node

5. Possible AOF rewrite time (if our slave node enables AOF, AOF will be rewritten after RDB is loaded to ensure that AOF is up-to-date)

partial copy

Why Partial Copy? Before the redis2.8 version, if the network between the master and the slave had a jitter connection and disconnected, the slave would not know the master's actions at all, and there would be problems with synchronization. In order to ensure data consistency, wait for the network to recover. A full copy. The overhead of full replication is very large, and the redis 2.8 version has a partial replication function.

The implementation principle of partial replication:

When the master and the slave are disconnected, the master will record the operations performed during the period into the replication buffer area (which can be regarded as a queue whose size defaults to 1M). After the slave reconnects, the slave will send the psync command to the master and pass in the offset and runId. At this time, if the master finds the value of the offset transmitted by the slave, in the buffer queue range, it will start from the offset to the queue The finished data is passed to the slave to achieve synchronization and reduce the overhead of using full replication.

Guess you like

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