Distributed topic | Three ways of Mysql master-slave replication you should know!

Continuing with us, we built the Mysql master-slave replication architecture. Today we will introduce three methods of master-slave replication, which will also be frequently asked during the interview:

Synchronous replication

Synchronous replication means that when the main library commits the transaction, the binlog has been transmitted to the relay log of the slave library through the dump thread. The main library needs to wait for the submission confirmation from the slave library. After the replay is completed, the slave library will reply an ACK to the main library. The main library then finishes waiting and performs subsequent operations. Note: If there are multiple slave nodes at this time, the longer the main library will wait, so it is necessary to set a timeout waiting time.

Insert picture description here

Asynchronous replication

In the asynchronous replication mode, in order to solve the problem of the long waiting time of the main library, the return confirmation is canceled. That is to say, after the main library submits the transaction, it does not need to wait for the submission confirmation from the slave library, and directly performs the subsequent operations and returns to the client;
but this It will cause such a problem: when the host submits the transaction and hangs up, but at this time binlog has not been synchronized to the slave library, if the master-slave switch is forced, the new master library data will be incomplete

Insert picture description here

Semi-synchronous replication

The semi-synchronous method is a compromise between the synchronous and asynchronous replication methods: the
same main library still needs to wait for the confirmation from the slave library before performing subsequent operations, but the difference is that this time it is not waiting for the slave library to submit the transaction before sending one The confirmation notification is sent to the main library, but when the slave library writes the binlog to the relaylog, it will send a confirmation notification to the main library. This not only reduces the waiting time but also maintains the security of the data

Insert picture description here

Enhanced semi-synchronous replication

In the case of semi-synchronous replication, if there is such a situation: after the main library submits the transaction, it starts to wait for the relay write return from the slave library. If the slave library has an accident at this time (it may be stolen), the master The library waiting timeout, this time the main library will directly end the waiting, because the transaction has been submitted, the user can definitely see the unsynchronized data, in order to solve this problem, mysql5.7 introduced enhanced semi-synchronous replication:
only when After receiving at least one relay log write confirmation returned from the library, the transaction is submitted, which means that the transaction is committed after the confirmation is received

Insert picture description here

Wechat search for a search [Le Zai open talk] Follow the handsome me, reply [Receive dry goods], there will be a lot of interview materials and architect must-read books waiting for you to choose, including java basics, java concurrency, microservices, middleware, etc. More information is waiting for you.

Guess you like

Origin blog.csdn.net/weixin_34311210/article/details/109980559