10 Essential Microservice Design Patterns Every Backend Engineer Should Know
Microservice design patterns provide proven solutions for common challenges such as service decomposition, communication, governance, and testing, and this article introduces ten key patterns—including API Gateway, Service Discovery, Circuit Breaker, Bulkhead, CQRS, Event‑Driven, Saga, Strangler, Sidecar, and BFF—to help build efficient, reliable, and scalable systems.
Microservice design patterns are a set of principles and practices that guide the design and development of microservice architectures. They aim to address common problems such as service decomposition, communication, governance, and testing, helping to build efficient, reliable, scalable, and maintainable systems.
The article introduces the following ten microservice design patterns:
API Gateway (Api Gateway Pattern)
Service Registration and Discovery (Service Registration and Discovery Pattern)
Circuit Breaker (Circuit Breaker Pattern)
Bulkhead (Bulkhead Pattern)
CQRS (Command and Query Responsibility Segregation Pattern)
Event‑Driven Pattern
Saga Pattern
Strangler Pattern
Sidecar Pattern
Backend for Frontend (BFF) Pattern
1. API Gateway
The API gateway serves as the entry point for all microservice calls, sitting between clients and services. It handles cross‑cutting concerns such as authentication, rate limiting, retries, load balancing, and service discovery, and can also perform request filtering, mapping, and aggregation based on client needs. This simplifies client logic, reduces network overhead, protects backend services, and enables different API versions. In Spring Cloud, Zuul or Spring Cloud Gateway can be used to implement this pattern.
Typical Use Cases
Provide a single entry point for all microservice calls, simplifying client logic.
Route requests to different services based on configurable rules.
Integrate with registration centers like Eureka, Nacos, or Ribbon for discovery and load balancing.
Aggregate responses from multiple backend services before returning to the client.
Perform protocol translation between services using different communication protocols.
Offload authentication and authorization from individual services to the gateway.
Log request details centrally.
Implement rate limiting and circuit breaking at the gateway level.
2. Service Registration and Discovery
Service registration and discovery manage the dynamic location information of microservice instances. Services register their address (IP and port) with a central registry and periodically send heartbeats. Clients query the registry to obtain the current address of a service by its logical name, enabling load‑balanced calls without hard‑coded endpoints.
Service Registry : Stores instance metadata and provides APIs for registration, lookup, and deregistration.
Discovery Client : Retrieves instance information from the registry and selects an appropriate instance based on a load‑balancing strategy.
Benefits include dynamic discovery of new or removed instances, load balancing across instances, and fault tolerance by avoiding calls to unhealthy services.
Common registries:
Eureka – Netflix’s discovery service, widely used in older Spring Cloud versions.
Consul – HashiCorp’s service mesh solution with health checking and multi‑datacenter support.
Zookeeper – Apache’s coordination service offering naming, configuration, and distributed locks.
Nacos – Alibaba’s dynamic service discovery and configuration platform.
Etcd – A Raft‑based key‑value store providing strong consistency, often used for service registration.
3. Circuit Breaker
The circuit breaker pattern protects a system from cascading failures caused by remote call timeouts or errors. It monitors call outcomes and, when a failure threshold is exceeded, opens the circuit to short‑circuit further calls, optionally providing fallback logic. After a cooldown period, it moves to a half‑open state to test the remote service before fully closing again. Spring Cloud implementations include Hystrix and Resilience4j.
A circuit breaker behaves like an electrical fuse: when consecutive request failures exceed a threshold, the breaker trips for a defined period, causing all subsequent calls to fail immediately. After the timeout, a limited number of test requests are allowed; if they succeed, normal operation resumes, otherwise the breaker trips again.
4. Bulkhead Pattern
The bulkhead pattern isolates resources (e.g., thread pools) for different downstream services, preventing a failure or slowdown in one service from exhausting shared resources needed by others. For example, a service with a 50‑thread pool can allocate 25 threads each for calls to Service B and Service C, ensuring that a slowdown in Service C does not affect Service B. Resilience4j can be used to implement this in Spring Cloud.
Bulkheads partition resources like compartments in a ship, isolating failures and allowing unaffected partitions to continue operating.
5. Command and Query Responsibility Segregation (CQRS)
CQRS separates read and write responsibilities into distinct models, allowing each to be optimized independently. Reads can use a query‑optimized database, while writes use a model that ensures consistency. This improves performance, scalability, and security, but introduces complexity in maintaining data synchronization and handling eventual consistency.
Advantages
Allocate resources based on read/write ratios, improving efficiency.
Use different data models and validation rules per use case, enhancing flexibility.
Reduce data conflicts and concurrency issues, increasing reliability.
Disadvantages
Increased complexity due to maintaining multiple data stores and synchronization.
Potential latency and eventual‑consistency challenges that may affect user experience.
6. Event‑Driven Pattern
In an event‑driven architecture, microservices communicate asynchronously by publishing and subscribing to events. Events represent state changes (e.g., order created, payment completed). This decouples services, improves scalability and reliability, and supports complex workflows. Challenges include ensuring ordering, idempotency, consistency, and handling distributed transactions. Message brokers such as RabbitMQ or Apache Kafka are commonly used.
Event‑driven communication lets services react to published events without direct calls, allowing each service to focus on its own business logic.
7. Saga Pattern
Sagas manage data consistency across microservices without using two‑phase commit. A saga consists of a series of local transactions, each followed by an event that triggers the next step. If a step fails, compensating transactions undo previous actions, ensuring eventual consistency. Sagas can be coordinated (decentralized) or orchestrated (centralized).
Coordination
In the coordination approach, participants exchange events directly without a central controller. Each local transaction publishes an event that other services listen to and react accordingly.
Benefits
Suitable for simple workflows with few participants.
No additional coordination service required.
No single point of failure; responsibility is distributed.
Drawbacks
Adding new steps can make the workflow hard to track.
Potential circular dependencies between participants.
Integration testing is difficult because all services must be running.
Orchestration
Orchestration uses a central saga orchestrator that tells each participant which local transaction to execute. The orchestrator tracks the state of each step and issues compensating actions when failures occur.
Benefits
Suitable for simple workflows with few participants.
No extra service implementation needed beyond the orchestrator.
Eliminates a single point of failure by distributing responsibilities.
Drawbacks
Workflow can become tangled as new steps are added.
Risk of circular dependencies due to command sharing.
Testing requires all services to be up, making integration tests complex.
8. Strangler Pattern
The strangler pattern enables gradual migration from a monolithic application to microservices. A new parallel service is built to replace parts of the monolith while keeping the external API unchanged. Over time, the new services take over functionality, and the old monolith is retired.
Steps:
Transform – Create a new parallel service.
Coexist – Redirect requests from the old service to the new one while both run.
Eliminate – Remove the legacy service once migration is complete.
Advantages include reduced risk and cost by allowing incremental migration; disadvantages involve maintaining compatibility between two systems, increasing complexity and development effort.
9. Sidecar Pattern
The sidecar pattern extracts cross‑cutting concerns (logging, monitoring, configuration, security, etc.) from the main application into a separate process or service that runs alongside it in the same host or container. The main app focuses on business logic, while the sidecar provides auxiliary functions via local interfaces or shared memory.
Benefits: lower coupling, improved performance, reliability, and easier maintenance. Drawbacks: additional resource consumption and the need to manage communication between the sidecar and the main app.
10. Backend for Frontend (BFF) Pattern
The BFF pattern creates a dedicated backend service tailored to the needs of a specific frontend or channel. It aggregates and adapts multiple microservice APIs into a unified, frontend‑friendly API, improving user experience and development efficiency by reducing over‑fetching and simplifying client‑side logic.
Advantages: customized APIs per frontend, reduced network overhead, and better separation of concerns. Disadvantages: extra development and maintenance effort for each frontend channel, potentially leading to code duplication.
Typical Scenarios
API slicing: Different frontends require different fields; BFF provides only the needed data.
API aggregation: Combine multiple backend calls into a single request for the frontend.
Conclusion
The ten design patterns described above can help build extensible software systems. In practice, choose the patterns that best fit your specific business scenarios and requirements.
Thank you for reading; hope this article proves helpful.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
