Two-Stage Processing 00:00:01 Three vaults must agree on one transfer The transaction in this lesson spans three databases. Two-phase commit first asks whether each participant can commit, then distributes one final decision. A prepared vote must survive a process restart. A client starts a transaction over three databases. Each database takes locks and writes to its write-ahead log. After every required yes vote, the coordinator durably records commit before announcing that decision. All send ACK. The coordinator may forget the transaction. The decision belongs to the coordinator's durable protocol state. A client's success answer is another message altogether and can still be lost after the distributed decision has become final. 00:00:47 One vault votes no Two vaults prepare and hold the resources needed to finish. The third finds a broken constraint. A single no prevents a commit, so the coordinator records and distributes the abort decision. The coordinator sends PREPARE to all. DB1 and DB3 lock and log. DB2 finds a broken rule. The decision is abort. DB1 and DB3 are told. Everyone has rolled back and released the locks. A prepared yes is a promise to follow the final decision. It does not mean a participant may announce success or expose a committed result on its own. 00:01:23 The coordinator goes silent at the worst time Every vault has voted yes, and the coordinator crashes just after recording commit but before broadcasting it. To a prepared vault the silence can look identical to a crash before that decision. Every database holds locks and waits. COMMIT is in the log. Nobody was told. All voted yes and none knows. Commit and abort are both possible. Any transaction that needs the locked rows waits as well. A timeout alone cannot safely resolve this uncertainty. Prepared participants can retain locks while waiting for authoritative recovery information, which can make unrelated business work wait as well. 00:02:01 Read the durable decision and send it again The coordinator restarts and reads its log. For the recorded commit, it tells every required participant to commit again. Repeated messages must be harmless both before and after a participant recovers. The coordinator starts again and reads its log. It sends COMMIT to everybody again. DB3 is back and commits. In this example the recovered coordinator has no recorded commit decision for the other transaction, so the configured recovery protocol can decide abort. For an undecided transaction the permitted recovery rule depends on the protocol and durable state. Never overwrite an already committed decision simply because one acknowledgment is missing. 00:02:42 Adding a faraway vault changes the price of agreement A participant on another continent adds network delay to a protocol that can hold locks while waiting. An unavailable required database can prevent new work from finishing across otherwise healthy locations. Fast enough. Both are in the same data centre. Payments runs on another continent. Every lock now lasts for that round trip. One far database is down, and no order can commit. Orders and stock stay together. Payments becomes a saga step with a refund. Use two-phase commit where atomicity across supported resource managers justifies its coordination cost. If the business can tolerate intermediate states, explicit local transactions and compensating actions offer another design.