Inside one database a transaction is atomic by design. The moment data lives in two databases or two services, there is no single commit that covers both, and the network can fail between any two steps. Two-phase commit, 2PC, is the classic protocol for restoring atomicity across participants. Knowing it, and its costs, explains why other patterns exist.
The two phases
A coordinator drives several participants, each of which has a local transaction already doing its part of the work.
- Prepare. The coordinator asks every participant "can you commit?". A participant that votes yes writes everything needed to commit into durable storage and promises not to change its mind. A participant that cannot commit votes no.
- Commit or abort. If every vote is yes, the coordinator durably records the decision to commit and tells everyone to commit. If any vote is no, or a participant is unreachable, it tells everyone to abort.
The key property is that after voting yes a participant is in doubt: it can neither commit nor roll back on its own. It must wait for the coordinator's decision.
XA in MySQL
MySQL implements the participant side of the standard XA protocol. An external transaction manager issues these statements to each database.
XA START 'order-9001';
UPDATE wallets SET balance = balance - 100000 WHERE id = 1 AND balance >= 100000;
XA END 'order-9001';
XA PREPARE 'order-9001';
-- the coordinator now decides
XA COMMIT 'order-9001'; -- or XA ROLLBACK 'order-9001';
XA RECOVER; -- lists prepared transactions still in doubtJava application servers and some libraries ship transaction managers that speak XA. Most web stacks and microservice frameworks do not.
Why it hurts
- It blocks. A prepared transaction holds its locks until the decision arrives. If the coordinator crashes after prepare, participants keep those locks, other transactions queue behind them and part of the system stalls until the coordinator recovers or a human resolves the in-doubt transactions.
- It is slow. Two network round trips and several forced disk writes per transaction, with latency set by the slowest participant.
- It lowers availability. Every participant and the coordinator must be up for a commit to succeed, so the probability of failure multiplies.
- It assumes participants that support it. A REST call to a payment provider, a Kafka topic or a third-party API cannot prepare and wait. The protocol does not fit HTTP services or message brokers.
- It couples services tightly, which is at odds with the independent deployment that motivates microservices.
When 2PC is still reasonable
2PC suits a small number of homogeneous, trusted databases in the same network, with a mature transaction manager and low contention, where strong consistency is worth the cost. Many databases use a form of it internally, for example MySQL coordinates the redo log and the binary log. The difficulty is mostly with spanning independent services.
What to do instead
- Keep data that must change atomically in one database. Drawing service boundaries so one service owns the money tables avoids the problem entirely.
- Use a saga: a chain of local transactions with compensating actions, covered in the next article.
- Use the transactional outbox to publish events reliably alongside a local change.
- Add reconciliation to find and fix the inevitable gaps.
Takeaway
2PC delivers atomic commit across databases by making participants wait in doubt for a coordinator, which causes blocking, latency and coupling. Prefer one owner per piece of data, then sagas and outbox for cross-service flows, and keep XA for the narrow cases where its guarantees justify its cost.
Comments