...

/

Working of the Two-Phase Commit Protocol

Working of the Two-Phase Commit Protocol

Learn how the two-phase commit protocol provides atomicity.

Two-phase commit (2PC) has two phases—prepare and commit. Intuitively, the prepare phase asks everyone: can you do X? If everyone says yes, then the commit phase asks them to do X. If anyone says no, X can not be done at that moment. In many databases, the required data is shipped to specific participants before starting a 2PC transaction. A unique identifier is provided for such data, which is later used in 2PC phases. Some participants start acquiring required locks in preparation for an impending 2PC, while others acquire locks in the preparation phase. Both of these strategies have their pros and cons. Latency can be reduced by acquiring locks early, though if a coordinator fails after sending data but before starting a 2PC transaction, data items will be unnecessarily locked, potentially reducing the availability of that data.

The prepare phase

The two-phase commit (2PC) process begins with the coordinator informing cohorts of the new transaction by sending a prepare message. Cohorts evaluate whether they can execute the portion of the transaction that pertains to them and make a decision to commit or abort. If a cohort determines that it can execute, it reports to the coordinator of its affirmative vote. If not, it informs the coordinator, who then aborts the ...