Two-phase commit

Two-phase commit is a method of executing distributed transactions. That is, a sequence of actions which either all succeed or fail entirely. Another approach is with sagas

The Two-Phase Commit protocol is a reliable method for ensuring the successful execution of transactions across all participants in a distributed system. This approach guarantees that actions are taken only when unanimous acknowledgment is received from all members within the distributed cluster.

Unsurprisingly, this protocol is carried out in two distinct phases: Prepare and Commit.

During the Preparation phase, the coordinator initiates a request for each individual node to prepare for the intended action. Each node then communicates its readiness to execute the transaction to the coordinator. If a node is incapable of executing the action, it signals an ABORT to the coordinator. Subsequently, the coordinator informs all other nodes to refrain from executing the action. Conversely, nodes responding with a READY signify their capability to proceed.

In the Commitment phase, the coordinator dispatches a COMMIT message to every cluster member. Upon receiving this COMMIT message, each node executes the transaction and dispatches an acknowledgment. Should any participant encounter an issue, it communicates an ABORT message to the coordinator. The coordinator then communicates this ABORT directive to the remaining clients, prompting each to rollback the preceding transaction. Conversely, if all nodes execute the action successfully, the coordinator transmits a COMMIT message to each member, officially marking the transaction as complete.