Messaging1 min read

Saga Pattern

Keeping data consistent across microservices without distributed transactions — choreography, orchestration and compensation.

In a monolith, placing an order is one database transaction. With microservices, the order, payment and inventory data live in different databases, and a two-phase commit across them is slow and fragile. A saga replaces one big transaction with a sequence of local transactions, each with a compensating action that undoes it.

An order saga

  1. Order service creates the order as PENDING.

  2. Payment service charges the card.

  3. Inventory service reserves the items.

  4. Order service marks the order CONFIRMED.

If inventory fails at step 3, the saga runs compensations in reverse: refund the payment, then mark the order CANCELLED. Nothing is rolled back — it is semantically undone.

Choreography

Each service listens for events and publishes its own: OrderCreated → payment charges → PaymentCompleted → inventory reserves → ItemsReserved. No central coordinator, which is simple for a few steps, but the overall flow becomes hard to see and change as it grows.

Orchestration

A dedicated saga orchestrator tells each service what to do and tracks the state of the saga. The flow lives in one place, which makes it easier to reason about, monitor and test — at the cost of one more component.

Things that bite

  • Isolation: other requests can see intermediate states (an order that is PENDING). Design the UI and APIs for it.

  • Compensations must be idempotent and retryable — they can run more than once.

  • Reliable messaging: use the transactional outbox pattern so a local commit and its event are never out of sync.

  • Some steps cannot be undone (sending an email) — put them last.

A saga trades atomicity for availability: the system is eventually consistent, and every step has a plan for going backwards.