Two-phase commit protocol

The two-phase commit protocol (2PC) is a type of atomic commitment protocol (ACP).

It is a distributed algorithm that coordinates all the processes that participate in a distributed atomic transaction on whether to commit or abort (roll back) the transaction

Recovery:

2PC is not resilient to all possible failure configurations, and in rare cases, manual intervention is needed to remedy an outcome.

To accommodate recovery from failure (automatic in most cases) the protocol's participants use logging of the protocol's states. Log records, which are typically slow to generate but survive failures, are used by the protocol's recovery procedures. Many protocol variants exist that primarily differ in logging strategies and recovery mechanisms.

The protocol consists of two phases:

The commit-request phase (or voting phase), in which a coordinator process attempts to prepare all the transaction's participating processes (named participants, cohorts, or workers) to take the necessary steps for either committing or aborting the transaction and to vote, either "Yes": commit (if the transaction participant's local portion execution has ended properly), or "No": abort (if a problem has been detected with the local portion)

  • The coordinator sends a query to commit message to all participants and waits until it has received a reply from all participants.
  • The participants execute the transaction up to the point where they will be asked to commit. They each write an entry to their undo log and an entry to their redo log.
  • Each participant replies with an agreement message (participant votes Yes to commit), if the participant's actions succeeded, or an abort message (participant votes No, not to commit), if the participant experiences a failure that will make it impossible to commit.


The commit phase, in which, based on voting of the participants, the coordinator decides whether to commit (only if all have voted "Yes") or abort the transaction (otherwise), and notifies the result to all the participants. The participants then follow with the needed actions (commit or abort) with their local transnational resources (also called recoverable resources; e.g., database data) and their respective portions in the transaction's other output (if applicable).

  • Success

If the coordinator received an agreement message from all participants during the commit-request phase:

  1. The coordinator sends a commit message to all the participants.
  2. Each participant completes the operation, and releases all the locks and resources held during the transaction.
  3. Each participant sends an acknowledgment to the coordinator.
  4. The coordinator completes the transaction when all acknowledgments have been received.


  • Failure

If any participant votes No during the commit-request phase (or the coordinator's timeout expires):

  1. The coordinator sends a rollback message to all the participants.
  2. Each participant undoes the transaction using the undo log, and releases the resources and locks held during the transaction.
  3. Each participant sends an acknowledgement to the coordinator.
  4. The coordinator undoes the transaction when all acknowledgements have been received.

Disadvantage:

The greatest disadvantage of the two-phase commit protocol is that it is a blocking protocol. If the coordinator fails permanently, some participants will never resolve their transactions: After a participant has sent an agreement message to the coordinator, it will block until a commit or rollback is received.