Module 16 / Consistency across services

Module 16 · lesson 2 of 8 · Read 8 min

Two-phase commit

Everyone agrees or nobody commits, and why it blocks.

If you want all-or-nothing across several databases, the textbook answer is two-phase commit, or 2PC. It's been around since the 1970s, it's inside many databases you already use, and it really does give you atomicity across machines.

It also has one flaw that makes most teams avoid it between services: at the wrong moment, a single crash can leave every participant stuck, holding locks, unable to decide. This lesson explains how it works, exactly where it blocks, and where it's still the right tool.

Like a wedding

The classic analogy is a wedding. The officiant asks each person "do you?". Only if both say yes does the officiant declare them married. In this ceremony's rules, a yes is a promise: whoever says it first can't take it back while the other is still answering. And once the officiant has declared it, it's done.

2PC works the same way, with a coordinator (the officiant) and several participants (the databases or services doing the work).

Two-phase commit is a protocol where a coordinator first asks every participant to promise it can commit, and only commits everywhere if all of them promise.