Full answer
Two-phase commit failure timeline
Participants: flight service, hotel service, payment service. Coordinator: booking service.
- Coordinator sends PREPARE to all three.
- Each participant writes its changes and a "prepared" record to its log, keeps the rows locked and votes YES.
- 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:
| Step | Action | Compensation |
|---|---|---|
| 1 | Hold flight seat (expires in 15 min) | Release hold |
| 2 | Reserve hotel room | Cancel reservation |
| 3 | Charge payment | Refund |
| 4 | Confirm flight ticket (issue) | None: pivot step |
| 5 | Send confirmation | Retry 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
PENDINGand 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