High concurrency system-layered architecture

Article from: Alibaba's billion-level concurrent system design (2021 version)

Link: https://pan.baidu.com/s/1lbqQhDWjdZe1CBU-6U4jhA Extraction code: 8888 

table of Contents

What is a layered architecture?

What are the benefits of stratification?

How to do system layering

Disadvantages of layered architecture

Course summary


In the system from 0 to 1, in order to get the system online quickly, we usually do not consider layering. However, as the business becomes more and more complex and a large amount of code is entangled, problems such as unclear logic, interdependence of various modules, poor code scalability, and changes in one place will affect the whole body. At this time, layering the system will be put on the agenda, so how do we layer the architecture? What is the relationship between architecture layering and high-concurrency architecture design? In this lesson, I will take you to find the answer.

What is a layered architecture?

Software architecture layering is a common design method in software engineering. It divides the overall system into N levels, each level has independent responsibilities, and multiple levels provide complete functions in collaboration . When we first became programmers, we would be "educated" that the design of the system should be "MVC" (Model-View-Controller) architecture . It divides the overall system into Model (model), View (view) and Controller (controller) three levels, which is to isolate the user view and business processing, and connect through the controller, to achieve a good performance Decoupling from logic is a standard software layered architecture. Another common layering method is to divide the overall architecture into a presentation layer, a logic layer, and a data access layer :

 

  • The presentation layer, as the name implies, is the one that displays data results and accepts user instructions, and is the layer closest to the user;
  • Logic layer: there are specific implementations of complex services;
  • Data access layer: It is the interaction between the main processing and storage.

This is the simplest layered approach in architecture. In fact, we have inadvertently designed the system according to the three-tier architecture. For example, when building a project, we usually create three directories: Web, Service, and Dao , which correspond to the presentation layer, the logic layer, and the There is a data access layer.

Chapter Interlude: This article only summarizes part of the "High Concurrency System Design", and there are more resources being compiled including JVM, Tomcat, MySQL, Spring, Mybatis, Redis, MongoDB, Mycat, Zookeeper Nginx, RabbitMq, RocketMq , Kafka, Dubbo, Docker distributed (architecture design, transaction scenario strategy, scenario solution, task scheduling scenario implementation) high-concurrency scenario response solutions, performance tuning actual combat, massive data scenario implementation, message service bus MQ, etc. You can add assistant QQ: [2986706524] to get it from irregular updates. Remarks: [Java] After passing the review, you can get it by private message: [Information].

In addition, if we pay a little attention, we can find many examples of layering. For example, the OSI network model we learned in the university, it divides the entire network into seven layers, from bottom to top are the physical layer, data link layer, network layer, transport layer, session layer, presentation layer, and application layer. The TCP/IP protocol is often used in work, which simplifies the network into four layers, namely the link layer, network layer, transport layer and application layer. Each layer performs its duties and helps each other. The network layer is responsible for end-to-end addressing and connection establishment, and the transport layer is responsible for end-to-end data transmission. At the same time, there will be data interaction between two adjacent layers. This can isolate concerns and allow different layers to focus on different things.

 

 

 

The Linux file system is also hierarchically designed. You can clearly see the hierarchy of the file system from the figure below. At the top of the file system is the virtual file system (VFS), which is used to shield the differences between different file systems and provide a unified system call interface. The lower layer of the virtual file system is various file systems such as Ext3 and Ext4, and the next step is to shield the implementation details of different hardware devices. We abstracted a single layer-the general block device layer, and then different types of disks. . We can see that some layers are responsible for the abstraction of different implementations of the lower layer, thereby shielding the implementation details from the upper layer. For example, VFS provides a unified call interface for the upper layer (system call layer), and at the same time, it specifies the implementation model for different file systems in the lower layer. When a new file system is implemented, it only needs to follow this model. Design, it can be seamlessly inserted into the Linux file system.

So, why do so many systems have to be layered design? The answer is that layered design has certain advantages.

What are the benefits of stratification?

The hierarchical design can simplify the system design and allow different people to focus on a certain level of things . Imagine how painful it is if you want to design a network program without layering. Because you must be an all-rounder who knows the network, you need to know what the interfaces of various network devices are so that you can send data packets to it. You also need to pay attention to the details of data transmission, and you need to deal with complex issues like network congestion and data retransmission over time. Of course, you need to pay more attention to how data is safely transmitted on the network, so that it will not be spied and tampered with by others. With a layered design, you only need to focus on designing the application layer program, and everything else can be completed by the following layers.

Furthermore, high reuse can be achieved after layering . For example, when we are designing system A, we find that a certain layer has a certain versatility, then we can extract it independently and use it when designing system B, which can reduce the development cycle and improve the efficiency of research and development.

Finally, the layered architecture can make it easier for us to scale horizontally . If the system is not hierarchical, we need to expand the overall system when traffic increases. However, if we layer the system according to the three-tier architecture mentioned above, then we can make detailed extensions for specific problems. For example, the business logic contains more complex calculations, which causes the CPU to become a performance bottleneck. In this way, the logic layer can be extracted and deployed independently, and then only the logic layer can be expanded, which is compared with the overall system expansion The price paid is much smaller.

This can also explain the question we asked at the beginning of the course: What is the relationship between architectural layering and high-concurrency design? In "01 | High-concurrency system: what is its general design method?" we learned that horizontal Expansion is one of the common methods for high-concurrency system design. Since the layered architecture can provide convenience for horizontal expansion, the system that supports high-concurrency must be a layered system .

 

How to do system layering

