[Articles] Overview of the evolution of distributed architecture

Foreword

I've put in front of MySQL Series have been updated over, then I believe we should have looked great harvest (should not exaggerate it ha ha ha ha), after all, through the MySQL spoke again, out of the interview should be able to say than the average interview official much more aware of the theoretical knowledge on the job performance tuning also basically have (also suggest that you practice, practice makes perfect). Ado, today's topic is non-chapter series, mainly for everyone to sort out things about architecture, I hope we can have after reading a certain understanding of server architecture, and thank you for your continued support.

text

The essence of architecture

A software system as more and more functions, calls increased dramatically, the whole system gradually fragmented, more and more chaotic, and ultimately unable to maintain and extend, so the system after a brutal period of growth, but also the need for timely intervention to avoid more and more disordered. The essence of architecture is to perform a reconstruction of ordering, the system has evolved.

  • That architecture is how to achieve disorder to order it?

The basic tool is divided harmony, break up the system first, and then reassembled.

Sub-process is to split the system into subsystems / modules / components, split time, we must first solve the problem of positioning of each component before you can divide each other's borders, to achieve a reasonable split.

The final bonding is required, each of the separated organic components integrated with, relatively speaking, more difficult to split the first step.

The results split enables developers to do business focus, skills, focus, achieve agile development, the result is bound to become flexible system can be changed because of the need to achieve business agility.

Category Architecture

Architecture can be divided into general business architecture, application architecture, technical architecture:

  1. Business architecture from the conceptual level to help developers better understand the system, such as business processes, business modules, input and output, business domain;
  2. Application Architecture from logic level to help develop landing systems, such as data interactions, application forms, interactive way, the entire system is logically easier to understand, into the well-known SOA will fall within the scope of application architecture;
  3. Mainly to solve the technical architecture platform selection techniques, such as operating systems, middleware, devices, multi-room, horizontal scaling, high availability and other issues

It should be noted that the system or architecture are first serve, high degree of order of the system, with a reasonable logic, business concept clear first. Now we discuss more of a technical infrastructure, such as high concurrent design, distributed transaction processing, simply because the business does not need to Context, better communicate with each other.

Specific architecture design, the first to focus on business architecture and application architecture that newcomers should pay special attention. When the interview is the pain points!

Architecture Evolution large sites

ps: The following part of the picture reference from "large sites system and Java middleware combat .pdf"

Starting a Web site electricity supplier

For a better understanding of our electricity supplier website to example,

As a trading type of site, the user will have the (user registration, user management), product (commodity display, merchandise management), transaction (single payment) functions

If we only need to support these basic functions, then we should be the beginning of architecture might look like this:
Here Insert Picture Description

This place should be noted that among the various functional modules that interact by calling method inside the JVM, and between applications and databases are accessed via JDBC.

Single load alarms, database and application separation

With the opening of the site, traffic is increasing, so this time the server load is bound to continue to rise, it is necessary to require a number of ways to cope. Here is not considered replacement levels and a variety of software to optimize the machine, starting with the architecture of the structure to do some adjustments. We can put the database and application assigned to two machines from one machine:Here Insert Picture Description

Variety:

Web site had changed from a two, this change affect us very little. Stand-alone case, we apply the approach to the use of JDBC and database connection, database and application are now separated, we just need to change the ip address database address database server from the machine on the line in the configuration file. For the development, testing, deployment, are not affected.

After adjustment we were able to alleviate the current system pressure, but as time goes on, traffic continues to increase, we still need to do system reform.

Why do you divide it? From the perspective of the computer itself to consider the case, a request for access to treatment and ultimately to return, only performance bottleneck is: CPU, file IO, network IO, memory, and other factors. And a computer in these latitudes there is a performance bottleneck, if an excessive consumption of resources, often resulting in slow response speed of the system, a machine is increased, so that the database IO and CPU resources occupied by one machine to increase performance.

This place the insertion point the way, the reason is briefly about each resource consumption.

CPU / IO / memory:

  1. Context switching is mainly because each CPU core can perform at the same time a thread, CPU scheduling a number of ways, such as preemptive and polling, so as to preemptive an example, each thread is assigned a certain execution time, when it reaches the execution time, the thread has blocked or IO high priority thread to be executed. CPU switches other threads to execute. In the process of switching, it is necessary to store the state of the current thread of execution and restore the thread state to be executed, the process is the context switch. Under such IO, lock-awaited scene will trigger a context switch, when a context switch too much can cause more cores occupy CPU;
  2. File IO, such as frequent log write, the disk itself is slow processing speed, can cause IO performance problems;
  3. Network IO, bandwidth is not enough;
  4. Memory, including memory overflow, memory leaks, out of memory;

