Part 4 of 8 · 3 min read
Transactions
2PC, TC/C and saga.
Two-phase commit (2PC). A low-level solution inside the databases (Level 5).
- The coordinator (here, the wallet service) does its reads and writes on each database. Locks are held.
- It asks all databases to prepare.
- If all answer yes, it tells all to commit. If any answers no, it tells all to abort.
It needs special database support (such as the XA standard). The main problems: locks are held while waiting for the others, which hurts performance, and the coordinator is a single point of failure.
Try-confirm-cancel (TC/C). A high-level solution in the application, a form of compensating transaction.
- Try: each database does a small local transaction that reserves or changes something and commits it.
- Confirm: if all tries succeeded, finish the job.
- Cancel: if any failed, undo the ones that succeeded by a new, opposite transaction.
Example, moving $1 from A to C:
| Phase | Account A | Account C |
|---|---|---|
| Try | Deduct $1 (local transaction, committed) | Nothing (a no-operation) |
| Confirm | Nothing | Add $1 |
| Cancel | Add $1 back | Nothing |
The key difference from 2PC: in TC/C, each local transaction is complete (unlocked) when the second phase begins, and the second phase uses new operations to finish or reverse. In 2PC, the local transactions are still open and are committed or aborted.
Why the first step deducts. At the end of Try, $1 has left A and not yet reached C, so the total looks lower by $1 for a moment. That is allowed, because the application can see the middle state and knows a transfer is in progress. The order matters. The deduction goes first, so a failure never creates money. Adding to C first could let someone spend the money before the deduction is known to succeed.
Keeping progress. The service may restart in the middle, so the state of each phase is stored in a phase status table: the transaction id, the status of Try on each database (not sent, sent, response), the name of the second phase (confirm or cancel), and a flag for out-of-order execution.
Out-of-order messages. A Cancel may reach a database before the Try. The node must accept the Cancel, record a flag, and refuse the Try when it later arrives.
Saga. A chain of local transactions, each with a compensating one. They run in order. If one fails, the earlier ones are undone in reverse order using the compensations. n steps need n normal operations and n compensations.
- Choreography: services react to each other's events. No central brain, but each needs its own state machine and it becomes hard with many services.
- Orchestration: one coordinator tells each service what to do. Easier to manage when there are many steps.
| TC/C | Saga | |
|---|---|---|
| Order of steps | Can run in parallel | Strictly in order |
| Undo | Cancel step | Compensating transaction |
| Best for | Latency-sensitive, many services | Microservices, simple flows |
For a two-account transfer, either works.
A worked example (made up). Try on A fails because A's balance is too low. The coordinator goes to Cancel for both accounts. A's Cancel finds nothing to undo, and C's Cancel is a no-operation. The transfer ends with both balances unchanged.
Compensating transaction
A compensating transaction is a new transaction that reverses the effect of an earlier committed one.
Example: after deducting $1 from A, the compensation adds $1 back to A.
Check yourself: 1. What is the main cost of 2PC? 2. Why does Try deduct and not add? 3. What problem does the phase status table solve?