Introduction

Imagine sending 100 from Alice to Bob.

If both accounts are in one database, this is easy: start a transaction, update both balances, and commit. But this example puts Alice in MySQL and Bob in PostgreSQL. One database cannot finish—or undo—the other database's work.

That is the problem two-phase commit, usually called 2PC, tries to solve.

This article follows a small Go program that performs the transfer and then crashes on purpose. The two crashes happen almost at the same moment, but recovery gives them opposite answers. That small difference is the easiest way to understand 2PC.