Skip to main content
PreparationZone Logo
PrepZone

Two-Phase Commit and Its Limits

Atomic commit across nodes — why ShardPay avoids 2PC for cross-shard transfers.

Pass checkpoint below

Why this matters

  • Enterprise interviews still probe XA transactions and JDBC commit() across datasources.
  • ShardPay experimented with XA for cross-shard transfers in 2023 and rolled it back after a coordinator outage left 400 accounts locked for 11 minutes.
  • Understanding 2PC limits explains why sagas, outbox patterns, and eventual consistency replaced distributed transactions in payment systems.
  • Every senior design review should answer: "Why not 2PC here?" with concrete failure modes, not hand-waving.
Coordinator
Shard ALock + vote
Shard BLock + vote
All release locks
Prepare locks resources; commit makes changes durable — coordinator failure blocks participants.

How 2PC works

A coordinator runs two phases. In phase 1 (prepare), each participant locks resources and votes yes or no. In phase 2 (commit or abort), the coordinator broadcasts the decision; participants apply it and release locks.

ShardPay's abandoned XA flow looked like this: the transfer API opened an XA transaction, prepared a debit on shard A and a credit on shard B, then committed both or aborted both. On paper, balances stayed consistent. In production, the prepare phase held row locks on both shards until commit completed — contending with normal merchant traffic.

Prepare phase in ShardPay's XA experiment

When merchant M-4421 initiated a $500 cross-shard transfer, shard A's participant locked the source row and wrote a prepare log entry. Shard B did the same for the destination. Neither balance changed yet, but both rows were unavailable to other transfers targeting those accounts. Under peak load, lock wait times on hot merchant accounts exceeded 800ms — unacceptable for ShardPay's 200ms transfer SLO.

Key points

  • Coordinator — the single process that collects votes and issues commit or abort. In ShardPay's XA experiment, the transfer service JVM acted as coordinator; when it GC-paused for 9 seconds, all prepared shards blocked.
  • Prepare vote — each participant locks resources and responds yes/no without making the change permanent. ShardPay shard participants returned yes only after verifying sufficient balance and acquiring row locks.
  • Commit log — participants write the decision durably before acknowledging. If a participant crashes after commit but before unlock, recovery replays the log — but only if the coordinator's decision is known.
  • In-doubt transaction — a participant voted yes but never received commit or abort. ShardPay's incident left 400 in-doubt transactions until the coordinator's recovery timeout fired at 11 minutes.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 2 sections

  • Why 2PC breaks in production
  • Alternatives ShardPay adopted

Test yourself

Answer these before moving on — recall is what makes it stick.