DAY 3 - SAGA Design Pattern
To manage distributed transactions across multiple microservices. It divides a large transaction into a series of smaller local transactions. Executed independently. Each service performs its own transaction via events or commands. If one transaction fails --> compensating actions are executed to undo previously completed operations. Distributed Transaction Multiple db or system working together…
Day 3: SAGA Design Pattern
The SAGA design pattern is utilized to manage distributed transactions across multiple microservices. It breaks down a large transaction into smaller local transactions, each executed independently by the respective service. Each service performs its transaction via events or commands.
In the event of a failure in one transaction, compensating actions are executed to undo previously completed operations. This pattern is employed to manage distributed transactions in microservices without relying on complex protocols like the 2-phase commit (2PC). The main advantage of SAGA is that it allows for the management of distributed transactions in microservices without using complex protocols like 2PC.
The working of SAGA involves dividing distributed transactions into a sequence of smaller transactions that execute independently across multiple services. There are two approaches to implementing SAGA: Choreography-Based Approach (Event-Driven) and Orchestration-Based Approach (Centralized).
In the Choreography-Based Approach, each service independently knows what to do and communicates through events using message queues or event streams. If a service fails, it publishes a failure event, and the other services perform compensating actions accordingly.
On the other hand, in the Orchestration-Based Approach, a SAGA Execution Coordinator (orchestrator) controls the flow of the transaction. The orchestrator tells each service when to start, what to do, and when to proceed to the next step. This approach can become complex as the system scales, and accidental event loops can occur.
Advantages of the SAGA design pattern include flexibility and scalability, as transactions can be executed independently without global locking. There are no single points of failure since transactions are distributed across multiple services. Additionally, the pattern provides fault tolerance through compensating actions to handle failures gracefully. Sagas embrace eventual consistency, meaning the system transitions through intermediate states, and the user experience remains transparent in a banking application.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.