2PC protocol

2PC is the most commonly used consensus algorithm. In this scenario, the global transaction manager contacts every participant, suggests a possible value, collects a response from every participant, and, if everyone agrees, decides on a commit; otherwise, it communicates to abort about the consensus and  performs a rollback on the transaction.

The procedure consists of two phases, as follows:

  • The prepare phase: All participants send their proposals to the coordinator, stating whether they are able to proceed and commit their single operation, or whether the transaction should be aborted. This phase is implemented by the prepare() method of the javax.transaction.xa.XAResource interface.
  • Commit or abort phase: All participants communicate the result of the vote, and tell the coordinator to either go ahead and decide to commit, or to abort the transaction. The coordinator specifies the behavior of the resources. The resources notify the coordinator when they finished to do this and when the transaction has finished. This is implemented by the commit and rollback methods of the javax.transaction.xa.XAResource interface.

The 2PC consensus protocol has the ability to manage node failures through the transaction log. Usually, a periodic recovery thread processes all of the unfinished transactions.

One of the main disadvantages of the 2PC protocol is that it is a lock-based protocol. If the coordinator fails after the first phase has completed, all of the participants will be blocked waiting for the coordinator's decision, and will not be able to progress until it recovers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset