Two phase commit protocol

Two phase commit protocol

In order to ensure that a transaction can be rolled back, a software system typically logs each operation, including the commit operation itself. A transaction/recovery manager uses the log records to undo a partially completed transaction. When a transaction involves multiple distributed resources, for example, a database server on each of two different hosts, the commit process is somewhat complex because the transaction includes operations that span two distinct software systems, each with its own resource manager, log records, and so on. Two-phase commit is a transaction protocol designed for the complications that arise with distributed resource managers. With a two-phase commit protocol, the distributed transaction manager employs a coordinator to manage the individual resource managers. The commit process proceeds as follows: Phase 1 Each participating resource manager coordinates local operations and forces all log records out:

  • If successful, respond “OK”
  • If unsuccessful, either allow a time-out or respond “OOPS”

Phase 2 If all participants respond “OK”:

  • Coordinator instructs participating resource managers to “COMMIT”

Participants complete operation writing the log record for the commit Otherwise:

  • Coordinator instructs participating resource managers to “ROLLBACK”
  • Participants complete their respective local undos

In order for the scheme to work reliably, both the coordinator and the participating resource managers independently must be able to guarantee proper completion, including any necessary restart/redo operations. The algorithms for guaranteeing success by handling failures at any stage are provided in advanced database texts. The 2-phase commit (2PC) protocol is a distributed algorithm to ensure the consistent termination of a transaction in a distributed environment. Thus, via 2PC an unanimous decision is reached and enforced among multiple participating servers whether to commit or abort a given transaction, thereby guaranteeing atomicity. The protocol proceeds in two phases, namely the prepare (or voting) and the commit (or decision) phase, which explains the protocol’s name. The protocol is executed by a coordinator process, while the participating servers are called participants. When the transaction’s initiator issues a request to commit the transaction, the coordinator starts the first phase of the 2PC protocol by querying—via prepare messages—all participants whether to abort or to commit the transaction. If all participants vote to commit then in the second phase the coordinator informs all participants to commit their share of the transaction by sending a commit message. Otherwise, the coordinator instructs all participants to abort their share of the transaction by sending an abort message. Appropriate log entries are written by coordinator as well as participants to enable restart procedures in case of failures.

 

Leave a Reply

Your email address will not be published. Required fields are marked *