In the world of microservices, maintaining data consistency across distributed services is one of the most challenging problems. The traditional ACID transactions (Atomicity, Consistency, Isolation, Durability) we rely on in monolithic databases simply don't scale well across multiple independent services. Enter the Saga Pattern.
Imagine a travel booking application. When a user books a trip, three distinct services might need to be invoked:
If the Flight and Hotel bookings succeed but the Payment fails, we can't simply "rollback" the database transaction because the flight and hotel reservations are in different databases. We need a mechanism to undo the changes made by the previous steps. This is where Sagas come in.
A Saga is a sequence of local transactions. Each local transaction updates the database and publishes a message or event to trigger the next local transaction in the saga. If a local transaction fails because it violates a business rule, the saga executes a series of compensating transactions that undo the changes that were made by the preceding local transactions.
There are two main ways to coordinate sagas:
| Feature | Choreography | Orchestration |
|---|---|---|
| Description | Services exchange events without a centralized point of control. | A centralized controller (Orchestrator) tells participants what local transactions to execute. |
| Pros | Simple, loose coupling, no single point of failure. | Centralized logic, easier to understand workflow, reduced cyclic dependencies. |
| Cons | Hard to track status, potential for cyclic dependencies. | Tighter coupling to orchestrator, orchestrator can become a bottleneck. |
Explore the interactive visualization below to see how a booking saga handles both success and failure scenarios using an orchestration approach.
Use the Saga pattern when:
The Saga pattern provides a robust way to handle distributed transactions in microservice architectures. While it introduces complexity compared to monolithic transactions, it is essential for building scalable, resilient systems. Choosing between choreography and orchestration depends on the complexity of your workflow and your team's familiarity with the patterns.
← Back to System Design