Application Architecture Evolution of Mysql on large websites _ MySQL

Source: Internet
Author: User
Preface: This article mainly describes the evolution of Mysql architecture at different concurrent access levels on the website. The scalability architecture is often closely related to concurrency. without the increase of concurrency, there is no need for a highly scalable architecture. here we will briefly introduce the scalability. Preface:

This article mainly describes the evolution of Mysql architecture under different concurrent access levels of the website.

Scalability

The scalability of the architecture is often closely related to concurrency. without the increase of concurrency, there is no need for a highly scalable architecture. here we will briefly introduce the Scalability. There are two common scaling methods:

Scale-up: Vertical scaling, which is achieved by replacing it with better machines and resources to improve service capabilities.

Scale-out: Scale out to improve service capabilities by adding nodes (machines.

For high-concurrency applications on the Internet, Scale-out is undoubtedly the way out. vertical purchase of higher-end machines has always been a problem we avoided, and it is not a long-term solution, in the scale out theory, what is the ideal state of scalability?

Ideal scalability

When a service is faced with higher concurrency, it can increase the concurrency supported by the service by simply adding machines, and there is no impact on online services (no down time) when the machine process is increased ), this is the ideal state of scalability!

Architecture Evolution

V1.0 simple website architecture

The architecture behind a simple small website or application can be very simple. data storage only needs one mysql instance to meet data reading and writing requirements (the data backup instance is ignored here ), websites that are in this period of time generally store all the information in a database instance.

In this architecture, let's look at the bottleneck of data storage?

1. total data size when a machine cannot be placed

2. data indexing (B + Tree) when the memory of a machine fails to fit

3. access (read/write hybrid) an instance cannot afford

Only when one or more of the above three things are met must we consider the next level of evolution. From this we can see that, in fact, for small applications of many small companies, this architecture is enough to meet their needs. the accurate evaluation of the initial data volume is an important part of eliminating excessive design, after all, no one is willing to waste their own experiences on impossible things.

Here is a simple example. for tables (with three indexes) such as user information, the 16 GB memory can put down indexes of about million rows of data, there is no problem with a simple mix of read and write traffic of about 3000/s. Is your application scenario

V2.0 vertical split

Generally, when V1.0 encounters a bottleneck, the simplest splitting method is vertical splitting. what is vertical? From the business point of view, data with poor relevance is split into different instances to eliminate bottlenecks. As an example, the user information data and business data are split into three different instances. For scenarios with many repeated read types, we can also add a layer of cache to reduce the pressure on the database.

In this architecture, let's look at the bottleneck of data storage?

1. the bottleneck described in V1.0 still exists for a single instance and a single business

When encountering bottlenecks, you can consider upgrading to a later version in this article. if a read request causes a performance bottleneck, you can consider upgrading to V3.0. if other bottlenecks cause a performance bottleneck, consider upgrading to V4.0.

V3.0 master/slave architecture

This architecture mainly solves the read problem in the V2.0 architecture. It migrates the read pressure by attaching real-time data backup to the Instance. in the Mysql scenario, it uses the master-slave structure, the master database is able to defend against write pressure and share the read pressure from the Slave Database. for applications with fewer writes and more reads, the master/slave architecture of V3.0 is fully competent.

In this architecture, let's look at the bottleneck of data storage?

1. write volume the master database cannot afford

Horizontal splitting of V4.0

When the V3.0 solution encounters a bottleneck, it can be solved through horizontal split. the horizontal split is different from the vertical split, A single instance has full data, and after horizontal split, all instances only have full 1/n data. take the Userinfo split as an example, split userinfo into three clusters. each cluster holds 1/3 of the total data, and the sum of the three clusters is equal to one complete data (note: it is no longer called a single instance but a cluster to represent a small mysql cluster containing the master and slave nodes)

How to route data?

1. Range split

Sharding key is routed by consecutive segments. it is generally used in scenarios with strict auto-increment ID requirements, such as a small example of Userid and Userid Range: use userid 3000 W as the Range to split cluster 1 userid 1-3000 W 2 cluster userid 3001 W-6000 W

2. List splitting

In the same way as the Range sharding, List sharding routes different sharding keys to different clusters. However, the specific methods are different, list is mainly used for sharding key generation when the sequence of not consecutive intervals falls into a cluster, as shown in the following scenarios:
Assume that there are 20 audio stores distributed in 4 regions with sales permissions, as shown in the following table:

Region

Store ID

North District

3, 5, 6, 9, 17

Eastern District

1, 2, 10, 11, 19, 20

West District

4, 12, 13, 14, 18

Central Area

7, 8, 15, 16

The business wants to organize all the data in a region for search. in this case, the List splitting can be done easily.

3. Hash splitting

Sharding key is divided by hash. common hash methods include division, String hash, and so on. division by userid % n determines which cluster to read and write data, other hash algorithms are not detailed here.

Problems introduced after data splitting:

Horizontal data sharding allows you to read and write data only by using the sharding key. for example, if you use userid as the sharding key, you must first read the details of userid, in this way, we can determine which cluster to query. if we need to search user information by username, we need to introduce an additional reverse index mechanism (similar to HBASE secondary index ), for example, store the usering of username-> userid on redis. the example of username query turns to first querying username-> userid, and then querying the corresponding information through userid.

In fact, this practice is very simple, but we should not ignore an extra hidden danger, that is, the hidden danger of data inconsistency. The username-> userid stored in redis and the userid-> username stored in mysql must be consistent. This guarantee is often difficult, for example, you need to modify redis and mysql at the same time to modify the user name. These two things are hard to guarantee the transaction, for example, if the mysql operation succeeds but the redis operation fails (the cost of introducing distributed transactions is high), availability is the most important for Internet applications, and consistency is the second, therefore, we can tolerate a small amount of inconsistencies. after all, the proportion of such inconsistencies can be negligible (generally, the write update also uses mq to ensure that the retry operation is stopped until the operation is successful)

In this architecture, let's look at the bottleneck of data storage?

The architecture built on this split concept does not theoretically have a bottleneck (the sharding key can ensure that the traffic of each cluster is relatively balanced), but it is really disgusting, that is the cost of redoing data during cluster resizing. for example, I originally had three clusters, but now my data is growing fast. I need six clusters, so we need to split each cluster into two. the general practice is

1. extract the next slave and stop synchronization,

2. incremental log of write records (in this way, the business side can write persistent mq or mysql master to create trigger record writes for multiple write operations)

3. start to perform data on the static slave, one split into two

4. play back the incremental write, and keep the synchronization with the original cluster.

5. write switchover, switching from the original 3 cluster to 6 cluster

Is there a similar feeling of air refueling? this is a dirty, tiring, and prone to problems. to avoid this, we generally started from the very beginning, design enough sharding clusters to prevent possible cluster resizing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.