In fact, whether tuning the application layer or just the hardware upgrades or. In fact, nothing more than adjust these factors.

Application server complex alarms, how to make an application server cluster

If you say that this time the pressure increases the application server, based on the detection result of the application, it can be targeted pressure on the performance of local optimization. We consider here be optimized by horizontal expansion, the single into a cluster:Here Insert Picture Description

The application server from one becomes two, there is no direct interaction between the two application servers, they rely on the database to provide services, then this time will be dished out two problems:

  1. End-user application server to access the corresponding two options
    for this problem can be resolved using DNS, it can be solved by load balancing device
  2. session of the problem?

Horizontal and vertical expansion

For large distributed architecture, we have been in pursuit of a simple, elegant way to deal with traffic and data volume growth. And in this way usually refers to software programs do not need to change, simply by hardware upgrades or adding machine can be resolved.

And this is a telescopic design in a distributed architecture.

Telescopic vertical scaling and horizontal scaling is divided into two kinds:

  1. Vertical scaling: represented by upgrading or adding a single machine hardware to support access of data amount and the amount of growth, vertical scaling benefit is relatively low technical difficulty, operating costs are relatively low and alterations. But the disadvantage is the performance bottleneck of the machine, while the minicomputer or mainframe upgrade performance, the cost is very large. This is also one of the reasons to go to Ali the IOE;
  • Increase the number of CPU cores: CPU increase service capacity of the system can be a big increase, the number of threads such as response speed, but can handle. But will also have some significant problems after the introduction of CPU:

- Lock competition; running multiple threads simultaneously access a shared data, then lock it comes to competition, while fierce competition will lead to lock many threads are waiting for a lock, so even increase the CPU can not let the thread gets faster processing. Of course, there are tuning means of lock contention can be reduced by tuning means;
- the number of threads to support concurrent requests is fixed, even if the increase in service capacity CPU, the system will not be promoted;
- for single-threaded task , multi-core CPU is not much effect;

  • Increase the memory: the memory can increase the speed of response directly commission the system, of course, also possible to reach the effect, if JVM heap memory is fixed.
  1. Horizontal scaling: by increasing the amount of machines to support access and data volume growth, become horizontal scaling, horizontal scaling theory, there is no bottleneck, but the drawback is the relatively high technical requirements, while operation and maintenance to bring greater challenges;

Vertical scaling and horizontal scaling has its own little, we will do combine both of the actual use of the process, on the one hand to consider the cost of the hardware upgrade, on the one hand to consider the cost of software transformation.

The introduction of load balancing device

Service routing, load balancing equipment to achieve
Here Insert Picture Description
after the introduction of load balancers, will bring problems session related.

Load Balancing Algorithm
  • Polling (Round Robin) method
    requests sequentially in turn assigned to the backend server, each server balanced treatment, without regard to the actual number of connections the server and the current system load
    disadvantage: When a different server hardware in cluster configuration, performance when big difference, can not be treated differently

  • Randomly
    by a random function system, according to the size of the value back to the server list in which a randomly selected access. With the increasing amount of invocation, the actual effect of increasing flow rate close to the average assigned to each server in the background, i.e. the effect of the polling method
    advantages: easy to use, and no additional configuration algorithm.
    Cons: Features of random numbers is when a large amount of data to a certain amount in order to ensure balanced, so if you request a limited amount, it may not meet the requirements of load balancing.

  • Hashing the source address of the
    server according to the service consumer requests the IP address of the client, a hash value calculated by the hash function, the hash value and the size of the server list modulo operation is performed, the result is to be accessed No. addresses. Using the source address hashing load balancing, the same IP client, server list if unchanged, will be mapped to the same back-end server to access it.

  • Weighted round robin (Weight Round Robin) method
    load may be different backend server machine configuration and the current system is not the same, they are not the same compression capability. Allocation with high-profile, low weight higher load machine, so that it can handle more requests, arranged low, high load machine, to lower their weight distribution, reducing the system load, it WRR good addressed this issue and the request is assigned to a rear end and a weight in the order according to the weight

  • The minimum number of connections Act
    in several ways is through the front of the largest reasonable allocation requests may improve server utilization, but in fact, the number of requests balanced and do not represent a balanced load. Therefore, the introduction of the minimum number of connection method. It is based on the current connection back-end server, wherein the dynamic selection of the current backlog minimum number of connections to a server processing the current request, the background as much as possible to improve server utilization, the load distributed to each logical server.

