Two-phase commit
Двухфазный коммит
A protocol that gives a transaction spanning multiple databases or other resources one outcome: commit everywhere or roll back everywhere. Participants first vote on whether they are ready, then a coordinator tells them the decision.
Also known as: 2PC
What two-phase commit solves
A local database transaction can commit or roll back a set of changes as a unit. That guarantee stops at the database boundary: if an application commits on one database and then crashes before committing on another, the operation can be left half done. Two-phase commit (2PC) coordinates the outcome of a single transaction across multiple resources. Each resource manages its local transaction, while a coordinator decides whether the whole transaction commits or aborts.
How the protocol works
- Prepare. The coordinator asks each participant whether it can commit its part. A participant votes
YESonly after recording enough information in durable storage to honor the final decision even if it crashes and restarts. It has not committed yet and may still hold locks. A participant that cannot prepare votesNO. - Decide and finish. The coordinator chooses
COMMITonly after receiving aYESfrom every participant. ANOor a timeout while collecting votes can lead toABORT. In classic 2PC, the coordinator records its decision durably before sending it out. Participants then commit or roll back locally. If a message is lost, the coordinator can resend the same decision; it does not take a new vote.
Example: updating inventory across shards
Suppose a warehouse system moves ten units from a count on shard A to a count on shard B. A stages the decrement, and B stages the increment. If A cannot prepare because there are only six units left, the coordinator aborts both parts. Now suppose both shards prepare and the coordinator records COMMIT, but only A receives the message before the coordinator crashes. A can commit. B stays prepared until it learns the outcome. When the coordinator recovers with its log, it can resend COMMIT to B.
Where it fits and what it costs
Databases can use 2PC for cross-shard transactions, and transaction managers can use it with resources that support a protocol such as XA. The extra network round trips and durable writes add latency. More importantly, classic 2PC can block: a participant that has voted YES cannot safely roll back just because the coordinator goes silent. Another participant might already have committed. While waiting for the decision, the prepared participant may hold locks that delay unrelated work.
Atomic commit is not the same as simultaneous visibility: 2PC agrees on the outcome, but does not by itself provide global transaction isolation. A saga makes a different trade-off. Its steps commit independently, and later failures may require compensating actions rather than rolling back one shared transaction.
How it comes up in interviews
A common follow-up is: “Can we fix blocking by adding a timeout?” Distinguish the coordinator waiting for votes from a participant that has already voted YES. The coordinator can abort if it times out before deciding to commit. The prepared participant can use a timeout to trigger a status check, but not to choose an outcome on its own.