System Design Patterns for Microservices Architecture
1. API Gateway Pattern
A single entry point that routes requests to the appropriate microservice, handles authentication, rate limiting, and aggregation. - Pros: Centralized auth, cross-cutting concerns handled once. - Cons: Single point of failure (mitigate with multiple gateway instances + DNS failover).
2. Circuit Breaker Pattern
Prevents cascading failures by stopping calls to a failing service after a threshold of failures.
CLOSED → (failures exceed threshold) → OPEN → (timeout) → HALF_OPEN → (success) → CLOSED
- Libraries: Hystrix (Java), resilience4j, circuitbreaker (Python).
3. Saga Pattern
Manages distributed transactions across microservices using a sequence of local transactions with compensating actions on failure. - Choreography: Each service emits events that trigger the next step. - Orchestration: A coordinator service manages the saga steps.
4. CQRS (Command Query Responsibility Segregation)
Separate read models from write models to optimize each independently. - Commands: Handle writes (can use event sourcing). - Queries: Handle reads (can use denormalized read-optimized views).
5. Strangler Fig Pattern
Incrementally replace a monolithic system by gradually migrating specific functionalities to new microservices while the legacy system remains operational.