session problem

We open a web page is basically a web server and browser need multiple interactions, we all know Http protocol itself is stateless, which is the http protocol was originally designed, the client only needs a simple request to the server to download some files either the client or the server did not need to record each other's past behavior is independent between each request, such as the relationship between a customer and a vending machine.

In fact, many of the scenes we all need characteristics with the state, so we have introduced a clever mechanism session + cookie to remember the session for each request.

At the start of the session to the current session is assigned a unique session identifier (sessionid), then put the cookie identifier tells the browser after every request, the browser will bring this session ID to tell the web server requests belong which session. On the web server, each session has a separate storage, save the information in different sessions. If you have disabled the cookie, the general practice is to put the session ID parameter of the URL.
Here Insert Picture Description
And we become two application servers from a single, session will encounter problems.

sharing session in a distributed environment

Session Sharing in the current context of the Internet, is not a new topic, but how to solve the session also shared in fact there are many very mature program.

Server implementation of session replication or session sharing, this type of sharing and server session is closely related.
Here Insert Picture Description

We've added a synchronization session data between a Web server, by synchronizing ensures consistent Session data between different Web servers. General application container supports Session Replication way, but there are problems:

  1. Session data synchronization overhead caused by network bandwidth. As long as there is a change Session data, we need to synchronize data to all other machines, the more machines, synchronous bring greater network bandwidth overhead.

  2. Each Web server must save all the Session data Session data if an entire cluster of many (many people visit the Web site at the same time), then save the content for each machine occupy Session data can be severe.

This program is by application container to complete the Session Session replication to solve problems, the application itself does not care about this. This program is not suitable for multi-machine cluster number of scenarios.

The use of sophisticated technology to do session replication, such as 12306 gemfire used, such as the common memory databases such as Redis.
Here Insert Picture Description
Session data is not saved to the machine and stored in a centralized storage place, the local modification is occurring in the Session centrally stored. Web server uses Session read from centralized storage place. This ensures that the different Web server to read the Session data is the same. DETAILED DESCRIPTION OF THE stored Session may be a problem with the database:

  1. Session to read and write data into the network operation, which read data with respect to the machine, the problem is that there is no delay and stability, but our basic communication network including all happen, no problem.

  2. If stored centrally Session of the machine or cluster there is a problem, it will affect our application.

With respect to the Session Replication, when the number of Web servers is relatively large, more Session number of times, the advantages of this centralized storage solution is very obvious.

  • The session maintenance client

It is easy to think of is the use of cookie, but there is a risk clients, data insecurity, but smaller than the data can be stored, so the session will also maintain information in an encrypted session to the client.
Here Insert Picture Description
Our data into Session Cookie, and then generates the corresponding data from the Cookie Session on the Web server. It's like every time we own dishes with me, so go to that restaurant you can freely choose. Relatively centralized storage foregoing embodiment, does not depend on an external storage system, or acquired from an external system does not exist, the write data and the Session network delay problems of instability.

There are still problems:

safety. Session data could have been server-side data, and this program is to make these services end data external to the network and the client, so the question on the security presence. We can do encrypt data Cookie Session of writing, but for safety, it can not touch is physically secure.

Database pressure becomes larger, separate read and write it

As the business continues to grow, and the amount of data traffic continues to increase. For larger sites, a lot of reading and writing business is less, this situation will be fed back directly to the database. So for this case, we can consider using separate read and write way to optimize pressure on the database.
Here Insert Picture Description

This structural change will bring two problems:

  1. How Data Synchronization

We want to share the reading of the pressure on the main library by reading library, you first need to solve the problem is how to read copy library. Database systems typically provide data replication capabilities, we can directly use the database system's own mechanisms. Different systems have different database support, such as Mysql support structure Master + slave provides data replication mechanism.

  1. Application for how to route the data source

For applications, add a library to read structural changes had some impact, that is, our application need to choose a different database sources depending on the circumstances.

In fact, the search engine is a reading library

In fact, search engines can be understood as a reading library, our goods stored in the database, and the site requires users to provide real-time search function, especially in this product search. For such a read request, if all commuting library, in fact, there will still be a performance bottleneck. And using a search engine, not only can greatly improve the retrieval speed. But also to reduce the pressure reading databases and search engines most important job is the need to build an index based on the data to be searched, and with the change of the data to be searched, the index also requires a corresponding change.
Here Insert Picture Description
Use search using cluster library and reading is the same, but the basic process of building an index of all we need to do it yourself. It can be planned manner from the two latitude indexing search engine, in accordance with a full amount / increment division. One is in accordance with real-time / non-real divide.

