PostgreSQL的同步级别与MySQL的半同步after_sync比较

MySQL的半同步中通过binlog进行流复制,同步级别和PostgreSQL对比可以发现:

PostgreSQL                MySQL

off

local                         after_commit         

remote_write            after_sync

remote_apply

on

所以,整体来说MySQL的日志同步上还是没有PostgreSQL做的严谨,在金融系统中,PostgreSQL的日志同步级别都是设置为on,即日志接收到,apply,然后等待数据刷盘才返回commit的ack。

Specifies whether transaction commit will wait for WAL records to be written to disk before the command returns a “success” indication to the client. Valid values are on, remote_apply, remote_write, local, and off. The default, and safe, setting is on. When off, there can be a delay between when success is reported to the client and when the transaction is really guaranteed to be safe against a server crash. (The maximum delay is three times wal_writer_delay.) Unlike fsync, setting this parameter to off does not create any risk of database inconsistency: an operating system or database crash might result in some recent allegedly-committed transactions being lost, but the database state will be just the same as if those transactions had been aborted cleanly. So, turning synchronous_commit off can be a useful alternative when performance is more important than exact certainty about the durability of a transaction. For more discussion see Section 30.3.

If synchronous_standby_names is non-empty, this parameter also controls whether or not transaction commits will wait for their WAL records to be replicated to the standby server(s). When set to on, commits will wait until replies from the current synchronous standby(s) indicate they have received the commit record of the transaction and flushed it to disk. This ensures the transaction will not be lost unless both the primary and all synchronous standbys suffer corruption of their database storage. When set to remote_apply, commits will wait until replies from the current synchronous standby(s) indicate they have received the commit record of the transaction and applied it, so that it has become visible to queries on the standby(s). When set to remote_write, commits will wait until replies from the current synchronous standby(s) indicate they have received the commit record of the transaction and written it out to their operating system. This setting is sufficient to ensure data preservation even if a standby instance of PostgreSQL were to crash, but not if the standby suffers an operating-system-level crash, since the data has not necessarily reached stable storage on the standby. Finally, the setting local causes commits to wait for local flush to disk, but not for replication. This is not usually desirable when synchronous replication is in use, but is provided for completeness.

If synchronous_standby_names is empty, the settings on, remote_apply, remote_write and local all provide the same synchronization level: transaction commits only wait for local flush to disk.

This parameter can be changed at any time; the behavior for any one transaction is determined by the setting in effect when it commits. It is therefore possible, and useful, to have some transactions commit synchronously and others asynchronously. For example, to make a single multistatement transaction commit asynchronously when the default is the opposite, issue SET LOCAL synchronous_commit TO OFF within the transaction.

下面转自:https://www.jianshu.com/p/3bfb0bfb8b34

今天主要剖析一下MySQL 5.7增强半同步的AFTER SYNC和AFTER COMMIT的区别。

如果我们生产库对数据的一致性要求比较高,那么我们一般会开启了半同步复制,但在MySQL5.5/5.6里,会存在数据不一致的风险。比如有如下场景,客户端提交了一个事务,master把binlog发送给slave,在发送的期间,网络出现波动,此时Binlog Dump线程发送就会卡住,要等待slave把binlog写到本地的relay-log里,然后给master一个反馈,等待的时间以rpl_semi_sync_master_timeout参数为准,默认为10秒。在这等待的10秒钟里,在其他会话里,查看刚才的事务是可以看见的,此时一旦master发生宕机,由于binlog没有发送给slave,前端app切到slave查看,就会发现刚才已提交的事务不见了。盗用两张图讲解一下两者的区别。

  • AFTER SYNC
    after sync是MySQL5.7官方新加的用以解决MySQL5.6半同步缺陷的选项,也是官方推荐的方式。下面我结合图来说明一下AFTER SYNC是怎么回事。

    image

    实际上,客户端发出commit请求后,在主库上写入binlog并推送给slave,slave接收到binlog并写入relaylog,发送ACK确认已经接收binlog后,master在引擎层commit,客户端接收commit完成,此时其他会话才可以看见已提交的数据。
        故障分析:假设master在接收ACK确认时宕机,因为在引擎层并没有提交,HA切换到从库,因为binlog已经写入从库的relaylog,因此不会造成数据丢失,个人认为是目前比较完美的解决方式。

  • AFTER COMMIT

    image

    after commit是MySQL5.6半同步参数,区别于after sync,after sync是在接收ack确认以后主库在引擎层做提交,而after commit是先在引擎层做提交后等待ACK确认。因此,在写入数据后并且在从库确认之前,其他的客户端可以看到在这一事务。
        故障分析:
    1.binlog 未发送到从库:
        事务B获取到事务A提交的内容, 此时宕机故障切换到slave,事务B获取到的内容却丢失了。事务A commit没有收到反馈信息(则需要业务判断了)。
    2.binlog 已经发送给从库 :
        事务B获取到事务A提交的内容,故障切换到salve ,B仍然获取到A提交的内容,没毛病。事务A commit没有收到反馈信息,若重新执行该事务,则相当于执行两次A事务(则需要业务判断了)。

dump thread过程分析:

  • mysql5.6版本之前:
    1. master dump thread 发送binlog events 给 slave 的IO thread,等待 slave 的ack反馈
    2. slave 接受binlog events 写入relay log ,返回 ack 消息给master dump thread
    3. master dump thread 收到ack消息,给session返回commit ok,然后继续发送写一个事务的binlog。
  • mysql5.7之后新增ack线程:
    1. master dump thread 发送binlog events 给 slave 的IO thread,开启ack线程等待 slave 的ack反馈,dump 线程继续向slaveIO thread发送下一个事务的binlog。
    2. slave 接受binlog events 写入relay log ,返回 ack 消息给master ack线程,然后给session返回commit ok。

