0%

MySQL Group Replication中同步的几个过程

Group Replication中同步的几个过程

MGR组复制大家都知道是一个数据强一致性的架构,但是这个强一致性是最终一致性,也就是在正常的复制过程中各个节点之间还是存在延迟,这篇文章目的是梳理下MGR的几个同步过程。

Group Replication数据同步的几个阶段

MGR中数据同步可以分为以下几个阶段:

  • locally applying
  • generating a binlog event
  • sending the binlog event to the slave(s)
  • adding the binlog event on the relay log
  • applying the binlog event from the relay log

这几个阶段中只有sending the binlog event to the slave这个过程是一个同步过程(要等待包过自己在内的大部分节点返回ACK),这个过程包含了数据冲突检测。

locally applying

这个阶段也就是本地事务执行,具体步骤如下:

首先开启事务

在事务内执行一些语句

generating a binlog event

这个阶段主要是生产binlog event ,用于MGR将他封装成write set 通过paxos原子广播到各个节点

当事务提交时会产生 binlog event

sending the binlog event to the slave(s)

发送binlog event到各个节点(其实也就是write set),但这个步骤是一个同步的过程, 这里就用了paxos协议将数据有序的广播出去,需要有包括自己在内的大多数节点返回ack之后才会继续

每个节点收到发送到的binlog event 就要在自己的本地做冲突检测

adding the binlog event on the relay log/applying the binlog event from the relay log

当node1节点,完成冲突检测后,node1节点上的事务commit就可以继续执行了

从上面这个图可以看出,node1上面可以继续commit了,不用等待别的节点完成冲突检测,或者是应用完事务

别的节异步的应用队列中的数据,并且可以看到数据在不同的节点上提交的时间也是不一样的

总结

通过上面的步骤图我们了解了MGR同步的几个阶段,知道了MGR的数据强一致性并不是需要等到每个节点都应用完事务,而是只在发送binlog
event做冲突校验这个阶段是同步的过程,其余阶段都是异步的,也就是各个节点读取到的数据可能存在延迟,所以在MySQL8.0.14中增加了group_replication_consistency参数,用于一致性读

参考资料:

https://lefred.be/content/mysql-group-replication-synchronous-or-asynchronous-replication/