The full amount for the first time indexing manner, may be new, it could be rebuilt. The incremental approach is to continuously update the index on the basis of the full amount of the.

Real-time and non-real-time reflected in the index update time is the best real-time, non-real-time data protection taking into account the main sources.

In general, the search engine technology to solve problems reading some scenarios when searching the station, provide better query efficiency.

Tool for speeding up data read - and distributed memory cache

In large sites, basically solving the problem is in storage and computing, so storage is a very important support system. The initial site construction we are starting from a relational database, and many times, for convenience, we will put some business logic inside the database to do, such as triggers, stored procedures. Although in the early can easily solve the problem, but in the future course of development will bring a lot of trouble, such as after a large amount of data, do sub-library sub-table operation. Meanwhile, the future business development to a certain body mass, demand for storage can not be fully met by a relational database.

Distributed File System

For some pictures, large text, use databases is inappropriate, so we will use a distributed file system for file storage, distributed file systems have a lot of products, such as Taobao TFS, google the GFS. There are open-source HDFS.

NoSQL

NoSQL we can be understood as Not Only SQL, or No SQL. The two senses are to be expressed in large sites, the relational database can solve most problems, but for the characteristics of different content access features, characteristics and other requirements for the transaction storage is not the same. NoSQL category is then between the file system and SQL relational database localization.

Data caching

Internal large sites will use some of the data cache, main pressure reading for sharing databases, caching systems are generally used to save queries and key-value pairs. Application system will typically placed in hot data to the cache, the cache should be done by filling applications. If the data does not exist, to read data from the database into the cache. Over time, when the cache capacity is not enough to clear the data, the data is not accessed recently will be cleared away. Another way is after the data in the database changes, take the initiative to put the data into the cache system, this advantage is the ability to update cached data when data changes will not cause reading failure.

Here Insert Picture Description

Page Caching

In addition to the data cache, we can also make caching of pages, the data cache can speed up application data read speed in response to the request, but ultimately displayed to the user or page, or page views and some dynamically generated pages particularly high, we will do some page or content caching.

To make up for lack of a relational database, the introduction of distributed storage

Our most widely used mainly relational database, but in some scenarios, the relational database is not very appropriate, we will introduce a distributed storage system, such as redis, mongoDB, cassandra, HBase and so on.

Depending on the scene and the data structure type, select the appropriate distributed storage system can greatly improve performance. Provide a distributed system to support high-capacity, high concurrent access, data redundancy, disaster recovery through clustering.
Here Insert Picture Description

After separate read and write, and database bottleneck

By separate read and write as well as some alternative scenarios relational database with a distributed storage system capable of reducing the pressure of the main library, to solve the problem of data storage, but as your business grows, we will encounter the main library bottleneck. Deduction up to now, each module of our website: trading, commodities, or user data are stored in a database. Despite the increase in cache, read and write separate ways, but the pressure is still in the database continues to increase, so we can solve the problem of database stress data split vertically and horizontally split.

For special purposes, the vertical split data

A vertical split means splits the different business data in the database to a different database, an example of our deduction, to separate user data, transactions, merchandise.
Here Insert Picture Description

Data from different business split from the original database to multiple databases, then you need to consider how to handle the affairs of the original single cross-business:

  1. Use distributed transactions to solve

  2. Remove transaction or a transaction not pursue strong support

After the data is split vertically, it solves all the traffic data in a database and issues the pressure, and may be performed more optimized according to the characteristics of different services.

After the split vertically, you encounter a problem, a horizontal split data

There are vertical split data corresponding to the horizontal split, horizontal split data is to split the data into two of the same table in the database, generates data of the horizontal resolution because the amount of data table or a business update amount reaches a bottleneck in a single database, this time table can be split into two or more databases.

Split with separate read and write data level difference is read in separate read and write to solve pressure problems for large amount of data or update a case does not work.

Difference data of the horizontal split data is split vertically, the vertical resolution is split into different tables of different databases, and the horizontal resolution is to split the same table to a different database.

We may further split the two database tables to the user, they have exactly the same structure of the user table, and each library user table only covers a part of the user, the user database together two is equivalent to no user table before splitting.
Here Insert Picture Description

The impact caused by the split level
  1. sql routing problem, it is necessary to determine that the current request to the database according to a condition;

  2. Processing master key, not using the self-energizing id, the global ID need;

  3. Since the data is split into business with a different database, and therefore involve a number of queries need to get across two databases, if the data is too big and needs paging, on the more intractable;

After a database problem solving, application challenges faced

