In general, when the MySQL database slows down, we can use some practical operations to make it more practical. What methods can be used to achieve this purpose? The following articles describe how to fulfill these beautiful aspirations.
After reading this series of articles, it will help you broaden your horizons and better plan your future needs. The first article in this series is "faster and stronger ".
MySQL discusses query optimization and hardware adjustment, including adding additional server and application changes. This article describes how to make your MySQL bigger and better through partitioning and load balancing solutions.
Larger MySQL
Adding more MySQL instances is an effective way to speed up application response. If your server has multiple CPUs, sufficient memory, and fast hard disks, A considerable portion of these resources are idle. In this case, multiple MySQL instances can be run on the server at the same time, because MySQL has only one process and multiple session threads by default, therefore, the maximum hardware resources it can use are limited.
If your server is saturated, you must add more servers, no matter whether multiple MySQL instances are on one server or multiple servers, you need to configure a method for the application to know which server the query is sent to. If you want to modify the data, you should send the command to the master database instance, if it is only a query operation, you can send it to any MySQL database instance.
1. Data Partition and horizontal Partition
Because many Web applications recognize users through sessions, it makes sense to allocate them to different slave database instances through sessions. For example, a A-G, A H-O database instance may be working, and users can be assigned to different servers through the hash of the user name or userid, which is called the partition key, be careful when selecting the partition key.
It will affect how you build slave database instances, mainly to consider how to make these servers take on average workload, if the selection is not good, if the slave database is dumped, it may also cause data interruption.
If you are using this partition, you need to decide which MySQL database to use when the program runs. This can be achieved through a middle layer such as the MySQL proxy. Although it is still in the Alpha stage, it has a good idea, many people have used it in the production environment. It runs on the server and responds to requests on port 3306, then, some logic implemented by these queries through high-speed languages such as lua is forwarded to the appropriate backend servers.
Second, you can also specify the server to which the query is sent in the application. This is also the most flexible method. you can completely control the entire decision-making process, or you can use master_pos_wait to check the slave database instance, check whether they have enough computing resources. The programming language or Web framework you are using may also provide support in this regard. If you are not clear about it, You can query their documents.
You can also study Continuent tunsten, DBIx: DBClusterforPerl and SQLRelay. They support many different programming languages and MySQL databases. Similarly, CMS, such as Drupal, also supports multiple Read-Only slave databases. You only need to enable this function.
Another thing to consider when using this architecture is whether to use the primary database instance and when to use it. Generally, all the insert, update, and delete operations should be completed on the primary database instance, all query operations are performed on the slave database instance. For example, if a user comments a blog post and directly uses the slave database, it may not be completed because the MySQL replication architecture lags behind, at this time, the blog post may not be available from the database.
Checking outdated data is a better way. If you have a REPORT query that runs at night, this method may work well. You just need to make sure that the replication can catch up with the progress.
Another method is to track database changes by version number and determine whether the data is the latest version before reading the data.
Finally, MySQL provides the master_pos_wait function to determine the time from which the database is updated.
2. Function partitioning
You may have used functional partitions. When using functional partitions, you need to create a copy of the production MySQL database for different purposes, such as one for the data warehouse and report, and the other for text search.
Use Server Load balancer to make MySQL better
If your slave database already has some read-only data, you may need to implement load balancing and evenly distribute the traffic to each slave database. There are multiple implementation methods, such as random allocation and least connection, the fastest response speed method or a weighted average method. Although some hardware Load Balancing Devices can provide Load Balancing functions, they are often designed to balance network traffic, database-related balancing functions are not provided.
Fortunately, there are many software solutions, and The LVS project is a good candidate. It has developed quite mature and stable. It provides load balancing algorithms similar to DNS round robin, however, the implementation speed at the IP layer is very fast. In addition, many projects are built on LVS, including wackamole, which is based on peer-to-peer networks. Therefore, no single point of failure (spofs) will occur. A recommended project is ultramonkey.
Summary
MySQL provides many advanced features to achieve unlimited scale expansion. Different application environments have different best solutions. Therefore, before using it to produce MySQL databases, we recommend that you first fully understand the various solutions and try to build a testing platform with the same load as the production environment for testing.