Distributed Systems Primitives • Distributed Transactions (2PC, Saga)Medium⏱️ ~3 min
Two-Phase Commit (2PC): Strong Atomicity with Blocking Trade-offs
Two-Phase Commit (2PC) is a distributed consensus protocol that achieves atomic commitment across multiple independent databases or services by using a coordinator to orchestrate two rounds of communication with all participants. In the prepare phase, the coordinator asks each participant to durably vote on whether they can commit; participants must write their vote to stable storage (typically requiring 0.5 to 5 milliseconds on SSDs) and acquire all necessary locks. If all participants vote yes, the coordinator enters the commit phase and instructs everyone to finalize; otherwise, it instructs an abort. This protocol provides full ACID atomicity across distributed resources, making it suitable for scenarios where invariants absolutely cannot be violated even temporarily, such as moving money between ledger accounts.
The major downside of 2PC is that it is a blocking protocol. If the coordinator crashes after participants have prepared but before issuing the commit decision, participants remain in an "in-doubt" state, holding locks and waiting for recovery. This can cascade into severe availability degradation. Latency is determined by the slowest participant: with network round-trips and disk flushes, single region commits typically take tens of milliseconds, but cross-region scenarios (50 to 150 milliseconds round-trip time) can push commit latency into the 100+ millisecond range. Throughput suffers because distributed locks serialize conflicting transactions, and the system's overall availability approximates the product of all participants' availability (two services at 99.5% availability each yield roughly 99.0% end to end availability).
Google Spanner demonstrates that 2PC can work at planetary scale when combined with Paxos replicated participants and TrueTime for tightly bounded clock uncertainty. Spanner commits within tens of milliseconds in a single region and adds inter-region round-trip time plus a commit wait equal to time uncertainty (a few milliseconds) for multi-region transactions. Amazon DynamoDB Transactions provide regional ACID writes across dozens of items within a region, adding single-digit to tens of milliseconds per transaction under normal load, but AWS explicitly does not offer cross-region transactional atomicity, highlighting the practical limits of 2PC availability.
💡 Key Takeaways
•Commit latency equals approximately two network round-trips plus disk flush time at the slowest participant; cross-region deployments typically see 100+ milliseconds end to end latency due to 50 to 150 milliseconds inter-region round-trip times.
•Blocking failure mode: coordinator crash after prepare leaves participants in-doubt, holding locks indefinitely until recovery or timeout policies fire, causing cascading latency and reduced availability.
•Overall availability is the product of participant availabilities; two services at 99.5% each yield roughly 99.0% combined, making 2PC unsuitable for systems requiring five nines availability.
•Google Spanner uses 2PC with Paxos replication and TrueTime to achieve global consistency with typical single-region commits in tens of milliseconds, demonstrating viability when combined with quorum replication and synchronized clocks.
•Tail latency amplification: with N participants, the 99th percentile commit time is bounded by the slowest participant's disk flush and network delays, often reaching hundreds of milliseconds across zones or regions.
•Best suited for few participants, short lived operations in single-region or low-latency environments where invariants cannot be violated even temporarily, such as financial ledger transfers.
📌 Examples
Google Spanner executing a global 2PC: within a single region, commits take tens of milliseconds; multi-region commits add 50 to 150 milliseconds inter-region round-trip time plus a few milliseconds commit wait for TrueTime uncertainty, totaling 100+ milliseconds for cross-continental transactions.
Amazon DynamoDB Transactions: regional ACID writes across up to dozens of items, adding single-digit to tens of milliseconds under normal load, but rising significantly under hot partition contention; no cross-region transactional atomicity is provided.
Bank ledger transfer: moving $100 from account A (database X) to account B (database Y) using 2PC ensures either both debits and credits succeed or neither does, preventing money creation or loss even if network or process failures occur mid-transaction.