Problem Statement
Compare 2PC and Sagas for multi-shard writes. When would you choose each?
Explanation
Two-phase commit gives atomicity across shards but adds a coordinator, locks, and higher latency. It is suitable when you need strict all-or-nothing semantics on small, infrequent transactions.
Sagas split the workflow into local transactions with compensations. They scale better and keep availability high, at the cost of transient inconsistency and more failure handling code. Use Sagas for high-throughput, user-facing operations where partial progress is acceptable with recovery.
Code Solution
SolutionRead Only
Reserve A → Reserve B → Confirm; on failure: Cancel B, Cancel A
