Two-Phase Commit (2PC)

Salem Alqahtani
2 min readMay 28, 2022

A two-phase commit (2PC) is a standardized protocol used in a distributed database for ensuring atomic commitment where all involved nodes are committed or aborted the transactions from their logs. In database management, saving data changes is known as a commit, and undoing changes are known as a rollback.

2PC breaks its commit operation into two phases: prepare and commit. In the below figure, I illustrate the 2PC algorithm.

Two-Phase Commit(2PC)

The algorithm details is illustrated in the figure below.

2PC Algorithm

What will happen if the coordinator crash?

Suppose the decisions are received from replicas before the coordinator is crashed. In that case, the coordinator writes the values to the disk and reads the values from the disk to recover, and takes a decision after recovery.

If the coordinator crashed before hearing back from replicas, the entire above algorithm is blocked until the coordinator is recovered. To overcome this issue, we need something called fault-tolerant two-phase commit. The algorithm is easy to follow in the below figures.

The 2PC is a blocking protocol. The failure of a single node blocks the progress of the protocol until the node recovers. Moreover, the coordinator is a single point of failure where the database will be in an inconsistent state until the coordinator recover. Not only that, 2PC can lead to latency depending on the slowest node.

--

--