Tencent cloud database Domestic database topic online technology salon is in hot progress, March 17, Zheng Han’s sharing has ended, the partners who did not have time to participate do not worry, the following is the live video and text review.
Pay attention to “Tencent Cloud database” public account, reply “0317 Zheng Han”, you can download live PPT sharing.
Click here for a live review
So without further ado, let’s get to today’s share. Today’s topic is “Smooth Capacity Expansion under 100 million Traffic Scenarios: TDSQL Horizontal Expansion Scheme Practice”.
Today’s sharing will mainly include the following four parts:
The first part firstly introduces the background of horizontal expansion, mainly introduces why horizontal expansion, mainly compares with vertical expansion, and talks about the general problems we will encounter in horizontal expansion.
Part 2 will briefly introduce how TDSQL does horizontal expansion, so that you have an intuitive impression.
Part 3 will describe in detail the design principles behind TDSQL horizontal expansion, mainly in parallel with Part 1, to see how TDSQL solves common horizontal expansion problems.
The fourth part will introduce practical cases.
I. Background and challenges of database horizontal expansion
First, let’s look at the background of horizontal expansion. The reason of expansion is very straightforward, in general is mainly as business visits, or is the need to scale, while the existing capacity or capability to meet the needs of the business, mainly in TPS, insufficient QPS or delay more than the tolerance range of the business, or the existing capacity can’t meet the requirements, the latter mainly refers to the disk or network bandwidth. Usually when we have this kind of problem, we have to expand. In terms of capacity expansion, there are two common ways, one is vertical capacity expansion, the other is horizontal capacity expansion. These two kinds have different characteristics, advantages and disadvantages are also very obvious.
1.1 Horizontal Expansion VS Vertical Expansion
First, let’s look at vertical expansion. Vertical expansion, mainly to improve the configuration of machines, or improve the configuration of instances. Because, as we know, the purchase of a database or an instance on the cloud is actually allocated on demand, that is to say, for users, the current business may not be large, only need two CPUS or a few gigabytes of memory; As his business grows, he may need to expand this instance, and he may currently need 20 cpus, or 40 GIGABytes of memory.
At this point, in the cloud we can control resources to dynamically adjust them to meet business needs — that is, we can dynamically add cpus to the same machine. The limit of capacity expansion is that when the entire machine is given CPU and memory, if it is not enough, it needs to prepare a better machine for capacity expansion. In MySQL, you can perform a master/slave switchover: select a slave and synchronize data; After data synchronization is complete, make a master/slave switch so that you can use the current better machine.
As you can see, during the whole process, there is basically no impact on the business — the primary/secondary switchover, if the IP is changed, it is in the way of front-end or VIP, and there is basically no impact on the business. One of its biggest downsides is that it relies on stand-alone resources: you can give it a better machine that meets a certain number of requirements. And as the business grows more rapidly, you’ll find that the best machines you can offer today may not be enough. Therefore, the biggest disadvantage of vertical expansion is that it depends on the resources of a single machine.
1.2 Horizontal Capacity Expansion
The other way to do it, as opposed to vertical expansion, is what we call horizontal expansion. The biggest advantage of horizontal capacity expansion is that it solves the problem of vertical capacity expansion. Theoretically, horizontal capacity expansion can be infinite, and it can dynamically adapt to service requirements by adding machines.
Compared with vertical expansion, horizontal expansion can solve the problem of vertical expansion, but it will introduce some other problems. Since horizontal expansion is more complex than vertical expansion, we will analyze the problems we may encounter and introduce TDSQL solutions later:
First of all, in vertical capacity expansion, after system capacity expansion, data still exists on one node in general. In an active/standby architecture, all data is stored on the standby machine. In the process of horizontal expansion, data will be split. The first question is: How to split data? Because if you don’t split well, when hot data is present, you can end up with a single node that stores hot data becoming a performance bottleneck, even though the data has been split into many pieces.
Second, in the whole process of horizontal expansion, data relocation and routing changes will be involved. So can the whole process be done without business awareness? Or how intrusive is it to the business?
Third, in the whole expansion process, because there are so many steps, if one of them fails, how can you roll back? In addition, how to ensure high data consistency during switchover during capacity expansion?
Furthermore, after capacity expansion, how can we ensure the performance after capacity expansion because the data is divided into various nodes? Because in theory, we want a linear increase in performance as we increase the number of machines, which is ideal. In fact, in the whole process of horizontal expansion, different architectures or different methods have a great impact on performance. Sometimes you find that it’s hard to scale linearly, even though you’ve added a lot of capacity.
Similarly, how can we continue to maintain the distributed nature of the database when the data has been split into multiple pieces? In a stand-alone architecture, data is stored in a single copy, similar to MySQL’s support for atomicity — ensuring that all data in a single transaction either succeeds or fails. In distributed architectures, atomicity only ensures consistency within a single point. So, globally speaking, since the data is now across nodes, how do you ensure global consistency across nodes, how do you ensure that all data is either written or rolled back across multiple nodes? This involves distributed transactions.
So as you can see, the advantage of horizontal expansion is obvious, it solves the limitation of vertical expansion machines. But it is more complex and introduces more problems. With these issues in mind, I’ll show you how TDSQL performs horizontal scaling and how it addresses these issues.
Ii. TDSQL horizontal expansion practice
2.1 TDSQL architecture
First, let’s look at the architecture of TDSQL. TDSQL simply consists of several parts:
The first part is the SQL engine layer: primarily acting as an access end, masking the data storage details of the entire TDSQL back end. For the business, the business accesses the SQL engine layer.
Next comes the data storage layer consisting of multiple sets: in a distributed database, data is stored on various nodes, and each SET is treated as a data unit. It can be one active/standby or one active/multiple standby, which can be deployed based on business needs. Some service scenarios have high data security requirements. You can work in active/standby mode or work in active/standby mode. This is the data store.
There is also a Scheduler module, which is mainly responsible for the monitoring and control of the whole system cluster. During system expansion or active/standby switchover, the Scheduler module is like the brain control module of the whole system. In fact, the business only pays attention to the SQL engine layer without paying attention to Scheduler or how data is divided into nodes, which is insensitive to the business.
2.2 TDSQL Horizontal Expansion Process
At the beginning, data is stored in a Set, that is, in a node. So scaling is essentially scaling up the data to — there’s 256 sets in there, it’s scaling up to 256 machines. We can see that there are several key points in the expansion:
At first, the data is on one node, on one machine, but the data has been split, in this case into 256 pieces.
Horizontal scaling simply means migrating these shards to other sets, that is, to other node machines, so that more machines can be added to provide system performance.
To sum up, the data has been partitioned at the beginning, and the expansion process is like moving shards to new nodes. During the expansion process, the number of nodes is increased, from 1 to 2 to 3, and even to 256, but the number of shards remains unchanged. If you start with 256 shards on one node and expand to two nodes, you might have 128 shards on one node; At the end of the scale, you can scale up to 256 nodes, and the data is on 256 machines, each machine is responsible for one of the shards. Therefore, the whole expansion is simply relocation sharding. We’ll talk more about that later.
In private cloud or public cloud, TDSQL provides a unified front page for the entire expansion, which is very convenient for users in the process of using.
Let’s look at this example. Now the example has two sets, that is, two nodes, each of which is responsible for part of the route. The first node is responsible for 0-31, and the other node is named 3, which is responsible for the route information 32-63. Now is two nodes, if must carry on the expansion, at the front desk page we will have an added “Set” button, click “add Set”, will pop up a dialog box, inside the default will automatically choose before a configuration, the user can own custom, including the Set now, how many resources and the allocation of memory, disk, etc.
In addition, because route switching is required for capacity expansion, we can manually select a time to switch routes automatically or manually operate route switching based on the actual situation of services. These can be set according to the needs of the business.
Once you’ve created that first step, the brain module is responsible for allocating the various resources and the whole logic of initialization and data synchronization. And finally, you’ll see that the first node — it was two nodes, but now it’s three nodes. Before capacity expansion, the first node was responsible for 0-31, now it is only responsible for 0-15, the other part of the route by the new node. So the whole process, as you can see, can go from two nodes to three nodes very quickly with a single click on the web — and we can continue to add sets, continue to expand with one click depending on the business needs.
Iii. Design principle behind TDSQL horizontal expansion
3.1 Design Principle: How to consider compatibility and performance in partitioning key selection
In fact, we can also do not specify shardkey when users create tables, and we randomly select a key at the bottom to split the data, but this will affect the subsequent use efficiency, for example, the performance of distributed database cannot be particularly well played. We believe that the business layer can have a significant performance advantage if it has a small involvement in the design of the table structure, balancing compatibility and performance. In addition, if the business chooses shardkey — partition key, when the business designs the table structure, we can see multiple tables and select the relevant column as shardkey, which can ensure that the relevant data is placed on the same node when the data is split. This avoids cross-node data interaction in many distributed situations.
3.2 Design Principle: High availability and reliability in capacity expansion
-
Data synchronization
-
Data validation
-
Routing updates
-
Delete redundant data
3.3 Design principle: distributed transaction
-
Atomicity, decentralization, linear growth in performance
3.4 Design Principle: How to achieve linear performance growth in capacity expansion
Vertical capacity expansion is usually achieved by replacing a better CPU or similar methods to achieve linear performance increase. Horizontal expansion, because the data split to a number of nodes, how to make good use of the split down each node, parallel computing, really give play to the advantages of horizontal distributed database, need a lot of operations, a lot of optimization measures. TDSQL does some optimizations like this.
Let’s take a quick look at an aggregation — how TDSQL is able to achieve horizontal expansion with almost no awareness of the business and is used in the same way as standalone MySQL. For a service, assume that there are seven pieces of data in the table. The service only needs to insert seven pieces of data regardless of whether the table has one node or multiple nodes. The system will parse the syntax according to the PASSED SQL and rewrite the data automatically. 7 pieces of data, the system will calculate according to the partitioning key, and find that these 4 are sent to the first node, the other 3 are sent to the second node, and then rewrite, rewrite and insert these data. For the user, it’s like this, but across the nodes, we’re going to use a two-phase commit, which is multiple SQL, and we’re going to make sure that if there’s a problem both sides are rolled back at the same time.
For more complex SQL, such as multiple tables or more subqueries, if you are interested, you can pay attention to our share later – SQL engine architecture and engine query practice.
4. Practice cases of horizontal expansion
In chapter four, we briefly introduce some practices and cases.
4.1 Practice: How to select partitioning keys
If it is a user-facing Internet application, we can use the corresponding field of the user, such as the user ID, as the partition key. This ensures that when you have a large number of users, the data can be split into each backend node based on the user ID.
As you can see, if the horizontal expansion is more — from one node to 256 nodes, if a SQL is not written well, it may need to do the aggregation of all data of 256 nodes, then the performance will not be very good.
4.2 Practice: When to Expand capacity?
Let’s look at a few more examples of clustering on the cloud. Each SET is responsible for a part of the shardkey. The routing information is 0-127, which means that it can eventually be expanded to 128 nodes, so it can be expanded by 128 times. This “128” can be predetermined by the initial business estimate. Because if the pool gets too big, yes, you could end up with thousands, but the data would be scattered. In fact, every cloud or physical machine today is so good that you don’t need thousands of specs.
Today my share is mainly these content, if you have any questions, please feel free to leave a comment.
Five, the Q&A:
Preferential experience cloud database
Click to enjoy Tencent Cloud MySQL database preferential activities