← Back to System Design, page 5

system-design-0086 · Medium mid

Walk through what happens when a two-phase commit coordinator crashes after the prepare phase, and then design a saga with compensations for a trip booking that reserves a flight, a hotel and a payment.

In two-phase commit, a participant that voted "yes" in prepare must hold its locks until it learns the outcome; if the coordinator crashes then, participants are blocked and cannot decide alone. A saga avoids that by running local transactions in sequence with a compensating action for each, such as cancel the flight if the hotel fails. Sagas trade atomic isolation for availability, so steps must be idempotent, compensations must be designed per step, and irreversible steps go last.

Full answer

Two-phase commit failure timeline

Participants: flight service, hotel service, payment service. Coordinator: booking service.

  1. Coordinator sends PREPARE to all three.
  2. Each participant writes its changes and a "prepared" record to its log, keeps the rows locked and votes YES.
  3. Coordinator crashes before sending COMMIT or ABORT.

Now each participant is in doubt. It cannot commit (another participant might have voted NO) and cannot abort (the coordinator might have decided COMMIT and told someone). It must keep locks and wait for the coordinator to recover and read its decision log. Other transactions that need those rows wait too. This is the blocking problem of 2PC. Making the coordinator highly available (replicating its log with consensus) reduces but does not remove the cost, and every participant must support the prepare protocol, which most external services (payment gateways, airline systems) do not.

Saga design for the trip

Orchestrated saga, run by a booking orchestrator that stores its state durably:

StepActionCompensation
1Hold flight seat (expires in 15 min)Release hold
2Reserve hotel roomCancel reservation
3Charge paymentRefund
4Confirm flight ticket (issue)None: pivot step
5Send confirmationRetry until success

Rules:

  • Order steps so that easy-to-undo actions come first, the pivot (point of no return, here issuing the ticket) after them, and only retryable steps after the pivot, which must eventually succeed.
  • Idempotency. Each step and compensation carries the saga ID and step number, so retries after timeouts do not double-book or double-refund.
  • Compensation is semantic. A refund is a new transaction, not an erasure; the customer may see a charge and a refund.
  • Isolation is lost. Between steps, others can see partial state (a room reserved but not yet paid). Use status fields such as PENDING and countermeasures like re-reading values before the pivot.

If payment fails at step 3, the orchestrator runs compensations in reverse: cancel the hotel, release the flight hold, then mark the saga FAILED.

Choosing

Use 2PC (or a single database transaction) inside one database or a tightly controlled set of resources that support it. Use sagas across services and external partners, where availability matters and blocking locks are unacceptable.

By level

Fresher: Explain that in 2PC everyone must agree before committing and can get stuck if the coordinator disappears. A fresher should describe undoing earlier steps when a later one fails.

Mid: Walk through the in-doubt timeline, design the saga table with compensations and idempotency, and explain why external services prevent 2PC. Mention orchestration.

Senior: Discuss pivot steps, semantic compensation, lost isolation and countermeasures, coordinator replication and choreography versus orchestration. Expect a careful failure timeline.

Key points

  • After voting yes, a 2PC participant is in doubt and must hold locks until the coordinator recovers
  • External services rarely support prepare
  • Sagas run local steps with compensations in reverse on failure
  • Put reversible steps first, the pivot next, retryable steps last
  • Steps and compensations must be idempotent; isolation is lost

Common mistakes

  • Assuming participants can safely abort on their own after preparing
  • Designing a saga without a compensation for each step before the pivot
  • Treating compensation as deleting history
  • Forgetting idempotency for retries and compensations

Follow-ups

  • How would you implement the same saga with choreography instead of an orchestrator?
  • What is the three-phase commit and why is it rarely used?
  • How do you show users a booking that is still pending?

Tips

  • Narrate the in-doubt state clearly
  • Use a table of steps and compensations
  • Name the pivot step

Evaluation rubric

  • Correct 2PC blocking analysis
  • Sound saga with compensations and ordering
  • Idempotency and isolation addressed
  • Clear choice between the two