Saga Pattern

1. What exactly is Saga Pattern?

Checkout calls Payments, then calls Inventory, as two separate steps against two separate services.

Payments charges the customer. That part goes fine.

Then Inventory fails, the item's out of stock, and there's no single transaction spanning both services to undo that charge automatically.

A process spanning two systems can succeed halfway, and nothing automatically undoes the half that already happened. That's exactly the gap the Saga pattern closes.

2. How would you fix it in .NET?

The Saga pattern gives every step an undo: set up a matching undo action ahead of time for each one, Payments' undo is a refund. When Inventory fails partway through the process, run the undo action for every step that already succeeded before it, so the customer ends up refunded, not charged with nothing to show for it.

Use: MassTransit (Saga State Machine)

When there's no single transaction across services, give every step its own undo, and run it the moment something later fails.

3. How can you visualize it in everyday terms?

A travel agent books your flight, then tries to book your hotel, and the hotel comes back sold out.

A good agent doesn't leave you holding a paid-for flight with nowhere to stay. They cancel and refund the flight too, undoing the step that already succeeded.

When a later step fails, undo the earlier ones instead of leaving them stranded.