西维蜀黍

【MySQL】MySQL in Docker

Context

容器的定义:容器是为了解决“在切换运行环境时,如何保证软件能够正常运行”这一问题。

目前,容器和 Docker 依旧是技术领域最热门的词语,无状态的服务容器化已经是大势所趋,同时也带来了一个热点问题被大家所争论不以:数据库 MySQL 是否需要容器化

认真分析大家的各种观点,发现赞同者仅仅是从容器优势的角度来阐述 MySQL 需要容器化,几乎没有什么业务场景进行验证自己的观点;反过来再看反对者,他们从性能、数据安全等多个因素进行阐述 MySQL不需要容器化,也举证了一些不适合的业务场景。之前推送过一篇文章:

下面,我们就聊一下 Docker 不适合跑 MySQL 的 N 个原因!

  ...


【Engineering】服务降级(Downgrade)

Background

When the issue is coming from our service (like a bug in our code), usually we need to fix the issue ourself quickly. When, the issue is coming from external party, usually we can’t fix the issue directly, we need other team to fix the issue for us. In other hand, we have some downgrades that we can do to reduce the impact of the issue. For example, if there is an issue with voucher recommendation service, we can downgrade the API call to voucher recommendation service. By doing this, we won’t get the error from voucher recommendation service. Initially, when the voucher recommendation service returns us an error, we will return an error to the buyer, but when we downgrade the service, we will skip the voucher recommendation. As a result, the buyer might not get the best voucher, but it is better than they can’t checkout at all.

  ...


【Store】Strong Consistency Store

  ...


【MySQL】Replication - GTID

Background

In MySQL 5.5, resuming a broken replication setup required you to determine the last binary log file and position, which are distinct on nodes if binary logging is enabled. If the MySQL master fails, replication breaks and the slave will need to switch to another master. You will need to promote the most updated slave node to be a master, and manually determine a new binary log file and position of the last transaction executed by the slave. Another option is to dump the data from the new master node, restore it on slave and start replication with the new master node. These options are of course doable, but not very practical in production.

How GTID Solves the Problem

GTID (Global Transaction Identifier) provides a better transactions mapping across nodes. In MySQL 5.5. or before, Replication works in such a way that all nodes will generate different binlog files. Binlog events are the same and in the same order, but binlog file offsets may vary. With GTID, slaves can see a unique transaction coming in from several masters and this can easily be mapped into the slave execution list if it needs to restart or resume replication.

Every transaction has a unique identifier which identifies it in the same way on every server. It’s not important anymore in which binary log position a transaction was recorded, all you need to know is the GTID: ‘966073f3-b6a4-11e4-af2c-080027880ca6:4’. GTID is built from two parts - the unique identifier of a server where a transaction was first executed, and a sequence number. In the above example, we can see that the transaction was executed by the server with server_uuid of ‘966073f3-b6a4-11e4-af2c-080027880ca6’ and it’s 4th transaction executed there. This information is enough to perform complex topology changes - MySQL knows which transactions have been executed and therefore it knows which transactions need to be executed next. Forget about binary logs, it’s now all in the GTID.

All necessary information for synchronizing with the master can be obtained directly from the replication stream. When you are using GTIDs for replication, you do not need to include MASTER_LOG_FILE or MASTER_LOG_POS options in the CHANGE MASTER TO statement; instead, it is necessary only to enable the MASTER_AUTO_POSITION option.

GTIDs (global transaction identifiers) - Transaction-based Replication

GTID (global transaction identifier) 即全局事务 ID,一个事务对应一个 GTID,保证了在每个在主库上提交的事务在集群中有一个唯一的 ID。

When using GTIDs, each transaction can be identified and tracked as it is committed on the originating server and applied by any replicas; this means that it is not necessary when using GTIDs to refer to log files or positions within those files when starting a new replica or failing over to a new source, which greatly simplifies these tasks. Because GTID-based replication is completely transaction-based, it is simple to determine whether sources and replicas are consistent; as long as all transactions committed on a source are also committed on a replica, consistency between the two is guaranteed.

  ...


【MySQL】Replication - Binlog-based

Binary Log file - position-based Replication

MySQL refers to its traditional replication method as binary log file position-based replication, where the MySQL instance operating as the source (where the database changes take place) writes updates and changes as “events” to the binary log. The information in the binary log is stored in different logging formats according to the database changes being recorded. Replicas are configured to read the binary log from the source and to execute the events in the binary log on the replica’s local database.

Each replica receives a copy of the entire contents of the binary log. It is the responsibility of the replica to decide which statements in the binary log should be executed. Unless you specify otherwise, all events in the source’s binary log are executed on the replica. If required, you can configure the replica to process only events that apply to particular databases or tables.

Each replica keeps a record of the binary log coordinates: the file name and position within the file that it has read and processed from the source. This means that multiple replicas can be connected to the source and executing different parts of the same binary log. Because the replicas control this process, individual replicas can be connected and disconnected from the server without affecting the source’s operation. Also, because each replica records the current position within the binary log, it is possible for replicas to be disconnected, reconnect and then resume processing.

  ...