All academies
Roadmap
Level 15Distributed Patterns 35 min

Distributed Transactions

Why ACID breaks across services: two-phase commit, sagas, compensation, and eventual consistency.

Hands-on lab: Saga Pattern SimulatorPair this lesson with its interactive simulator

The problem: ACID doesn't cross service boundaries

Placing an order touches Orders, Inventory, and Payments — three services, three databases. No single database transaction can span them. Yet the business still requires all-or-nothing semantics: never charge without reserving stock.

Two-phase commit (2PC) and why it's avoided

The classic answer is two-phase commit: a coordinator asks all participants to 'prepare' (lock resources and promise to commit), then orders 'commit' once all agree.

In microservices, 2PC is almost always rejected: participants hold locks while waiting (killing throughput), the coordinator is a single point of failure whose crash leaves everyone blocked, and most modern datastores and brokers don't support XA anyway.

Coordinator: PREPARE → all participants
All vote YES (resources locked)
Coordinator: COMMIT → all participants
Locks released

The saga pattern

A saga replaces one distributed transaction with a sequence of local transactions, each committed immediately in its own service. If step N fails, previously completed steps are undone by compensating transactions executed in reverse order.

Compensation is business-level undo, not rollback: 'refund the payment', 'release the reservation'. Some actions can't be literally undone (an email was sent) — you compensate semantically (send a cancellation email).

Create order (pending)
Reserve inventory ✓
Charge payment ✗ FAILS
Compensate: release inventory
Compensate: cancel order

Choreography vs orchestration

  • Choreography — no central brain: each service reacts to events and emits its own. Loosely coupled, but the workflow logic is scattered — nobody can point to where 'the process' is defined.
  • Orchestration — a saga orchestrator (e.g. in the Order service, or Temporal/Camunda) explicitly commands each step and handles failures. The flow is visible in one place; the orchestrator must itself be reliable.
  • Rule of thumb: choreography for simple 2–3 step flows; orchestration once flows grow branches, timeouts, and human steps.

Living with eventual consistency

Between saga steps the system is temporarily inconsistent — an order exists whose payment hasn't settled. Design for it: model intermediate states explicitly (PENDING, AWAITING_PAYMENT), make every step idempotent (retries!), and use timeouts with automatic compensation for stuck sagas.

Key takeaways

  • No ACID across services — 2PC blocks, doesn't scale, and isn't supported by modern stacks.
  • Sagas: sequences of local transactions + compensating transactions for undo.
  • Choreography scatters logic but decouples; orchestration centralizes visibility.
  • Model intermediate states explicitly and make every saga step idempotent.

Knowledge check

Q1Multiple choice

Why is 2PC generally avoided in microservices?

Q2Scenario

In an order saga, payment fails after inventory was reserved. What happens next?

Q3System design

Your order flow has 7 steps, conditional branches, and a manual fraud-review step. Choreography or orchestration?

Answer all questions to submit