当前位置:网站首页>Redis master-slave replication - underlying principle

Redis master-slave replication - underlying principle

2022-06-13 07:34:00 A hard-working dog

Redis Master slave copy

adopt Slaceof Options , Let one server replicate another , The server to be replicated is the primary server , The server that replicates the primary server is called the secondary server .

SLAVEOF <IP><PORT>

The data of both the master and slave servers during replication will be the same , Call it data consistency .

Implementation of data consistency

SYNC command ( The old version )

The synchronization operation from the slave server to the master server needs to send the SYNC Command complete .

  1. Sent from the slave server to the master server SYNC command
  2. received SYNC The main server of the command executes BGSAVE command , Generate a... In the background RDB file , And use a buffer to record all write commands executed from now on .
  1. When the main server BGSAVE At the end of the command , The master server will BGSAVE Command generated RDB File sent to slave , Receive and load this... From the server RDB file , Update your own database state to the master server for execution BGSAVE Database state at command time .
  2. The master sends all write commands recorded in the buffer to the slave , Execute these write commands from the server , Update your database status to the current status of the primary server database .

Keep data consistent in real time

When the primary server is modified , The command propagation operation will be performed on the slave server : The master server will execute its own write commands , That is, the write command that causes inconsistency between the master and slave servers , Send to execute... From server , After executing the same write command from the server , The master and slave servers will return to the consistent state again .

Defects in the old copy function

When you drop the line from the server and reconnect , The primary server will execute again SYNC command , It will cause a lot of waste of resources .

PSYNC command ( The new version )

Redis Use PSYNC Command to realize synchronous operation during replication .

PSYN Commands have full synchronization and partial resynchronization .

  • Full resynchronization : Used to handle initial replication : Full resynchronization steps and SYNC The execution steps of the command are basically the same , All are By having the master server create and send RDB file , And send the write command saved in the buffer to the slave server for synchronization .
  • Partial resynchronization : Used to handle copying after disconnection : When the slave server reconnects to the master server after disconnection , If conditions permit , The master server can send the write command executed during the disconnection of the master-slave server to the slave server , Just receive and execute these write commands from the server , You can update the database to the current state of the primary server .

Time

master server

From the server

T0

The master-slave server synchronization is completed

The master-slave server synchronization is completed

T1

Execute and propagate commands SET K2 V2

Execute the SET k2 v2

.......

.......

........

T1000

The master and slave servers are disconnected

The master and slave servers are disconnected

T1001

perform SET K10086 V10086

Disconnection attempt reconnection

T1002

perform SET K10087 V10087

Disconnection attempt reconnection

T1003

The master and slave servers are reconnected

The master and slave servers are reconnected

T1004

Send to primary server PSYNC command

T1005

Return to the slave server +CONTINUE reply , Represents a partial resynchronization

T1006

receive +CONTINUE reply , Ready to perform partial resynchronization

T1007

To the slave server K10086 V10086、 K10087 V10087 command

T1008

Receive and execute two messages from the master server SET command

T1009

The master and slave servers complete the synchronization again

The master and slave servers complete the synchronization again

It's obvious that PSYNC Partial resynchronization ratio of the command SYNC Many fast resources are much less , because PSYNC For partial resynchronization, you only need to send the missing write command from the slave server to the slave server for execution .

原网站

版权声明
本文为[A hard-working dog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270548049438.html