advanced
Distributed transactions
Understand why two-phase commit is rare in service architectures and when stronger coordination is worth availability and coupling costs.
Two-phase commit (2PC) coordinates all participants before commit, providing strong consistency but coupling availability — if a coordinator or participant hangs, locks persist. Microservice architectures rarely use 2PC across services; databases may use it internally.
Prefer sagas, outbox, and idempotent steps for cross-service workflows. Reserve distributed transactions for rare cases where regulatory or inventory correctness demands it and you accept operational cost.
On interviews: explain CAP trade-offs; why XA across microservices is uncommon; when local transactions plus events suffice.
Common pitfalls: distributed transactions as default; long-held locks over HTTP; ignoring partial failure on prepare phase.
Checklist:
- Default to saga or outbox patterns.
- Keep transactions local to one service.
- Measure lock duration and failure modes.
- Use 2PC only with explicit justification.