19 Microservices Design Patterns
Microservices architecture is a design approach that structures an application as a collection of loosely coupled services.
To build scalable, maintainable, and resilient microservices-based systems, various patterns have emerged.
Here are essential microservices patterns you can use in your project, and also remember for system design interviews.
- Service Registry Since there are many microservices in a Microservice architecture, they need to discover and communicate with each other.
A Service Registry, such as Netflix Eureka or Consul, acts as a centralized directory where services can register themselves and discover others.
Here is what it looks like:
Service Registry Pattern
2.** API Gateway**
An API Gateway serves as a single entry point for client applications, aggregating multiple microservices into a unified API.
It handles requests, routing them to the appropriate services, and may perform tasks like authentication, authorization, and load balancing.
Here is what API Gateway looks like:
API Gateway
- Circuit Breaker Inspired by electrical circuit breakers, this pattern prevents a microservice failure from cascading to other services. Circuit breaker pattern monitors for failures, and if a threshold is crossed, it opens the circuit, preventing further requests.
This helps in graceful degradation and fault tolerance, and it’s absolutely must in a Microservice architecture to prevent total shutdown of your services.
Here is an example of Netflix Hysrix as a Circuit breaker:
Circuit Breaker
- Bulkhead In a microservices system, isolating failures is crucial. The Bulkhead pattern involves separating components or services to contain failures.
For example, thread pools or separate databases for different services can be used to prevent a failure in one part of the system from affecting others.
Here is a diagram showing the Bulkhead pattern in Microservices architecture:
Bulkhead Pattern
- Saga Pattern This pattern is used for managing distributed transactions. The Saga pattern breaks down a long-running business transaction into a series of smaller, independent transactions.
Each microservice involved in the saga handles its own transaction and publishes events to trigger subsequent actions.
Here is how the Saga Pattern looks in action:
Saga Pattern
- Event Sourcing This is another popular pattern that is used heavily in high-frequency low-latency applications.
In this pattern, instead of storing only the current state, Event Sourcing involves storing a sequence of events that led to the current state.
This pattern provides a reliable audit trail and allows for rebuilding the system state at any point in time.
Here is how Event Sourcing looks in action:
Event Sourcing
- Command Query Responsibility Segregation (CQRS) The CQRS Pattern separates the read and write sides of an application. It uses different models for updating information (commands) and reading information (queries).
This pattern can improve scalability, as read and write operations have different optimization requirements.
Here is a nice diagram that shows the CQRS pattern:
Command Query Responsibility Segregation (CQRS)
- Data Sharding The database sharding pattern is used to distribute the database load and avoid bottlenecks. Data Sharding involves partitioning data across multiple databases or database instances.
In this pattern, each microservice may handle a subset of data or specific types of requests.
Here is how database sharding looks, credit - Design Guru
Types of Database sharding
- Polyglot Persistence Different microservices may have different data storage needs. Polyglot Persistence allows using multiple database technologies based on the requirements of each microservice, optimizing for data storage, retrieval, and query capabilities.
Here is a nice diagram that shows Polyglot persistence in Azure :
Polyglot Persistence
- Retry In Microservice architecture, when a transient failure occurs, the Retry pattern involves retrying the operation instead of immediately failing.
It can be applied at various levels, such as service-to-service communication or database interactions.
Here is a nice diagram from ByteByteGo, a great place for system design learning, which shows the Retry pattern in Microservices:
Retry Pattern in Microservices
- Sidecar The Sidecar pattern involves attaching a helper service (the sidecar) to the main microservice to provide additional functionalities such as logging, security, or communication with external services.
This allows the main service to focus on its core functionality.
Here is what a Sidecar pattern looks like:
Sidecar pattern in Microservices
- Backends for Frontends (BFF) Also known as BFF, this pattern is useful when dealing with multiple client types (e.g., web, mobile). The BFF pattern involves creating separate backend services tailored for each type of client.
This allows for optimized and specialized APIs for each client.
Here is what a Backends for Frontends (BFF) pattern looks like:
Backends for Frontends (BFF)
14.** Shadow Deployment**
The Shadow Deployment pattern involves routing a copy (shadow) of production traffic to a new microservice version without affecting the actual user experience.
This is one of the popular deployment strategies, and it helps validate the new version’s performance and correctness.
Here is what shadow deployment looks like
Shadow Deployment
- Consumer-Driven Contracts In a microservices ecosystem, multiple services often interact with one another. The Consumer-Driven Contracts pattern involves consumers specifying their expectations from producers, allowing for more robust and coordinated changes.
Here is a nice diagram that explains Consumer-Driven contracts
Consumer-Driven Contracts
Smart Endpoints, Dumb Pipes
This pattern advocates for placing business logic in microservices (smart endpoints) rather than relying on complex middleware. The communication infrastructure (pipes) should be simple and handle only message routing.Database per Service
This is another popular Microservices pattern where each microservice has its own database, and services communicate through well-defined APIs.
Database per Service pattern provides isolation but also requires careful consideration of data consistency and integrity.
Here is how this pattern looks:
Database per Service pattern
- Async Messaging Instead of synchronous communication between microservices, the Async Messaging pattern involves using message queues to facilitate asynchronous communication. This can improve system responsiveness and scalability.
Here is a nice diagram that shows the difference between sync and async messaging
Async Messaging pattern
- Stateless Services Designing microservices to be stateless simplifies scalability and resilience. Each service processes a request independently, without relying on stored state, making it easier to scale horizontally.
Here is a nice diagram that shows the difference between Stateless Services and Stateful Services
Stateless Services
Top comments (0)