0 day streak
9. Distributed systemsDistributed transactions

Distributed transactions

A distributed transaction is a change that must succeed or fail as a whole even though it touches several separate databases or services, like charging a card in one system and booking a seat in another. The classic protocol is two-phase commit: a coordinator asks every participant to prepare and promise it can commit, and only if all say yes does it tell them to go ahead. The weak spot is that a participant who has promised must wait for the verdict, so if the coordinator dies at the wrong moment, participants sit holding locks. Many systems use sagas instead: a sequence of ordinary local steps, each paired with a compensating action that undoes it if a later step fails. Sagas don’t block, but other parts of the system can see the half-finished states in between.

In this lesson: 2PC where crashing the coordinator causes blocking. A saga with compensations. The outbox pattern.