Use MySQL Proxy to solve MySQL master-slave synchronization delay


Using MySQL Proxy to Solve the Delay of MySQL Master-Slave Synchronization

    MySQL's master-slave synchronization mechanism is very convenient to solve the application requirements of high concurrent reading, which brings great convenience to Web development. However, this method has a big flaw. The synchronization mechanism of MySQL relies on the Slave to actively send requests to the Master to obtain data, and due to server load, network congestion and other reasons, the data synchronization delay between the Master and the Slave is There is absolutely no guarantee. It may be as short as 1 second, as long as several seconds, tens of seconds or even longer.


Due to the existence of the data delay problem, when the application updates the data on the Master, and then immediately needs to read the data from the database, if the application fetches the data from the Slave at this time (this is also the current practice of web development), It may occur that the expected data cannot be read, causing the program to run abnormally.

There are various ways to solve this problem, such as the simplest, force sleep for a few seconds after all inserts and updates. This is a very rude way, and for small to medium systems where the update operation is not very high, this method basically solves the problem.

Another way is that the application saves the updated data in the local memory (or centralized cache), and if the data needs to be read directly after writing the data, it is read from the local memory. The disadvantage of this method is that it greatly increases the complexity of the application, and the reliability cannot be completely guaranteed.

Using MySQL Proxy can easily solve this problem. MySQL Proxy is an agent program based on MySQL Client and MySQL Server, which can monitor and modify requests sent by Client. From the Client's point of view, there is no difference between accessing the Server through the Proxy and accessing the Server directly. For the existing program, just replace the IP address and port number of the directly accessed Server with the IP address and port number of the Proxy.

The working principle of MySQL Proxy is also simpler. When the Proxy starts, you can specify the lua script that the Proxy needs to use, and implement 6 methods in advance in the lua script:

    * connect_server() // When receiving a connection request from the Client, call
    * read_handshake() //
    * read_auth() // Call * read_auth_result() when reading the client's authentication information // call
    * read_query() when reading the authentication result // call
    * read_query_result() when reading the client's query request
    // call

when the Proxy receives When the Client requests, the different methods above are called at different stages of the request. In this way, Proxy users can freely implement these six methods to achieve their goals according to their own business needs.

By adding code to read_query(), we can intercept whether the current request is insert, update or select, then send the insert and update requests to the Master, and send the select request to the Slave, thus solving the separation of read and write question.

After solving the separation of read and write, how to solve the synchronization delay?

The method is to add an auto-increment table on the Master, which contains only one field. When the Master receives any data update request, this trigger will be triggered, and the trigger will update the records in the auto-increment table. As shown below:

mysql_proxy_write

Since Count_table also participates in the master-slave synchronization of Mysq, Update updates made on the Master will also be synchronized to the Slave. When the Client reads data through the Proxy, the Proxy can first send a query request to the Count_table table of the Master and Slave. When the data of the two are the same, the Proxy can determine that the data status of the Master and Slave is consistent, and then send the select request. to the Slave server, otherwise it is sent to the Master. As shown in the figure below:

mysql_proxy_read

In this way, the problem of uncontrollable synchronization delay of MySQL can be compared perfectly. The reason why it is "perfect" is that this solution doubles the query request and puts additional pressure on the Master and Slave. However, since the Proxy is connected to the real Mysql Server using a connection pool, the extra pressure is acceptable.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326664275&siteId=291194637