Distributed Transactions
5 exercises — master distributed transaction vocabulary: 2PC vs Saga trade-offs, compensating transactions, the Outbox pattern for reliable event publishing, lost updates, and idempotency key implementation.
0 / 5 completed
Distributed transactions quick reference
- 2PC — prepare + commit phases; ACID atomicity; blocking on coordinator failure. Use for co-located, short-lived XA resources.
- Saga — sequence of local transactions + compensating transactions. Use for cross-service, long-lived flows.
- Compensating transaction — application-level semantic undo for an already-committed Saga step.
- Outbox pattern — write event to outbox table in same DB transaction; relay publishes to message broker. Solves dual-write.
- Lost update — concurrent read-modify-write overwrites each other. Fix: optimistic locking (version), SELECT FOR UPDATE, or atomic DB operation.
- Idempotency key — caller-generated UUID; service stores key → result; retries return cached result without re-executing.
1 / 5
An architect explains: "We use Two-Phase Commit (2PC) to coordinate our order and inventory updates. A colleague says '2PC is blocking — you should use the Saga pattern instead.' When is 2PC actually appropriate?"
2PC = synchronous, blocking, ACID atomicity for co-located resources. Saga = asynchronous, eventually consistent, with explicit compensation.
Two-Phase Commit protocol:
• If the coordinator crashes after Phase 1 but before Phase 2, participants are stuck holding locks
• They cannot proceed without hearing the coordinator's Phase 2 decision
• Resolution requires coordinator recovery (from stable storage) or manual intervention
• This is the "blocking" nature of 2PC — participants block waiting for the coordinator
When 2PC is appropriate:
• Same data centre: microsecond to millisecond network latency
• Under your operational control (you can recover the coordinator)
• XA-capable resources (databases, message brokers supporting XA: PostgreSQL, MySQL, Oracle, ActiveMQ)
• Short-lived transactions (seconds, not minutes)
When 2PC is problematic:
• Cross-datacenter (high latency + unreliable network = long blocking windows)
• Third-party systems (you can't guarantee they implement XA correctly or recover predictably)
• Long-lived operations (multi-step workflows that take seconds to minutes)
• High-throughput systems (lock holding during 2PC reduces concurrency)
Key vocabulary:
• Two-Phase Commit (2PC) — distributed transaction protocol ensuring atomicity across multiple participants via prepare and commit phases
• XA — open standard for distributed transaction management; implemented by databases and message brokers
• Coordinator — the node that drives the 2PC protocol and makes the final commit/abort decision
• Blocking — 2PC characteristic where participants hold locks indefinitely if the coordinator fails between phases
Two-Phase Commit protocol:
Phase 1: Prepare Coordinator → Participant A: "Can you commit?" Coordinator → Participant B: "Can you commit?" A → Coordinator: "Yes (VOTE_COMMIT)" or "No (VOTE_ABORT)" B → Coordinator: "Yes" or "No" Phase 2: Commit (if all voted YES) / Abort (if any voted NO) Coordinator → A: COMMIT (or ABORT) Coordinator → B: COMMIT (or ABORT) A, B: execute and release locks2PC blocking problem:
• If the coordinator crashes after Phase 1 but before Phase 2, participants are stuck holding locks
• They cannot proceed without hearing the coordinator's Phase 2 decision
• Resolution requires coordinator recovery (from stable storage) or manual intervention
• This is the "blocking" nature of 2PC — participants block waiting for the coordinator
When 2PC is appropriate:
• Same data centre: microsecond to millisecond network latency
• Under your operational control (you can recover the coordinator)
• XA-capable resources (databases, message brokers supporting XA: PostgreSQL, MySQL, Oracle, ActiveMQ)
• Short-lived transactions (seconds, not minutes)
When 2PC is problematic:
• Cross-datacenter (high latency + unreliable network = long blocking windows)
• Third-party systems (you can't guarantee they implement XA correctly or recover predictably)
• Long-lived operations (multi-step workflows that take seconds to minutes)
• High-throughput systems (lock holding during 2PC reduces concurrency)
Key vocabulary:
• Two-Phase Commit (2PC) — distributed transaction protocol ensuring atomicity across multiple participants via prepare and commit phases
• XA — open standard for distributed transaction management; implemented by databases and message brokers
• Coordinator — the node that drives the 2PC protocol and makes the final commit/abort decision
• Blocking — 2PC characteristic where participants hold locks indefinitely if the coordinator fails between phases