过程总结:

1.Master在收到slave的应答后才Commit事务--after_sync(5.6上Master在commit后,才等待Slave的应答--after commit).
因此在确认事务复制到Slave上之前,并发的事务看不到当前事务的数据.当Master出现故障时,所有已经提交的事务都复制到了Slave上.
2.缺省采用无数据丢失的应答等待机制after_sync。用户也可以选择使用5.6的应答等待机制after_commit

设置方法:

mysql> SET rpl_semi_sync_master_wait_point= AFTER_SYNC;

Master接收到N个slave的应答后,才commit 事务.
用户可以设置应答Slave的数量:

mysql> SET GLOBAL rpl_semi_sync_master_wait_for_slave_count= N;

原文地址:https://www.cnblogs.com/kuang17/p/11331969.html

时间: 2024-08-03 01:06:39

PostgreSQL的同步级别与MySQL的半同步after_sync比较的相关文章

MariaDB(MySQL):半同步复制+ssl+复制过滤

一.半同步复制   1.mysql的复制 通过记录主服务器的二进制日志,并在从服务器上进行重放(replay)完成复制,默认都是异步进行的. 2.半同步复制 半同步复制是google贡献给MySQL的一个补丁,在MySQL 5.5之后就支持了,MariaDB都是支持的. MySQL的插件,一般在MySQL安装目录下; 半同步复制插件默认没有启用,需要自己安装,/usr/local/mysql/lib/plugin可以看到semisync_master.so和semisync_slave.so和其

mysql 主从半同步模式和数据库同步过滤

在mysql主从架构中,默认采用的是异步模式,也就是在master中将数据保存在数据库,再将操作写到bin-log中即响应给客户端.至于slave是否同步了二进制文件,是否完成了本地操作,master无从得知.异步模式固然能以最快的速度响应给客户端,减少用户的等待时间,但在一些数据同步.安全性较高的场景,要求slave中的数据要尽最大能力与master保持一致,那么半同步模式就可以用上了. mysql的半同步模式是以插件的方式由google提供的.主要文件在${mysql_home}/lib/p

mysql GTID 半同步复制

1)什么是GTID GTID(Global Transaction ID)是对于一个已提交事务的编号,并且是一个全局唯一的编号.GTID实际上是由UUID+TID组成的.其中UUID是一个MySQL实例的唯一标 识,保存在mysql数据目录下的auto.cnf文件里.TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增.下面是一个GTID的具 体形式:3E11FA47-71CA-11E1-9E33-C80AA9429562:23.2)GTID的作用 根据GTID可以知道事务最初是在哪

MySQL主从复制半同步复制原理及搭建

在MySQL5.5之前的版本中,MySQL的复制是异步复制,主库和从库的数据之间存在一定的延迟,比如网络故障等各种原因,这样子容易存在隐患就是:当在主库写入一个事务成功后并提交了,但是由于从库延迟没有及时得到主库推送的Binlog日志时,主库突然宕机了,那么此时从库就可能损失这个事务,从而造成主从不一致的状况. 因此我们MySQL5.5版本之后引入了半同步复制的概念 半同步复制的原理: 半同步复制时,为了保证主库上的每一个Binlog事务都能够被可靠的复制到从库上,主库在每次事务成功提交时,并不

MySQL增强半同步的搭建实验,和一些参数的个人理解

环境信息 role ip port hostname master 192.168.188.101 4306 mysqlvm1 slave 192.168.188.201 4306 mysqlvm1-1 5306 6306 7306 MySQL版本 5.7.26 前置条件 已配置好主从复制. 配置增强半同步 1.加载lib,所有主从节点都要配置. 主库:install plugin rpl_semi_sync_master soname 'semisync_master.so'; 从库:inst

MySQL的半同步复制

1>半同步复制的出现: 默认情况下,复制是异步的,就是客户端提交事务给主库,主库将事务写入到存储引擎和binlog中后会立即返回给客户端告诉其事务执行成功.如果此时该事务还未来得及复制到从库上,如果主库在此时发生崩溃或者服务器宕机,会导致主从切换,此时客户端访问新选举的主库时,就会看不到刚提交的数据. 2>半同步复制的原理: mysql5.5开始通过插件的方式支持半同步复制,主库执行完客户端提交的事务后不会立即返回给客户端,而是等待至少一个从库接收到该事务后才返回给客户端. 半同步复制是全同步

MySQL数据库半同步复制

半同步复制,是有一个从节点或者一部分从节点与主节点之间是同步复制的,其他的从节点仍是异步复制 半同步复制是谷歌公司贡献给MySQL的一个插件,默认在MySQL中没有此插件,所以要实现主从的版同步复制需要安装此插件 rpm -ql mariadb-server| grep semi #找到需要安装的插件,以so结尾 SHOW PLUGINS; #查看当前支持的插件,此处也能看到myisam和innodb也是插件类型 下面开始介绍如何配置主从版同步复制: 1.创建传统的主从复制功能的mysql,请参

mysql的主从、主主及半同步复制

mysql复制 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重新执行一遍来实现的.复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环.这些日志可以记录发送到从服务器的更新.当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置.从服务器接

mysql半同步复制的实现

mysql半同步复制和异步复制的差别如上述架构图所示:在mysql异步复制的情况下,Mysql Master Server将自己的Binary Log通过复制线程传输出去以后,Mysql Master Sever就自动返回数据给客户端,而不管slave上是否接受到了这个二进制日志.在半同步复制的架构下,当master在将自己binlog发给slave上的时候,要确保slave已经接受到了这个二进制日志以后,才会返回数据给客户端.对比两种架构:异步复制对于用户来说,可以确保得到快速的响应结构,但是