What the Heck is SAGA? ๐ค
SAGA is a design pattern used to manage distributed transactions in microservices. When youโve got multiple services talking to each other, ensuring data consistency can be a nightmare. SAGA swoops in like a superhero to coordinate things smoothly.
Also: “saga” does not actually stand for anything as it’s not an acronym; instead, the term is borrowed from storytelling, signifying a sequence of events that form a complete narrative,
So Whatโs the Problem we need to solve?
In a monolithic system, transactions are simple. You wrap everything in a database transaction (BEGIN TRANSACTION โฆ COMMIT
), and if anything fails, you roll it back (ROLLBACK
).
But in microservices, each service has its own database. So, if Service A calls Service B, which calls Service C, and something breaks halfway through, you canโt just roll back everything.
Enter SAGA. Instead of a single transaction, it breaks things into a series of smaller transactions, ensuring consistency through compensating actions (i.e., undo operations).
How SAGA Works ๐ ๏ธ
SAGA ensures that if something fails mid-way, the system doesnโt end up in a half-baked state. There are two main ways to implement it:
1. Choreography ๐บ
Each service is like a well-trained dancerโit knows its moves and what to do next. Thereโs no central brain; each service reacts to events and triggers the next step.
- Example: You book a flight, which triggers a hotel booking, which triggers a car rental.
- If the hotel booking fails, an event is emitted to cancel the flight.
Pros:
โ
Simple to implement for small workflows
โ
No central coordinator required
Cons:
โ Can become messy with too many services
โ Harder to debug and track events
2. Orchestration ๐ป
Here, a central orchestrator (think of a conductor in an orchestra) manages the entire flow. It calls each service in sequence and handles rollbacks if anything goes wrong.
- Example: A central Order Service coordinates payments, shipping, and inventory.
- If shipping fails, the orchestrator triggers a refund.
Pros:
โ
More control and visibility
โ
Easier debugging
Cons:
โ Adds complexity (you need an orchestrator service)
โ Can become a single point of failure (unless made resilient)
Real-World Example: Ordering a Pizza ๐
Letโs say you order a pizza. Hereโs how SAGA keeps things sane:
- Order Service โ Receives order and initiates payment.
- Payment Service โ Charges your card.
- Kitchen Service โ Starts making the pizza.
- Delivery Service โ Assigns a delivery guy.
- Success! ๐ Your pizza arrives, and life is good.
But what if something goes wrong?
- If Payment fails โ Cancel the order.
- If the Kitchen burns your pizza โ Refund the payment.
- If no delivery driver is available โ Offer pickup instead.
Choreography Example: Order Processing (C#) ๐
Hereโs a choreographed SAGA for an order processing system in C#:
|
|
Breakdown:
โ
Each service does its part and calls the next step.
โ
No central coordinator.
โ If something fails, rolling back is tricky! ๐ฌ
Orchestration Example: Order Processing (Python) ๐
Now, letโs do the same thing using a central orchestrator in Python:
|
|
Breakdown:
โ
Orchestrator (OrderSaga) manages the flow.
โ
Easier rollback handling.
โ Slightly more complex to set up.
Which One Should You Use? ๐คท
Pattern | Pros | Cons |
---|---|---|
Choreography | Simple, no central service needed | Hard to debug, complex failure handling |
Orchestration | More control, easier rollback | Adds complexity, requires extra service |