Two-phase commit (2PC)
Understand how two-phase commit achieves atomicity across multiple databases, why the coordinator is a blocking single point of failure, and what alternatives engineers reach for instead.
The problem
Your e-commerce checkout touches three services: the payment service (charge the card), the inventory service (reserve the items), and the orders service (create the order record). You fire all three calls. The payment service succeeds. The inventory service times out. The orders service never receives a request because your coordinator stopped at the timeout.
The customer's card is charged. No inventory is reserved. No order exists in the system. The customer calls support. You have a data inconsistency that requires a human to resolve.
This happens because each service commits independently. There is no mechanism to say "all three must succeed or all three must roll back." Distributed atomicity is the problem two-phase commit solves.
What it is
Two-phase commit (2PC) is a distributed consensus protocol that ensures all participants in a distributed transaction either all commit their changes or all roll them back. A single coordinating process orchestrates two phases: the Prepare phase ("can you commit?") and the Commit phase ("please commit now"). Named after the two round trips between the coordinator and the participants.
Analogy: Think of a wedding ceremony. The officiant (coordinator) asks each party "do you take this person as your spouse?" before anyone says "I do." Both parties must answer yes while the ceremony is paused. Only when both agree does the officiant declare them married. If either says no or stays silent, the ceremony stops and nothing changes. No vows are exchanged until both parties commit.
Two round trips, one coordinator
2PC adds exactly two extra network round trips to every distributed write: one for PREPARE (collecting votes) and one for COMMIT/ABORT (broadcasting the decision). For a three-participant transaction over a 5ms LAN, that is roughly 10ms of added coordination overhead before any participant can release its locks.