Hello, I’m Alan the Programmer, nice to meet you.
In the article “System Architecture Design – Microservice Combat in High Concurrency Scenarios (3)”, I asked a question “Why should the system architecture design be layered?” In this article, I will explain my opinions in detail and write a comparison Shallow, I laughed.
What is layered architecture
Software architecture layering is a common design method in software engineering. It divides the overall system into N layers, each layer has independent responsibilities, and multiple layers cooperate to provide complete functions. Let me share with you some of my favorite layered architecture diagrams.
What are the benefits of layering
If the company wants to develop a small program but you are the only programmer, you have to write the front and back ends. Even if the program is very simple, if you are not familiar with front-end or back-end development, is it painful? Because you must be a programmer who is proficient in full-stack development, you must know the knowledge of front-end and back-end and the handling of various abnormal situations. And if the front-end and back-end are separated, and there is a front-end or back-end to cooperate with you, you only need to focus on the areas you know, and leave the rest to other colleagues, isn’t it very happy?
Let’s take an example of something that happened to me. When I wrote the last article “System Architecture Design-Microservice Combat in High Concurrency Scenarios (3)”, I really wanted to integrate the high concurrency technology stack with microservices. I have explained all the problems I know in the development of the service technology stack, but my ability is not enough for me to express clearly the contradictions and entanglements in a short tweet, so I feel very painful. The extent of the pain, you only need to read the long string of sentences without punctuation above me to feel it deeply.
Why is it so painful, I probably figured it out when I wrote this article, there are two main reasons. The first point is that my own ability is not enough, and I don’t have enough grasp of various components. The second point is that I did not decouple the tasks in layers, and thought about everything together, which increased the complexity of the tasks. To sum up, I am too greedy for everything.
I am not an all-rounder, not now, nor will I be in the future. Many complex problems will be encountered. At this time, the problem needs to be decoupled layer by layer and divided into small problems to solve. I will encounter many problems that I personally cannot solve but have to solve. At this time, I need to seek help from my friends. I don’t need to be very clear about how my friends solve the problems, but I need to know which friends to ask for help and be grateful.
How to do system layering
There are many advantages of layered architecture, so how do we do layered design, what key factors need to be considered?
Personally, I think the most important point is to figure out what the boundaries of each level are. Even for hierarchical Web projects, when the business logic is complex, there will be problems with increasingly blurred boundaries. Let me give a simple example.
There should be a user service in the system that everyone has developed. The most basic interface is the query user information interface. Its request link is DTO -> Controller -> Service. The DTO layer receives the request parameters from the front end and serializes them. Then pass it to the Controller layer to call the Service layer interface.
At this time, if the PO proposes a request, when the queried user does not exist, a user should be automatically created and returned. At this time, the boundary of the logic layer begins to become blurred, because the presentation layer also undertakes part of the business logic (querying users and creating users is arranged). What can we do at this time? 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:
In this layered architecture, a Manager layer is added, and its relationship with the Service layer is: the Manager layer provides atomic service interfaces, and the Service layer is responsible for arranging atomic interfaces based on business logic.
Taking the above example as an example, the Manager layer provides interfaces for creating users and obtaining user information, while the Service layer is responsible for assembling these two interfaces. In this way, the business logic originally distributed in the presentation layer is unified into 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 adjacent layers must be interdependent between layers, and data flow can only flow between two adjacent layers.
Insufficiency of layered architecture
Although the layered architecture has many advantages, it also has many defects. One of the most important defects is that it increases the complexity of the code. This is obvious, obviously we can directly query and operate the database after receiving the request, but we have inserted multiple levels in the middle, and each level may simply do data transfer, sometimes adding a small requirement is also It may be necessary to modify the code on the entire link. If it increases the burden on colleagues at this time, it will definitely attract a lot of complaints.
In addition, if we deploy each layer independently, then the interaction between layers will be lost in performance.
leave some questions
Do you know what is the Single Responsibility Principle?
Do you know what Dimiter’s Law is?
Do you know the open-closed principle?
Standing on the shoulders of giants
Tang Yang – High Concurrency System Design 40
Ask the Code and Wenqiang – SpringCloud Microservices in Practice
Learn architecture from 0