Today, we mainly introduce the content of the database of the second level of the smooth expansion architecture scheme.
First, Origin
(1) Large-scale, high-traffic Internet architecture, generally, the database has a service layer, the service layer records the "Business Library name" and "database instance" mapping, through the database connection pool to the database routing SQL statements to execute:
For example, the service layer configures user library the database instance corresponding to the physical location is IP (actually an intranet domain name).
(2) with the increase of data volume, the data should be split horizontally, the data will be distributed to different database instances (even physical machines), in order to reduce the amount of data, enhance the performance of expansion purposes:
such as: User library users distributed on two instances, Ip0 and ip1, service layer through the user identity uid modulo way to seek seobinggo by, modulo 2 more than 0 access ip0 the user library, modulo 2 more than 1 of the user library on the access ip1.
For more details on horizontal segmentation and vertical segmentation, see "One Minute master database vertical split".
(3) The Internet architecture needs to ensure that the database is highly available, a common way, the use of dual-master synchronous +keepalived+ virtual IP to ensure the availability of the database:
For example: two synchronized master libraries use the same virtual IP.
such as: When the main library hangs, the virtual IP automatically drift to another main library, the whole process of transparent to the caller, in this way to ensure the high availability of the database.
For more details on high availability, see "What exactly is the Internet architecture" high Availability ".
(4) Combining the above (2) and (3), the actual online architecture, both horizontal and high availability, so the actual database architecture is this:
Question: If the amount of data continues to increase, 2 library performance can not carry, how to do?
Answer: Continue to split horizontally, split into more libraries, reduce the amount of data in a single library, increase the number of library master instances (machines), and improve performance.
The final question throws: After dividing into X library, as the amount of data increases, to increase to the Y library, the process of database expansion, whether smooth, continuous external service, ensure the availability of services, is the issue to be discussed in this paper.
Second, stop the service plan
Before discussing the smoothing scenario, let's briefly explain the scenario of the "x library split Y-Library" outage Service:
(1) Site hanging an announcement "in order to provide better services for the general user, this site/game will be in the 00:00-2:00 between tonight, will not be able to log in, the user is well known"
(2) Stop service
(3) Create a new Y-Library to do high-availability
(4) Data migration, redistribution, write a Data Migration program, import from x library into Y library, the routing rules are upgraded from%x to%y
(5) Modify the service configuration, the original X line configuration upgrade to Y line
(6) Restart the service, connect the new library to provide services
The whole process, the most time-consuming is the fourth step of data migration.
Rollback scenario:
If the data migration fails, or if the test fails after the migration, change the configuration back to the X library, restore the service, and hang up the announcement another day.
Solution Advantages: Simple
Scenario Disadvantages:
(1) Stop service, not high availability
(2) Technical students under pressure, all work to be done within the specified time, according to experience, the more prone to stress error (this is fatal)
(3) If there is a problem the first time did not check out, started the service, run for a period of time and then found that there is a problem, difficult to rollback, need to go back, may lose a part of the data
Is there a smoother plan?
Third, second level, smooth, handsome plan
Again look at the pre-expansion of the architecture, divided into two libraries, assuming that each library 100 million data volume, how to smooth the expansion, increase the number of instances, reduce the amount of single-database data? Three simple steps to get it done.
(1) Modify the configuration
Two main changes:
A) The database instance is located on the machine to do dual virtual IP, the original%2=0 library is virtual ip0, now add a virtual ip00,%2=1 another library similarly
b) Modify the configuration of the service (whether in the configuration file, or in the configuration Center), the database configuration of 2 libraries, the database configuration of 4 libraries, the modification should pay attention to the old library and hard mapping relationship:
%2=0 of the library, will become%4=0 and%4=2;
The part of%2=1 will become%4=1 and%4=3;
This modification is to ensure that the correct data is still routed after the split.
(2) Reload configuration, instance expansion
Service layer Reload configuration, reload may be in several ways:
A) Compare the original, restart the service, read the new configuration file
b) Advanced, configure the center to signal the service, reread the configuration file, re-initialize the database connection pool
Either way, after reload, the database instance expansion is complete, the original is 2 DB instance provides the service, now becomes 4 DB instance provides the service, this process can generally complete in the second level.
The process can be progressively restarted, with no impact on the correctness and availability of the service:
A) even if the%2 search and%4 are present simultaneously, it does not affect the correctness of the data, as this is still a dual master data synchronization
b) Service reload prior to non-service, redundant services to ensure high availability
By completing the expansion of the instance, you will find that the amount of data in each database is still not declining, so the third step is to do some finishing touches.
(3) Finishing work, data shrinking
There are some finishing touches to the work:
A) Change the dual virtual IP to the false IP
b) Remove the old two-master synchronization, so that the data of the paired library is no longer in sync
c) Added new dual-master synchronization to ensure high availability
d) Remove redundant data, for example: Ip0%4=2 data are all killed, only for%4=0 data service
This way down, the amount of data per library is reduced to the original half, the data contraction is completed.
Iv. Summary
The handsome scheme can realize the second-level, smooth expansion of n-Library 2n library, increase the database service ability, reduce the data volume of half-library, its core principle is: Multiply enlarge, avoid data migration.
Migration steps:
(1) Modify the configuration
(2) Reload configuration, instance expansion complete
(3) Delete redundant data and other finishing work, data volume contraction completed
Database second-level smooth scaling architecture scheme