When would you choose a saga over two-phase commit, and when the reverse?
Sketch your own answer first, then compare it with the one below.
A strong answer
Choose a saga when the steps are in separate services or organizations, take long, or call things that cannot join a transaction, such as a payment provider. A saga holds no locks across steps, so one slow step does not block others, and no service waits for a coordinator.
Choose two-phase commit when the participants are databases under one team, the transaction is short, you need real isolation, and compensation would be awkward. A money transfer between two shards of one store is an example.
The trade-off is isolation against availability. Two-phase commit gives strong isolation and blocks. A saga stays available and makes the application deal with the anomalies.
Sources
- Pat Helland (2007). Life beyond Distributed Transactions: an Apostate's Opinion.
- Hector Garcia-Molina, Kenneth Salem (1987). Sagas.
- Philip A. Bernstein, Vassos Hadzilacos, Nathan Goodman (1987). Concurrency Control and Recovery in Database Systems.