Layering is a common method of architecture design, and also an important idea to guide us to do architecture design and function design. The application of layering can help us solve many problems in our work. The following three parts will introduce layering: typical layering architecture, the idea of layering everywhere, and how to layering.
Typical hierarchical architecture
We focus on three classic hierarchical architectures.
Microservices layered architecture
The diagram above shows a typical microservice architecture hierarchy. The client represents the user’s App.
- As the real entrance of the system, the gateway layer receives the request from the client and preprocesses it to ensure that its forwarding to the business logic layer is meaningful. The gateway layer implements security policies, user authentication, traffic control, Session generation, and request ID generation.
- The function of the business logic layer is to implement specific business logic, such as order service logic, shared document merging algorithm, red envelope algorithm, etc.
- The data access layer provides data read and write capabilities, isolating business logic from the database. In this way, data can be processed in a unified manner. Operations such as adding cache to hotspot data, dividing databases and tables, and replacing databases do not need to be concerned by the service logic layer.
- The database layer is responsible for storing data.
Let’s consider a question: must microservices architecture be divided into four layers: gateway layer, business logic layer, data access layer and database layer? The answer is no. The four-tier architecture is only a relatively optimized scheme, and there will be many changes for different business scales and different business scenarios.
For example, in the early stage of the project, the data structure may be relatively single, so there is no need to separate the business logic layer from the data access layer prematurely, which will only increase the cost of service maintenance. A better approach is to encapsulate access to the database into a common JAR package that everyone calls at the business logic layer to access the database. As the business evolves, the transformation cost will be lower when the data access layer is separated into a separate service. For example, with the development of business, some modules that need to be called by other services appear in the business logic layer. We can divide the business logic layer into two layers, common business logic layer and personalized business logic layer, to increase the reusability of code logic.
The microservice hierarchy also has an invocation principle that only allows the upper layer to call the lower layer of services, not the same layer and reverse invocation, to avoid circular dependencies. Microservices solve the coupling of services, clarify the invocation relationship and accelerate business development through layers.
The network layer
The figure above is a typical network five-tier architecture hierarchical model diagram.
Networks are for data communication between computers.
- The communication between two computers requires the conversion of data and physical signals, which is the main role of the physical layer.
- Two communicators need to find each other in order to send the message correctly. How do you ensure that the message is sent to the correct machine? This requires MAC address and broadcast functions. The data link layer is to find the physical link that can communicate between machines through MAC and broadcast.
- There are millions of computers in the world, and each computer has its own MAC address. It would be too expensive to send a message that had to be broadcast around the world. The main work of the network layer is to divide networks by IP addresses to distinguish different network areas.
- Each computer has multiple port numbers to realize parallel communication, but also to ensure the reliability of communication, which is the main problem solved by the transport layer.
- Each computer has different applications to provide web services, FTP services, mail services and so on, and the application layer is mainly used to solve the problem of how to provide services to users.
Each of the five layers of the network is responsible for different functions and ultimately completes the transfer of data from one computer application to another. After stratification, hardware manufacturers are mainly responsible for the physical layer and data link layer, the operating system is mainly responsible for the network layer and transmission layer, and the application layer is responsible for the application developer.
By layering complex problems into layers, the division of labor becomes clearer.
Think: Can the network not be a five-tier architecture?
MVC layered
MVC is a very typical application development architecture. When I first graduated from Java development, the MVC pattern of Spring framework was deeply in my mind. M: Model Business Model. V: View The interactive page seen by the user. C: Controller A Controller that receives user requests and invokes View and Model to output data required by the user.
By dividing the business model, view, and controller layers, you can focus on business code in different folders when developing Web applications. Different controllers can combine different business models and views, which also increases code reuse.
Layered architecture everywhere
The previous introduction is some classical hierarchical architecture, in fact, hierarchical architecture and ideas, can be reflected in all aspects of our work.
Web page development
This is a hierarchical architecture diagram of a Web front-end project.
- The front-end routing layer is responsible for unifying the common logic that handles browser address and route changes and selecting different pages based on different routes.
- The page rendering layer is responsible for drawing the page and requesting the corresponding data to populate the page.
- The data logic layer is responsible for assembling the data and supporting the page presentation layer.
- The interface layer is responsible for calling the server interface and pulling data.
Layering makes it easy to organize code structure and reuse code logic. For example, if the same data is used on multiple pages, the same data logic layer function can be called on multiple pages.
Interface service development
This is a schematic diagram of the layered architecture of a Web interface service.
- The server route receives the request and forwards the request.
- The routing middleware is responsible for adding some general logic before and after forwarding, such as verifying user Token, counting request times, etc.
- The Controller is responsible for receiving specific requests and calling different services.
- A Service implements the specific business logic of a request.
- The Model layer provides reads and writes to the data for Service calls.
Specific instance
When we worked on the intelligent outbound call project in 360 Artificial Intelligence Research Institute, we also designed the overall structure of the service with the idea of layering.
First of all, we divide the business into two layers: online service and support service, and abstract the data storage from the storage layer. The online service provides the voice interaction function. The support service is responsible for configuring data and collecting logs.
Online services are divided into three layers: access layer, central control layer and algorithm layer. The access layer is similar to the gateway, responsible for user authentication, traffic forwarding, Session generation, etc. The central controller is responsible for scheduling algorithm services according to the policy, and the algorithm layer realizes the specific algorithm logic.
Through two layers, the relationship between each module of the whole business is clarified and the main functions of each module are divided, which not only reduces the functional coupling between modules but also facilitates the development, deployment and upgrade of services.
layered
Since layering is so important, how can we use it better? I have summarized some rules of use.
- Data logic and presentation logic layering.
- Public services sink and private services float and stratify.
- Public methods sink, and personalized methods float and layer.
- Unified network request closing layer.
- Layering external apis and business logic.
- View and behavior layering.
conclusion
Layered architecture is an indispensable idea in system architecture design. From the overall system architecture to a small function library package can reflect the idea of hierarchical design.
The functions of layering include: function cohesion, decoupling, enhancing code reuse, clarifying call relationship to avoid cyclic dependence, facilitating code structure organization, facilitating business expansion, etc.
That’s my idea of layering. I welcome private messages.
Invite attention to the public number: a row of weekly update technical articles, and we progress together.