[System Design Series] Application layer and microservices

The original intention of the system design series

System Design Primer: 英文文档 GitHub - donnemartin/system-design-primer: Learn how to design large-scale systems. Prep for the system design interview. Includes Anki flashcards.

Chinese version: https://github.com/donnemartin/system-design-primer/blob/master/README-zh-Hans.md

The original intention is mainly to learn system design, but the Chinese version looks like machine translation, so I still take some simple notes manually, and compare the English version with the help of AI based on my own understanding of the difficult to understand parts. Translation and knowledge expansion.

Application layer

Source: Introduction to Scalable System Architecture

First, you need to understand the concepts of service layer and application layer:

service layer

        Mainly responsible for processing the business logic of the system. The service layer provides functional support to the application layer, including data verification, data processing, business process control, etc. The service layer provides a higher level of abstraction for the application layer by encapsulating the underlying data processing and logic, so that the application layer can use these services more conveniently.

Application layer

        It is the highest level of the software system and directly provides functions and services to users. The application layer includes various applications, clients, servers, etc. The application layer responds to and meets user needs by calling the functions provided by the service layer. The application layer needs to focus on user interaction, user experience, and the implementation of various applications.

relation

        The relationship between the service layer and the application layer is mainly reflected in the following aspects:

        The service layer provides services for the application layer: The service layer provides a higher level of abstraction for the application layer by encapsulating underlying data processing and logic. The application layer responds to and meets user needs by calling the functions provided by the service layer.

        The application layer depends on the service layer: The application layer needs to implement various functions and services, and these functions and services rely heavily on the support provided by the service layer. The quality of the service layer design directly affects the performance, stability and maintainability of the application layer.

        The service layer and the application layer are isolated from each other: The service layer and the application layer are functionally independent of each other, and the interface between them is clear and clear. In this way, when needs change, adjustments and modifications can be made flexibly. The design of mutual isolation between the service layer and the application layer helps to improve the maintainability and scalability of the system.

        The early service layer existed based on the application layer, and was deployed on the same platform and operated and maintained together.

        The proposal of microservices separates the Web service layer and the application layer (also called the platform layer), and the two layers can be scaled and configured independently. Adding a new API only requires adding an application server, not an additional web server.

        The single responsibility principle promotes small, autonomous services working together. Small teams can plan for growth more aggressively by providing smaller services.

        Work processes in the application layer can also be asynchronous .

microservices

         Microservices can be described as a series of small, modular services that can be deployed independently. Each service runs in an independent thread and communicates through well-defined lightweight mechanisms to jointly achieve business goals.

For example, Pinterest might have these microservices: user profiles, followers, feed stream, search, photo uploads, etc.

advantage

The main advantages of microservices include:

Flexibility: Microservices can be developed, tested, and deployed independently, thereby speeding up software development iterations. At the same time, each service can choose an appropriate technology stack based on actual needs, allowing developers to respond to different business scenarios more flexibly.

Scalability: By splitting a complex application into multiple simple microservices, the system can more easily scale horizontally when needed. This enables the system to quickly adjust resources according to changes in business needs and improve the overall performance of the system.

High availability: Since each microservice is independent, the failure of one service will not directly cause the entire system to crash. In addition, microservice architecture usually adopts a decentralized design, which further improves the fault tolerance of the system.

Loose coupling: Communication between microservices uses lightweight HTTP API, making the dependencies between services looser. This helps reduce the coupling between systems and makes the system more flexible in the face of changes in requirements.

shortcoming

Disadvantages of microservices:

Complexity: Microservice architectures are typically more complex than monolithic architectures. Developers need to master multiple technology stacks, understand how different services collaborate, and deal with issues such as communication and data consistency between services.

Deployment and operation costs: Because microservices need to run in multiple processes, they can be more expensive to deploy and operate. In addition, monitoring, log management and troubleshooting of distributed systems are also challenging.

Communication overhead: Communication between microservices is usually based on HTTP API, which may cause certain network overhead and latency. In high-concurrency scenarios, communication overhead may become a performance bottleneck.

Data consistency: In a microservice architecture, data consistency between different services requires special attention. Since data interaction between services is conducted through APIs, there may be issues with data synchronization and transaction processing.

Security: Multiple services in a microservices architecture can make the system more vulnerable to attacks. Developers need to fully consider the security protection of services and how to deal with security issues in a distributed environment.

Suitable for small projects: Microservice architecture may be too complex in some small projects, wasting resources and time. Therefore, developers need to choose an appropriate architecture based on the actual needs and scale of the project.

service discovery

Systems like  Consul , Etcd,  and  Zookeeper  can help services discover each other by tracking registration names, addresses, ports, and other information. Health checks  can help confirm the integrity of the service and whether an  HTTP  path is used regularly. Both Consul and Etcd have a built-in  key-value store  for storing configuration information and other shared information.

Main process

The main process of service discovery is as follows:

  1. Service registration: When a microservice starts, it will register its address, protocol and related metadata to the service registration center. The service registry is responsible for maintaining a list of available services so that other services can find it.
  2. Service discovery: When a microservice needs to call another microservice, it will obtain a list of available services from the service registry. Based on the information in the manifest, the calling service can find the callee's service address and port to make remote calls.
  3. Service health check: During the service discovery process, the health check of the service also needs to be performed to ensure that the called service is running normally. The service registration center can regularly receive health information provided by the service to monitor the service status.
  4. Service logout: When a microservice stops running, it will log out its information from the service registration center. In this way, when other services call the service, they will find that the service no longer exists, thus avoiding the call failure.

Service discovery plays a key role in microservice architecture, which solves the problem of location and communication between services. Through service discovery, microservices can communicate and collaborate more flexibly and efficiently, thereby improving the performance and scalability of the entire system.

Application uses microservices

When the application layer calls specific microservices, it needs to go through the following steps:

  1. Service registration: When a microservice starts, it will register its address, protocol and related metadata to the service registration center. The service registry is responsible for maintaining a list of available services so that other services can find it.
  2. Service discovery: When an application needs to call a microservice, it will obtain a list of available services from the service registry. Based on the information in the manifest, the application can find the address and port of the service that needs to be called, and make remote calls.
  3. Service call: The application sends a request to the microservice through HTTP or other protocols, and passes the requested data to the microservice. After the microservice receives the request, it processes the data and generates a response, and then returns the response to the application.
  4. Response processing: The application receives the response returned by the microservice and processes the response data. If the response contains error information, the application can perform corresponding processing based on the error information, such as retrying or reporting an error.
  5. Exception handling: During the service call process, if a network exception, timeout or other abnormal situation occurs, the application needs to perform corresponding exception handling to ensure the stability and reliability of the system.
  6. Load balancing and circuit breaker: In order to improve the availability and performance of the system, load balancing technology can be used to distribute microservices. In addition, when a microservice fails, a circuit breaker mechanism can be used to remove it from the service registration center to avoid failure of other application calls.

Through the above process, applications can call microservices, thereby fully utilizing the advantages of microservice architecture and improving system flexibility, scalability, and high availability. At the same time, during the calling process, you also need to pay attention to issues such as service security and data consistency to ensure the stability and reliability of the microservice architecture.

 

Guess you like

Origin blog.csdn.net/u013379032/article/details/132756765