Having said so many advantages of stratification, what are the key factors that need to be considered when we are designing stratification? In my opinion, the most important point is that you need to clarify what the boundaries of each level are. You may ask: "If you are layered according to a three-tier structure, isn't it easy to define the boundaries of each layer?" Yes, when the business logic is simple, the boundaries between the layers are indeed clear, and new functions are developed. It also knows which code to write where. But when the business logic becomes more and more complex, the boundary will become more and more blurred. Let me give you an example. There is a user system in any system. The most basic interface is the interface that returns user information. It calls the GetUser method of the logic layer, and the GetUser method interacts with User DB to obtain data, as shown on the left side of the figure below.

At this time, the product puts forward a requirement. When displaying user information in the APP, if the user does not exist, then a user must be automatically created for the user. At the same time, to make an HTML5 page, the HTML5 page should retain the previous logic, that is, there is no need to create users. At this time, the boundary of the logic layer becomes unclear, and the presentation layer also assumes part of the business logic (orchestrating the acquisition of users and the creation of user interfaces).

 

 

So how do we do it? Referring to the "Alibaba Java Development Manual v1.4.0 (Detailed Edition)" released by Ali , we can refine the original three-tier architecture into the following:

Let me explain the role of each layer in this layered architecture

 

 

  • Terminal display layer: The layer where templates are rendered and displayed at each end. Currently, it is mainly Velocity rendering, JS rendering, JSP rendering, mobile display, etc.
  • Open interface layer: Encapsulate the Service layer methods into open interfaces, and perform gateway security control and flow control at the same time.
  • Web layer: It is mainly for forwarding access control, checking various basic parameters, or simply processing services that are not reused.
  • Service layer: business logic layer.
  • Manager layer: general business processing layer. This layer has two main functions. First, you can sink some of the general capabilities of the original Service layer to this layer, such as the interaction strategy with cache and storage, and the access of middleware; second, you can also This layer encapsulates calls to third-party interfaces, such as calling payment services and calling audit services.
  • DAO layer: Data access layer, which interacts with the underlying MySQL, Oracle, Hbase, etc.
  • External interface or third-party platform: including RPC open interfaces of other departments, basic platforms, and HTTP interfaces of other companies.

In this layered architecture, the Manager layer is mainly added. Its relationship with the Service layer is: the Manager layer provides atomic service interfaces, and the Service layer is responsible for orchestrating atomic interfaces according to business logic.

In the above example, the Manager layer provides interfaces for creating users and obtaining user information, and the Service layer is responsible for assembling these two interfaces. In this way, the business logic originally scattered in the presentation layer is unified to the Service layer, and the boundaries of each layer are very clear. In addition, another factor that needs to be considered in the layered architecture is that the layers must depend on each other between adjacent layers, and the flow of data can only flow between two adjacent layers.

Let's take the three-tier architecture as an example. After the data enters from the presentation layer, it must flow to the logic layer for business logic processing, and then flow to the data access layer to interact with the database. Then you may ask: "If the business logic is very simple, can you go directly from the presentation layer to the data access layer, or even directly read the database?" In fact, it is functionally possible, but considering long-term architecture design, this will cause The confusion of hierarchical calls, for example, if the presentation layer or the business layer can directly manipulate the database, then once the database address changes, you need to make changes at multiple levels, which loses the meaning of layering, and for subsequent maintenance or Refactoring will be catastrophic.

Disadvantages of layered architecture

Nothing can be perfect. Although the layered architecture has advantages and disadvantages, one of its main disadvantages is that it increases the complexity of the code . This is obvious. After receiving the request, you can directly query the database to obtain the results, but you have to insert multiple levels in the middle, and it is possible that each level is simply data transfer. Sometimes adding a small requirement also requires changing the code at all levels, which seems to increase the cost of development, and also increase the complexity from the debugging point of view. Originally, if I directly access the database, I only need to debug one method, but now I To debug multiple methods at multiple levels.

Another possible flaw is that if we deploy each layer independently and interact with each other through the network, then the multi-layer architecture will suffer performance losses . This is also the reason why the performance of the service-oriented architecture is slightly worse than that of the monolithic architecture , which is the so-called "one more hop" problem .

Then do we choose a layered architecture? The answer is of course yes. You have to know that any solution architecture has advantages and disadvantages. The world is not complete. What about our architecture? The layered architecture will increase the system complexity and may also have performance loss, but compared to it For our benefit, these are acceptable or can be solved by other solutions. When we are making decisions, we must not be partial and give up food because of choking.

Course summary

Today I took you to understand the advantages and disadvantages of layered architecture, and how we layer the architecture in actual work. What I want you to understand is that layered architecture is an external manifestation of software design ideas and a way of implementation . Some of the software design principles we are familiar with are reflected in the layered architecture.

For example, the Single Responsibility Principle stipulates that each class has only a single function. It can be extended here to mean that each layer has a single responsibility, and the boundary between layers is clear; the original intention of Dimit's law is that an object should have everything to other objects. It may be less understood that the manifestation of the layered architecture is that data interaction cannot be cross-layered, but can only be carried out between adjacent layers ; and the opening and closing principle requires software to be open for extension and closed for modification. Its meaning is actually to separate the abstraction layer from the implementation layer. The abstraction layer is a summary of the common features of the implementation layer and cannot be modified, but the specific implementation can be expanded indefinitely and replaced at will.

Mastering these design ideas will naturally understand the beauty of layered architecture design, and it can also help us make better design solutions.

Guess you like

Origin blog.csdn.net/sanmi8276/article/details/113084155