最近在看 MySQL5.7 Manual, 有关 Semisynchronous Replication 这一块的内容, 我们知道, MySQL 默认的 Replication 是异步的, 何为异步? 何为半同步? 废话不多说, 直接看官方解释吧:
1. 背景知识
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.
-- 主库只管把 events 写入 binlog 中, 不管从库有没有收到.
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.
-- 介于异步复制和全复制之间, 主库仅仅只要等待至少一个从库收到和记录 events. 它不需要等待所有的从库告诉它收到 events, 也不需要从库执行和提交事物, 从库只是收到 events 就会告诉主库, 这样主库就可以提前提交事物了.
此外, 半同步也分两种, 有参数 rpl_semi_sync_master_wait_point 控制, 这里我就不多做解释了, 我们使用默认设置 after_sync, 这种数据零丢失
AFTER_SYNC (the default): The master writes each transaction to its binary log and the slave, and syncs the binary log to disk. The master waits for slave acknowledgment of transaction receipt after the sync. Upon receiving acknowledgment, the master commits the transaction to the storage engine and returns a result to the client, which then can proceed.
AFTER_COMMIT: The master writes each transaction to its binary log and the slave, syncs the binary log, and commits the transaction to the storage engine. The master waits for slave acknowledgment of transaction receipt after the commit. Upon receiving acknowledgment, the master returns a result to the client, which then can proceed.
2. 测试环境
Role | Hostname | IP | CPU | Memory | MySQL Version |
TPCC | sht-sgmhadoopcm-01 | 172.16.101.54 | 2Core | 8G | NO |
master | sht-sgmhadoopdn-01 | 172.16.101.58 | 2Core | 6G | 5.7.21 |
slave1 | sht-sgmhadoopdn-02 | 172.16.101.59 | 2Core | 6G | 5.7.21 |
slave2 | sht-sgmhadoopdn-03 | 172.16.101.60 | 2Core | 6G | 5.7.21 |
一个 master,slave1 和 slave2 都是 master 的直接从库.
分两种情况测试:
(1) 当 slave1 和 slave2 都是异步复制的时候
(2) 当 slave1 是半同步复制, slave2 是异步复制的时候
3. 压力测试
使用 TPCC 压力测试软件, 比较 TPS 和 QPS 来判断异步和半同步复制的性能差异到底有多大.
具体如何测试, 可以参考之前的博客: MySQL 压测 --TPCC 安装, 测试 http://blog.51cto.com/darrenmemos/2130202
来源: http://www.bubuko.com/infodetail-2658272.html