Speaking in front of separate read and write, distributed storage, data in the vertical and horizontal split are split solve data problems, then we have to look at changes in applications.

With the development of business applications feature more and more applications will be growing, we need to think about how to prevent application of continuous increases, which requires the application apart from one application into a two or even It is more.

The first way is to use the characteristics of the business split, in our example, the main business functions in three parts, users, commodity trading. We can put the original application were split into two applications to commodity-based transactions and for transactions and goods will have a place to use the user's design, we let the two systems involved in the completion of the user's own work, and a similar user registration , user login and other basic work, temporarily handed over to one of the two systems can be done.
Here Insert Picture Description
We can also according to user registration, user login, user information maintenance subdivided into three systems, but split after this there will be some similar code in different systems, such as user-related code, how to protect it the same part of the code and how to provide multiplexing problems also need to be addressed to other modules. Moreover, there is no split between the new system this out directly call each other.

Services of the road

We look at service-oriented approach, we put applications into three layers, in the top of the web system is used to perform different business functions, in the middle of some service centers, different service centers provide different business services ; in the lowest layer is the service database.
Here Insert Picture Description
There are several important changes compared with the previous first way to access more than just stand-alone internal calls between business functions, also introduced a service calls remotely. Secondly, shared code is no longer scattered in different applications, these implementations are placed in each service center. Finally, the connection to the database has also undergone some changes, we interworking database into a service center, web application front-end pay more attention to the interworking browser, without having too much focus on the business logic of things. Tasks link to the database to the Business Service Center of the response, which can reduce the number of connections to the database.

The service center can not just put some common code set management, but also makes the code better maintenance.

Service-oriented approach will bring many benefits, first of all, from a structural point of view, the system architecture more clear, more three-dimensional than the original architecture. From the stability point of view, scattered across multiple applications code into a unified service and maintained by a dedicated team, on the one hand we can improve the quality of the code, on the other hand due to the relative stability of the underlying core module, modify and distribute the frequency with respect to the business systems will be a lot less, which will also improve the stability of the whole structure. Finally, a more unified underlying resources managed by the service layer, the structure is more clear, for team development efficiency for a relatively big way to improve service-oriented, will be a great influence on the development, the previous development model is a few large team is responsible for several large applications, with the service of the ground, our application will be rapid growth in the number of dependencies within the system also become complex, while the team has also been split, each focused on a specific small team the service or application, iterative efficiency will be higher.

What is a distributed architecture

The definition of a distributed architecture

Many students can not understand a distributed system, in simple terms, it refers to the distributed system components located on a computer network to communicate only by message passing and the target coordinate system. And there are two important factors:

  1. Component is distributed over a computer network;

  2. Pass only communicate through between the assembly and coordinate action message;

Distributed systems can in fact be considered to be a center of ideas to achieve, for the user is not aware of.

Meaning distributed architecture

From single to single-user stand-alone multi-user, to the current Internet age, applications undergone many changes, why use a distributed system?

  1. Upgrading a stand-alone processing capacity of more low cost;
  2. Single handling capacity bottlenecks;
  3. For stability and availability requirements;

We know that the processing power of a single machine capacity, including CPU, memory, disk and network.

We heard of Moore's Law now, when prices change every 18 months, the number of transistors on an integrated circuit that can be accommodated will be doubled, the performance will also be doubled. Means that over time, the ability to calculate the unit cost of expenses that can be purchased in the promotion, but we demand higher performance processor, the higher the costs required to pay. Therefore, a single machine through continuous improvement of performance caused by the output value is not cost-effective at the same time, the processor itself, there are performance bottlenecks.

There is also a very important factor, stability and usability, in a stand-alone environment is not provided. It is bound to need a distributed system to resolve.

By the way

something wrong? Give me a shout or whisper
harvest? That easily point a praise chant ~

Of course, I can also go to the public under the number "6 Xixuan"

Reply to "learning", you can receive a
[Java architect engineer advanced video tutorials] ~

Reply to "interview" can be obtained:
[I worked hard to organize the Java interview questions]

Reply to "MySQL brain map", you can get
[MySQL knowledge combing HD brain map]

Xi Xuan I am a trained programmer, php, Android and hardware have done, but in the end chose to focus on doing Java, so what's the problem may be the number of public discussion questions (emotional talk technology can ha ha ha), see the words will reply as soon as possible, hoping to learn together with you progress, on the server architecture, Java core analytical knowledge, career, interview summary and other articles occasionally adhere to push output, welcome attention ~ ~ ~

Here Insert Picture Description

Published 41 original articles · won praise 20 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42669785/article/details/104945955