The goal
When a payment touches two stores, such as a wallet and a ledger, two phase commit makes the change atomic: both commit or neither does.
The two phases
- Prepare: a coordinator asks each participant to do the work and lock resources, then vote yes or no.
- Commit: if all voted yes, the coordinator tells everyone to commit; if any voted no, it tells everyone to abort.
The cost
- Participants hold locks between prepare and commit, hurting throughput.
- If the coordinator crashes after prepare, participants are blocked waiting for the decision.
When to use it
- Use it when both stores share an environment and strong consistency matters more than availability.
- For cross service payments that span networks, prefer a saga instead.
Key idea
Two phase commit gives atomic payments across stores through a vote then commit protocol, at the cost of locks and blocking if the coordinator fails.