π§© Microservices Design Patterns
π§© Microservices Design Patterns
1. Decomposition Patterns
By Business Capabilityβ Services align with domain areas (e.g., Payments, Orders, Inventory).By Subdomain (DDD)β Use Domain-Driven Design to identify bounded contexts.Strangler Fig Patternβ Gradually replace monolith pieces with microservices.
2. Integration Patterns
API Gatewayβ Single entry point for clients, routes to services.Aggregatorβ One service calls multiple others and aggregates results.Proxyβ Gateway just forwards requests, adds security, throttling.Chained Service Callsβ Service β Service β Service (can get risky with latency).Asynchronous Messagingβ Use queues/events (Kafka, RabbitMQ) to decouple services.
3. Database Patterns
Database per Serviceβ Each service owns its data (most common).Shared Databaseβ Multiple services share one DB (not recommended, but sometimes practical).Saga Patternβ Distributed transaction management via events/compensating actions.CQRS (Command Query Responsibility Segregation)β Separate read/write models for performance.Event Sourcingβ Store state as a series of events instead of just final state.
4. Observability Patterns
Log Aggregationβ Centralized logs (ELK, Loki).Distributed Tracingβ Trace requests across services (Jaeger, Zipkin, Tempo).Health Check APIβ Each service exposes/healthfor monitoring.Metrics Aggregationβ Collect metrics (Prometheus + Grafana).
5. Resiliency Patterns
Circuit Breakerβ Stop calls to failing service (e.g., Resilience4j, Hystrix).Retry Patternβ Retry failed calls with backoff.Timeout Patternβ Donβt let one service hang others.Bulkheadβ Isolate failures so one service doesnβt crash the system.Fail-Fastβ Quickly reject if downstream is not available.Fallbackβ Provide default response when service fails.
6. Security Patterns
Access Token (JWT, OAuth2)β Secure service-to-service & client calls.API Gateway Authenticationβ Central place for auth.Service Mesh (Istio, Linkerd)β Handles mTLS, auth, rate limiting at infra level.
7. Deployment Patterns
Blue-Green Deploymentβ Switch traffic between two environments.Canary Releaseβ Gradually roll out new versions.Sidecar Patternβ Extra container alongside a service (e.g., Envoy proxy).Service Meshβ Offload cross-cutting concerns (security, retries, tracing).
β
These patterns are often combined in real-world microservices systems depending on business needs, scalability, and reliability.
β Bulkhead Pattern (Resiliency)
π What It Is
The Bulkhead pattern is a resiliency design pattern that isolates failures in a system so that when one part fails, it doesnβt bring down everything else.
Itβs inspired by ships:
- A ship has bulkheads (partitions) so if one compartment floods, the others stay safe.
- Similarly, in microservices, you isolate resources so a failure in one area wonβt sink the entire system.
π How It Works
- Allocate
separate resources(threads, DB connections, memory pools) per service or per feature. - If one service gets overloaded, it
only consumes its own quota, not the entire pool. - Prevents a βnoisy neighborβ problem where one service hogs resources and starves others.
β Example
Imagine an e-commerce app with:
Payment ServiceOrder ServiceNotification Service
If the Payment Service suddenly gets slow (maybe the bank API is lagging):
Without bulkheadβ all system threads are stuck waiting, and Orders + Notifications also fail.With bulkheadβ only the Payment Serviceβs thread pool is affected, Orders + Notifications still work.
π§ Implementation Approaches
Thread pool per serviceβ Each service gets its own threads.Connection pool isolationβ Separate DB connections per microservice.Rate limiting per serviceβ Prevent one service from exhausting shared resources.With Circuit Breakerβ Often combined: bulkhead isolates, circuit breaker stops repeated failures.
π Tools & Frameworks
Java / Spring Bootβ Use Resilience4j bulkhead module (BulkheadRegistry)..NETβ Polly provides bulkhead policies.Service Mesh (Istio/Linkerd)β Can enforce isolation & limits at infra level.
π In short: Bulkhead = Contain the blast radius of a failure.
β‘ Circuit Breaker (Microservices)
Definition
Circuit Breaker is a resiliency pattern that stops calling a failing service to prevent cascading failures and system overload.
π How It Works (States)
-
Closed- Normal operation
- Requests flow
- Failures are monitored
-
Open- Too many failures detected
- Calls are blocked immediately
- Fallback response is returned
-
Half-Open- After wait time
- Allows few test requests
- If success β back to
Closed - If failure β back to
Open
π― Why Use It
- Prevents system overload
- Avoids cascading failures
- Improves response time
- Enables graceful degradation
π Example
Order Service β calls β Payment Service
If Payment Service is down:
- Without circuit breaker β Order service keeps retrying β thread pool exhausted
- With circuit breaker β calls blocked β instant fallback β system stays stable
π§ Implementation
@CircuitBreaker(name = "paymentCB", fallbackMethod = "fallback")
public String pay() {
return paymentClient.call();
}
public String fallback(Exception e) {
return "Payment service unavailable";
}
π Popular Tools
Resilience4j(Java)Hystrix(Deprecated)Polly(.NET)Istio / Service Mesh(infra level)
π In Short
Circuit Breaker = Fail fast + protect system + auto recovery