Turn: talk about three high Mysql cluster architecture, the so-called three high is "high availability", "high load", "high performance" framework program.

from:https://www.toutiao.com/i6717521873397088780/?timestamp=1569389190&app=news_article&group_id=6717521873397088780&req_id=2019092513263001002607901724F149F2

table of Contents

  1. Foreword
  2. Master-slave architecture
  3. MHA architecture
  4. PXC program
  5. MHA given PXC
  6. The final recommendations
  7. to sum up

Foreword

Small partners in project development, keep up the database can not be avoided dealing generally used by companies in the Internet database is Mysql, and start-up companies have adopted the single mode.

This way you can play, applied to the actual project, it will certainly be being criticized.

On the one hand data insecurity, in case of computer disk database is broken, they pit.

On the other hand concurrency database is limited, the number of concurrent generally 200 to 500 on the same subject, of course, you want to continue to add up, is also possible, it would affect the overall response time of Mysql.

Today the old care to say something about the three high Mysql cluster architecture, the so-called three high is "high availability", "high load", "high performance" framework program.

Old Gu explain here, just introduced a whole cluster solution from above, it will not be so deep; but speaks some online missing, and very important idea. It is important to understand the overall architecture, the details look yourself.

Master-slave architecture

Mysql master-slave architecture is most likely to think, first to a map:

Female programmers asked this question, let me think for a long time, Mysql "three high" architecture

 

Master-slave scheme is used in many ways our middleware, from the master Mysql embodiment, data from the master to the slave Mysql Mysql synchronized, the data synchronization is unidirectional.

There are several synchronization schemes (asynchronous, synchronous, semi-synchronous), many online introduction, the old Gu is not talked about in detail.

Main features of the program:

1, solve the problem of data security

2, in conjunction with a number of middleware (eg: mycat) or tools (eg: sharding-jdbc) separation of read and write; Mysql improve the overall performance / load

NOTE: separate read and write Meaning: the updated data (i.e., write request) is operated master Mysql, then synchronized to the data from the Mysql; read data (read request) is accessed from Mysql.

We can see from the figure, from a multi-master's program in only a write node (main mysql), once the main Mysql problems, the entire system can not write a request, it is certainly not acceptable.

Mysql single master node failure problems, how to solve? Internet, said there are many programs, only the old Gu introduced, more mature program makers adopted.

MHA program

MHA stands for Master High Availability, from the name above you can see its role is to solve the problem of availability of the master node.

Currently in terms of high availability MySQL MHA is a relatively mature solution, which was developed by the Japanese, it is a good switch as a failure at the primary and MySQL high availability environments from an elevated high availability software. MHA can be done automatically within 0-30 seconds of switching operation of the failed database.

MHA consists of two parts: MHA Manager (management node), and the Node MHA (node ​​data), as shown:

Female programmers asked this question, let me think for a long time, Mysql "three high" architecture

 

Play a major role in monitoring the management node, if we find the master node is unavailable, you initiate the transfer from the main switching and faults.

MHA is currently the main support is a master multi-slave architecture that requires at least three nodes, two from a master.

That MHA hang after the master node, is how the switch?

1, the master node hung up in the re-election of a new candidate master node from the node, the principle is the newest binlog recently updated the new candidate node as master node.

2, in an alternative synchronization master node and the other from the difference between the log relay node (relay log)

3, the application stores binary logs from the original primary node

4, an alternative lifting master node is the new master

5, migration from other cluster nodes from the node as new master.

Described above is the core of the process, in fact, inside the MHA done a lot of business, the core idea is to ensure data consistency as much as possible, to prevent data loss. While the MHA has done well, but some scenes still can not be avoided.

Another problem is that the data is synchronized to the master node from the node is delayed, especially at high concurrency, the same moment the master node and be inconsistent data from the node.

Mysql in this respect also a lot of optimizations, such as semi-synchronous mode, the 5.7 version added after sync mode to ensure data consistency, but there is still more data from the delay between nodes.

Is there a program that can ensure strong data consistency? We then look down.

PXC program

PXC是percona公司的percona xtraDB cluster,简称PXC。它是基于Galera协议的高可用集群方案。可以实现多个节点间的数据同步复制以及读写,并且可保障数据库的服务高可用及数据强一致性。

Female programmers asked this question, let me think for a long time, Mysql "three high" architecture

 

PXC架构中Mysql无主从之分,都是相同的。而且每个节点都是能够提供读和写,是不是很酷,那PXC是怎么实现各个节点数据强一致性的呢?

Female programmers asked this question, let me think for a long time, Mysql "three high" architecture

 

上面是个时序图,就是PXC执行的流程,小伙伴们是不是感觉很复杂,老顾可以教大家可以这样理解:

其实就是一句话,PXC的原理其实在提交事务时,确保所有的节点事务都要成功提交,才返回成功;如果其中有一个不成功,就回滚数据,返回不成功,

正因为这样的原理,就确保数据肯定是一致的,而且是实时一致;当然这样就导致性能有损耗。PXC另一个好处就是每个节点都可以提供读写请求,不管写在哪个节点,都能够保证数据强一致性。

MHA与PXC

1、MHA主要写入速度很快,但数据不是强一致性

2、PXC保证数据强一致性,但写入速度慢

那有没有取他们优点的方案呢?来一个终极方案。老顾告诉小伙伴们,其实很多方案不可能都是优点,没有缺点,不可能很完美,最主要的是要知道在什么场景下运用什么方案。

根据MHA 和 PXC方案的特点,我们可以结合自己的业务去决定怎么使用它们?

PXC适合存储高价值的数据,要求数据强一致性,如:账户,订单,交易等等

MHA适合存储低价值的数据,不要求强一致性,如:权限,通知,日志,商品数据,购物车等等

现实情况,很多大厂都是结合使用的,我们看看2017年天猫双11,数据库峰值4200万次/秒,支付峰值25.6万次/秒;这个支付峰值已经创造了一个世界记录(国人的骄傲)。

我们发现支付场景的峰值相对其他业务的峰值比较低,这个是因为支付场景肯定是要求数据强一致性的,只要涉及到钱,用户都会很在意。

最终推荐方案

Female programmers asked this question, let me think for a long time, Mysql "three high" architecture

 

两种方案的结合,因为PXC架构都可以写,所以在入口处放一个HAProxy作负载均衡,客户端只要访问HAProxy的地址就行了,不需要知道每个PXC节点的地址。

In the database access middleware, there will certainly have to conduct business package, the design must be clear, what data into the cluster environment which, of course, to do a good point, you can configure oriented (it is not necessary, because the business is actually determined)

to sum up

Many small partners on the Internet like to ask, what a good program, which program is not good, there is no take-all world of a program, this is unrealistic, the choice of many programs is to combine related business scene.

About MHA and PXC many details, the old Gu is not presented here, there are many online introduction, you can look yourself.

Hopefully this article can open up your mind, thank you! ! !

Guess you like

Origin www.cnblogs.com/liuqingsha3/p/11584116.html