Summary: MySQL 5.5 supports single-threaded mode replication, MySQL 5.6 supports library-level parallel replication, and MySQL 5.7 supports transactional-level parallel replication. With this mainline we can analyze the causes and consequences of MySQL and community development.
MySQL5.5, for replication we can understand this: the main library has a dump binlog thread constantly dump Binlog, and then the event unit to send from the library iothread,iothread The event received from the main library is written to Relaylog and then Sql_thread read Relaylog to replay the event as a transaction unit.
What about the MySQL 5.5 version, the problems encountered during our use, or the inconvenience?
First When the db pressure is large, the delay from the library is large, affecting the read-only service
Due to the development of new hardware, the introduction of SSD and the concurrent processing ability of multi-core Cpu,master node continue to improve, the slave node is completely in accordance with the Binlog write sequence of single-threaded playback, has not been able to keep up with the master node throughput capability.
Without considering the differences in the master-slave hardware configuration, the root cause of the delay is that the master pressure is too high and the slave is a single-threaded replay log. So to solve this problem, from the technology can be a single-threaded to multi-threading, take advantage of the advantages of parallelism, from the business can be removed from the library, some lines of business or functional modules out of the way, further we can split the table, Share the pressure on multiple master.
If we do not change the business situation, from the technical side to solve the problem in what direction:
Community Solutions: Ali Open Source Canal, parallel synchronization based on table level, can reduce synchronization delay time
Official Solution: A milestone version was released in October 2011 based on the schema-level parallel replication [MySQL5.6.3 (multi-threaded Slave)], and the group commit-based MySQL5.7 version, maximizing the parallelism of the primary library.
MySQL5.6, for replication we can understand that the main library has a dump binlog thread constantly dump Binlog, and then the event unit to send from the library iothread,iothread Received the event written by the main library Relaylog. "The things that followed and the MySQL5.5 changed", read the Relaylog by the coordinator thread, and then assign the transaction units to different work threads based on the different db. If the Binlog row event operates on objects of different schemas, parallel replication can be implemented without the DDL and foreign key dependencies determined.
MySQL5.7 can be said to be the largest restore of the main library of parallelism, on the basis of group commit, all the statements on the main library can be completed prepared statement that there is no data conflict, assigned to the same lastcommitted, Can be replicated in parallel on the slave node. So how does it identify those transactions that were submitted together? In fact, in the Gtid event added two fields "Int64 lastcommitted;int64 sequence number", when slave's coordinator thread distributes these event, Transactions with the samelast committed (the collection of event) can be sent to different work threads at the same time to achieve the purpose of parallel synchronization.
Summary: in parallel replication, according to the granularity of the three strategies, granularity from coarse to fine is by library, by table, by row. In these three comparisons, the degree of parallelism is increasing and the additional loss is also. Irrelevant large transactions do not affect the degree of concurrency. According to COMMIT_ID's strategy, the scope of application is wider and the additional consumption is low. The 5.7 improvement strategy is more concurrency-optimized. But there are big things that can be dragged down.
Then we only have one instance of the database, in which case we only have to split the table:
In this case, we can choose to do the sub-table at the application layer , or choose to have a middle tier . Different schemes have different merits and demerits.
The application layer has good performance, but the code is coupled in the business, if the subsequent expansion of the code will not be able to achieve a smooth expansion of the split, if there are multiple businesses need to achieve the same function, then will bring duplication of effort, and the difficulty of work also rose a step.
The middleware layer has good expansibility, low coupling, if the DB is enlarged and split, the application can do no sense, no change. Then there are some mature open source programs, such as Mycat,cobar,atlas,kingshard.
second , the master-slave switch brings a large complexity, need to calculate position or redo from the library
In general, our MySQL is a master multi-slave architecture, which can provide us with read-write separation, load balancing convenience, but also provide us with disaster-tolerant capabilities. But if our main library is hung up, then we will promote the library to the main library, but the change from the library to the new master brings about the micro-changes of the architecture. In order to make use of the above conveniences and provide disaster resilience, we have to rebuild multiple slave libraries for this new master. At this point, we have to know from the library that I should now start copying from that location in master, which means I have to get master's position. In order to get this position, we have two ways, a simple rough, redo slave; the other is to calculate the difference between the current data and the new master data through some columns complex calculation and back up the difference data, so as to get the new main library position, which brings the great challenge to the HA handoff and data protection.
OK, here MySQL from 5.5 single-threaded replication, to 5.6 schema-level-based replication, to 5.7 to maximize the parallelism of the primary repository is nearing completion. At the same time, we have also given some community, or non-technical solutions.
See the evolution of replication from MySQL 5.5 to 5.7