β
Category: Distributed Transactions / Microservices
β
Problem: How to maintain data consistency across multiple microservices without 2PC (two-phase commit).
β
Solution: Break long transactions into a series of local transactions. Each has a compensating action (undo) if something fails.
π Key Points
Orchestration Saga β Central coordinator tells services what to do.
Choreography Saga β Services listen to events and react accordingly.
Each step = Local transaction + Possible compensating action.
π₯οΈ C# Example β E-Commerce Order (Orchestration Saga)
Scenario:
Place Order
Reserve Payment
Update Inventory
If failure β rollback via compensating transactions
// Step definitions
public interface ISagaStep
{
Task ExecuteAsync();
Task CompensateAsync();
}
public class PaymentStep : ISagaStep
{
public async Task ExecuteAsync() => Console.WriteLine("β
Payment Reserved");
public async Task CompensateAsync() => Console.WriteLine("β Payment Refunded");
}
public class InventoryStep : ISagaStep
{
public async Task ExecuteAsync() => Console.WriteLine("β
Inventory Updated");
public async Task CompensateAsync() => Console.WriteLine("β Inventory Restored");
}
// Orchestrator
public class SagaOrchestrator
{
private readonly List<ISagaStep> _steps = new();
public void AddStep(ISagaStep step) => _steps.Add(step);
public async Task ExecuteAsync()
{
var executedSteps = new Stack<ISagaStep>();
try
{
foreach (var step in _steps)
{
await step.ExecuteAsync();
executedSteps.Push(step);
}
Console.WriteLine("π Saga Completed Successfully!");
}
catch
{
Console.WriteLine("β οΈ Error! Starting Compensation...");
while (executedSteps.Count > 0)
await executedSteps.Pop().CompensateAsync();
}
}
}
Usage:
var saga = new SagaOrchestrator();
saga.AddStep(new PaymentStep());
saga.AddStep(new InventoryStep());
await saga.ExecuteAsync();
π Real-World Example
π Remember in Interview
Saga = Long transaction broken into smaller steps.
Each step = do + undo (compensating action).
Two styles: Orchestration (central brain) vs Choreography (event-driven).
Prevents inconsistency in distributed microservices.