Skip to content

3. Microservices

Microservices, also known as the microservices architecture, is an architectural style that structures an application as a collection of small autonomous services, modeled around a business domain.

Key concepts of microservices include:

  • Single Responsibility: Each microservice should have a single responsibility and should implement a single business capability.
  • Independence: Microservices should be able to run and evolve independently of each other. They should be independently deployable and scalable.
  • Decentralization: Microservices architecture favors decentralized governance. Teams have the freedom to choose the best technology stack that suits their service.
  • Isolation of Failures: If a microservice fails, it should not impact the availability of other services.
  • Data Isolation: Each microservice should have its own database to ensure that the services are loosely coupled and can evolve independently.
  • Communication: Microservices communicate with each other through well-defined APIs and protocols, typically HTTP/REST with JSON45 or gRPC with Protobuf.
  • Infrastructure Automation: Due to the distributed nature of the microservices architecture, automation of infrastructure is a must. This includes automated provisioning, scaling, and deployment.
  • Observability: With many different services, it's important to have excellent monitoring and logging to detect and diagnose problems.

Domain Driven Design

Domain-Driven Design (DDD) is a software development approach that emphasizes collaboration between technical experts and domain experts. The goal is to create software that is a deep reflection of the underlying domain, which is the specific area of business or activity that the software is intended to support.

Key concepts of DDD include:

  • Ubiquitous Language: A common language established between developers and domain experts, used to describe all aspects of the domain.
  • Bounded Context: A boundary within which a particular model is defined and applicable.
  • Entities: Objects that have a distinct identity that persists over time and across different representations.
  • Value Objects: Objects that are defined by their attributes, not their identity.
  • Aggregates: Clusters of entities and value objects that are treated as a single unit.
  • Repositories: They provide a way to obtain references to aggregates.
  • Domain Events: Events that domain experts care about.
  • Services: Operations that don't naturally belong to any entity or value object.

By focusing on the domain and domain logic, DDD provides techniques to develop complex systems targeting real-world scenarios. It helps to reduce the complexity by dividing the system into manageable and interconnected parts.

Best Practices

Best practices for microservices

Source: System Design 101 - Microservice Architecture

Typical Microservices Architecture

typical microservices architecture

Source: System Design 101 - Microservice Architecture

Components of a typical microservices architecture include:

  • Load Balancer: Distributes incoming network traffic across multiple servers to ensure no single server becomes overwhelmed.

  • CDN (Content Delivery Network): A geographically distributed network of servers that delivers content to users based on their location, improving performance and availability.

  • API Gateway: Acts as a single entry point for all clients, routing requests to the appropriate microservices and handling cross-cutting concerns such as authentication, logging, and rate limiting.

  • Identity Provider: Manages user authentication and authorization, often using protocols like OAuth2 or OpenID Connect.

  • Service Discovery & Registry: Allows microservices to find and communicate with each other without hardcoding their network locations.

  • Configuration Service: Centralizes the management of configuration settings for all microservices, allowing for dynamic updates without redeploying services.

  • Microservices: Independent services that implement specific business capabilities, communicating with each other through APIs.

Our Microservices Architecture

The proposed architecture for our microservices will be based on the principles of Domain-Driven Design (DDD) and will follow the Clean Architecture pattern. Each microservice will be designed to have a single responsibility, and we will ensure that they are loosely coupled and independently deployable. We will also implement a robust API Gateway to handle client requests and route them to the appropriate microservices. Additionally, we will use a service discovery mechanism to allow microservices to find and communicate with each other without hardcoding their network locations. Finally, we will implement monitoring and logging to ensure that we can detect and diagnose problems effectively in our distributed system. A diagram of the proposed architecture is shown below.

flowchart LR
  subgraph client["Internet"]
    direction LR
    Web
    Mobile
    Desktop
  end
  subgraph Trusted Layer
    direction LR
    lb(["Load Balance"])
    gateway["Gateway"]
    auth["Auth"]
    subgraph bm ["Business Microservice"]
      direction LR
      ms1["Service 1"]
      ms2["Service 2"]
      ms3["Service 3"]
      db1[("Database 1")]
      db2[("Database 2")]
    end
  end
  subgraph third-party["Third-Party API"]
    direction LR
    tp1["Third-Party Service"]
  end
  client --> lb --> gateway
  gateway --> ms1
  gateway --> ms2
  gateway --> ms3
  ms1 --> db1
  ms2 --> db2
  ms2 --> ms3
  ms3 --> tp1
  gateway --> auth

  1. XU, A., System Design 101: A comprehensive guide to system design, covering various architectural patterns, including microservices. It provides insights into best practices, trade-offs, and real-world examples to help developers design scalable and maintainable systems. 

  2. Wikipedia - Domain Driven Design: A software development approach that emphasizes collaboration between technical experts and domain experts to create software that is a deep reflection of the underlying domain. It provides techniques to develop complex systems targeting real-world scenarios by focusing on the domain and domain logic. 

  3. Domain-Driven Design Reference: A comprehensive reference for Domain-Driven Design, covering all the key concepts and patterns in detail. It serves as a valuable resource for developers and architects looking to implement DDD in their projects. 

  4. RFC 7159: The application/json Media Type for JavaScript Object Notation (JSON). 

  5. JSON: JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.