What is a two-phase commit (2PC) and in what scenario would you need it?
Last updated
Two-phase commit (2PC) keeps one transaction atomic across multiple independent resources: a coordinator asks all participants to prepare and vote, then sends commit only if all vote yes, otherwise rollback.
Use it when a single logical transaction spans several databases or a database plus a message broker (e.g. XA transactions).
Downsides: it blocks (participants hold locks if the coordinator crashes after prepare) and adds latency. Modern systems often prefer sagas or eventual consistency.
- https://dev.to/ovichowdhury/demystifying-two-phase-commit-2pc-for-distributed-transaction-in-microservices-5ca7
- https://medium.com/@bhagidega9848/what-is-2-phase-commit-2pc-f988b1527d08
- https://shivam-srivastav.medium.com/understanding-two-phase-commit-2pc-ensuring-atomic-transactions-in-distributed-systems-f372e30ea665
- https://milon.im/post/two-phase-commit/
- https://docs.pgdog.dev/features/sharding/2pc/
- https://www.postgresql.org/docs/current/sql-prepare-transaction.html
- https://www.postgresql.org/docs/current/two-phase.html
- https://dev.mysql.com/doc/refman/8.1/en/xa.html