MySQL学习(二十四)主从同步-半同步

概述

MySQL 默认是异步复制,半复制是为了数据一致性,防止异步同步数据过程中,事务丢失。同步复制的话可以保证数据的一致性,数据不丢失可以回滚,但是响应慢,master 必须等待 slave 返回的ack响应才算完整地完成事务,而异步复制则有可能出现数据不一致的问题,半复制处于异步复制和同步复制的中间。
半复制只需要等待(默认)一台 slave 完成了写入就算完成事务请求了。

半复制过程

Semisynchronous replication can be used as an alternative to asynchronous replication:

- A slave indicates whether it is semisynchronous-capable when it connects to the master.

- If semisynchronous replication is enabled on the master side and there is at least one semisynchronous slave, a thread that performs a transaction commit on the master blocks and waits until at least one semisynchronous slave acknowledges that it has received all events for the transaction, or until a timeout occurs.

- The slave acknowledges receipt of a transaction‘s events only after the events have been written to its relay log and flushed to disk.

- If a timeout occurs without any slave having acknowledged the transaction, the master reverts to asynchronous replication. When at least one semisynchronous slave catches up, the master returns to semisynchronous replication.

- Semisynchronous replication must be enabled on both the master and slave sides. If semisynchronous replication is disabled on the master, or enabled on the master but on no slaves, the master uses asynchronous replication.

为了理解,以下列出和同步复制和异步复制的对比。

To understand what the “semi” in “semisynchronous replication” means, compare it with asynchronous and fully synchronous replication:

With asynchronous replication, the master writes events to its binary log and slaves request them when they are ready. There is no guarantee that any event will ever reach any slave.

With fully synchronous replication, when a master commits a transaction, all slaves also will have committed the transaction before the master returns to the session that performed the transaction. The drawback of this is that there might be a lot of delay to complete a transaction.

Semisynchronous replication falls between asynchronous and fully synchronous replication. The master waits only until at least one slave has received and logged the events. It does not wait for all slaves to acknowledge receipt, and it requires only receipt, not that the events have been fully executed and committed on the slave side.

参考资料 :

MySQL学习(二十四)主从同步-半同步

上一篇:mysql存储表情


下一篇:mongodb客户端操作常用命令