Sub topic 16
Distributed Transactions and Two-Phase Commit
This chapter assumes you know local SQL transactions, COMMIT, ROLLBACK, constraints, and isolation levels. A distributed transaction is one logical operation that updates data managed by more than one independent system. For example, an order service might write an order to one database and reserve inventory in another.
A local transaction can ask one database to commit or roll back. With two databases, neither database automatically knows whether the other succeeded. The application needs a protocol that coordinates their decisions.
Why a distributed transaction is difficult
Suppose an order creates a row in the orders database and decreases stock in the inventory database. If the order commits but the inventory update fails, the systems disagree. Retrying only one statement may create duplicate orders or incorrect stock.
Two-phase commit, or 2PC, makes all participating systems reach one shared decision:
- Commit everywhere.
- Roll back everywhere.
The systems involved are:
- The coordinator, which controls the protocol.
- Participants, also called resource managers, which run local transactions.
The two phases
Phase one: prepare
The coordinator sends the updates to every participant and asks each one to prepare. A participant then:
- Checks constraints and other local rules.
- Writes enough recovery information to durable storage.
- Holds locks or other resources needed to commit.
- Replies
yesif it can commit, ornoif it cannot.
A yes response is a promise. The participant must be able to commit later, even if the coordinator temporarily disappears.
Phase two: commit or rollback
If every participant replies yes, the coordinator records a global commit decision and sends COMMIT to all participants. If any participant replies no, or does not respond before the policy timeout, the coordinator sends ROLLBACK.
| Situation | Coordinator decision | Participant action |
|---|---|---|
| Every participant prepared successfully | Commit | Make local work permanent |
| At least one participant rejected the work | Roll back | Discard local work |
| Coordinator fails after prepare | Unknown until recovery | Keep the prepared transaction |
Failure modes and cost
The difficult case occurs after a participant has prepared but before it receives the final decision. It cannot safely commit or roll back on its own. It may hold locks while waiting for the coordinator to recover. This blocking behavior can reduce throughput and cause long waits.
2PC also adds network round trips, durable logging, coordination code, and operational complexity. A participant can be healthy while another service is unavailable, yet the whole logical operation remains incomplete.
Alternatives and design choices
Use 2PC when strict atomicity across systems is required and all systems support a compatible transaction protocol, such as XA. The cost may be acceptable for a small number of tightly controlled participants.
For many service architectures, an eventual-consistency design is easier to operate. One service commits its local transaction and publishes an event using an outbox table. Other services consume the event and repair or compensate for failures. A compensation is a deliberate action that reverses an earlier business effect, such as issuing a refund.
Choose 2PC for one indivisible business decision. Choose local transactions plus events when temporary disagreement is acceptable and each service must remain independently available.
Exercise
Why can a participant not immediately roll back after replying “yes” to the prepare phase?
Show answer
The coordinator may already have chosen commit. The participant must retain its prepared state until it learns the final decision, or recovery determines it.