Saga Pattern

Problem
  1. Checkout calls Payments, then calls Inventory, as two separate steps against two separate services.
  2. Payments charges the customer successfully.
  3. Inventory then fails because the item is out of stock, and there's no single transaction spanning both services to undo the charge automatically.

A process spanning two systems can succeed halfway, and nothing automatically undoes the half that already happened.

Solution Saga Pattern
  1. Give every step a matching undo action ahead of time: Payments' undo is a refund.
  2. When Inventory fails partway through the process, run the undo action for every step that already succeeded before it.
  3. The customer ends up refunded, not charged with nothing to show for it.

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

Analogy

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.

The saga pattern is that agent: when a later step fails, undo the earlier ones instead of leaving them stranded.