Microservices Design Patterns Series — Part 3/5
Building Resilient Systems with Design Patterns
This article is Part 3 of the Microservices Design Patterns series, focusing on the SAGA Pattern and API Gateway Pattern.
Previously, in Part 1, we explored the fundamentals of microservices, emphasized the importance of design patterns, and dissected the Service Registry Pattern and the Service Mesh Pattern. In Part 2, we explored how Circuit Breaker Pattern enhances system resilience and Event Sourcing Pattern enables reconstruction of the system state at any time.
In this article, we will dive into the SAGA pattern and API Gateway pattern, two essential architectural patterns in distributed systems. The SAGA pattern addresses data consistency in distributed transactions by breaking them into smaller steps, while the API Gateway pattern centralizes access to microservices through a single entry point. We’ll explore how these patterns work and their roles in building resilient and scalable distributed systems.
Saga Pattern
The Saga design pattern is a way to manage data consistency across microservices in distributed transaction scenarios. A saga is a sequence of transactions that updates each service and publishes a message or event to trigger the next transaction step. If a step fails, the saga executes compensating transactions that counteract the preceding transactions. -[5]
The saga pattern is the leading choice for handling distributed transactions and orchestrating complex processes in microservices. It solves challenges associated with traditional ACID transactions in decentralized microservice architectures.
By breaking down transactions into smaller units within each microservice, sagas enable independent execution without central coordination. They define steps and compensating actions to manage failures gracefully, exemplified in scenarios like e-commerce order processing.
There are two methods of coordinating sagas:
Choreography
The saga choreography pattern relies on microservices publishing events, which are then subscribed to and acted upon by other microservices, known as saga participants. For instance, when the order service emits an OrderPlaced event, the inventory service updates its inventory accordingly.
This pattern is ideal for simple implementations with few participants and no single point of failure. However, as the number of participants increases, tracking dependencies becomes more complex.
Orchestration
In the saga orchestration pattern, a central coordinator known as an orchestrator oversees the transaction lifecycle, managing and coordinating each step. It possesses knowledge of the sequential operations necessary to finalize the transaction.
To execute a step, the orchestrator dispatches a message to the relevant participant microservice. Upon completion, the participant microservice notifies the orchestrator, which then determines the subsequent microservice to engage based on the received message. This approach suits scenarios with many participants requiring loose coupling. However, relying on the orchestrator as a single point of control carries risks.
For further information on the Saga Pattern and its implementation, you can explore more here — Link
API Gateway Pattern
The API Gateway pattern serves as a single entry point for clients to access multiple services or microservices within a system. Acting as a reverse proxy, it routes incoming requests to the appropriate services, handling tasks such as authentication, authorization, rate limiting, and logging. By centralizing these concerns, the API Gateway simplifies client interaction and enhances security. Additionally, it enables efficient monitoring and management of service communication, contributing to improved scalability and resilience in distributed architectures.
The main characteristics of API Gateway architecture include:
- Routing and Load Balancing: API gateways route incoming requests to appropriate microservices based on predefined rules, ensuring reliability and scalability through load balancing across service instances.
- Protocol Translation: They facilitate the translation of diverse protocols and data formats. For instance, they can convert HTTP requests into formats compatible with backend services, such as gRPC.
- Request Transformation: API Gateways modify outbound requests and inbound responses according to backend service specifications, including parameter changes, body transformations, and header manipulation.
- Caching: Implementing caching mechanisms within API Gateways reduces request and response latency, enhancing overall performance by serving stored data directly to clients.
For further information on the API Gateway Pattern and its implementation, you can explore more here — Link
References
- Richardson, C. (2024). Pattern: Saga. https://microservices.io/patterns/data/saga.html
- Farheen, R. (2023, October 13). 4 Microservice Patterns Crucial in Microservices Architecture https://orkes.io/blog/4-microservice-patterns-crucial-in-microservices-architecture/
- Amazon Web Services, Inc. (2024). Saga. https://docs.aws.amazon.com/prescriptive-guidance/latest/cloud-design-patterns/saga.html
- GeeksforGeeks. (2024, April 5). API Gateway Patterns in Microservices. https://www.geeksforgeeks.org/api-gateway-patterns-in-microservices/
- Microsoft Learn. (n.d.). Saga pattern — Azure Design Patterns. Retrieved April 13, 2024, from Microsoft Learn