Click here to Skip to main content
15,917,565 members
Articles / All Topics

Two-Phase Commit Protocol

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
6 Jan 2011CPOL2 min read 30.9K  
Two-Phase Commit Protocol

Overview

The two phase commit protocol is a distributed algorithm which lets all sites in a distributed system agree to commit or rollback a transaction based upon consensus of all participating sites. The two phase commit strategy is designed to ensure that either all the databases are updated or none of them, so that the databases remain synchronized. The protocol achieves its goal even in many cases of temporary system.

Assumptions

In two phase commit protocol, there is one node which acts as a coordinator and all other participating nodes are known as cohorts. The assumptions made in this protocol are listed below:

  1. Each cohort should have its own stable storage.
  2. To provide atomicity and durability, each participating node should maintain a log before performing an operation. This type of logging is called write-ahead logging.
  3. In case of system failure of any of the cohorts, the storage can be recovered and information from the log file can still be retrieved.
  4. No participating node can fail permanently.
  5. In case of permanent failure of any cohort or complete loss of storage, the data cannot be recovered.

Basic Algorithm

As the name suggests, the two phase commit protocol involves two phases. The first phase is “Commit Request”phase, and the second phase is “Commit” phase.

Commit Request Phase

  1. To commit the transaction, the coordinator sends a request asking for “ready for commit?” to each cohorts.

  2. The coordinator waits until it has received a reply from all cohorts to “vote” on the request.

  3. Each participant votes by sending a message back to the coordinator, as follows:
    • It votes YES if it is prepared to commit
    • It may vote NO for any reason, usually because it cannot prepare the transaction due to a local failure.
    • It may delay voting indefinitely, for example, because its system was busy with other work because it failed.

Commit Phase

    1. If the coordinator receives YES response from all cohorts, it decides to commit. The transaction is now officially committed. Otherwise, it either receives a NO response or gives up waiting for some participants, so it decides to abort.
    2. The coordinator sends its decision to all participants (i.e. COMMIT or ABORT).

    3. Participants acknowledge receipt of commit or abort by replying DONE.

Disadvantages

The two phase commit protocol is a blocking protocol; hence it has some disadvantages which are listed below:

  1. A cohort locks the required resources while it is waiting for a message from coordinator. Other processes competing for resource will have to wait for the locks to be released.
  2. A single node will continue to wait even if all other sites have failed.
  3. If the coordinator fails permanently, some cohorts will never resolve their transactions, causing resources to be locked up forever.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Société Générale
India India
Summary:

1. Extensive working experience in web and windows application development, database programming using Java and .Net technologies.

2. Good knowledge in SDLC and Processes like project planning, requirement gathering, development, test planning, release management and production support.

3. Good knowledge and working experience in following methodologies:
a. Agile Development Approach
b. Test Driven Development
c. Behavior Driven Development
d. Continuous Integration & Delivery Model.

4. Excellent communication & analytical skills, good team player, great mentoring capability.

5. Interaction with customer and team spread over different geographical area.

Technologies / Languages: J2SE 5/6, J2EE, .Net 2.0/3.5, C#, ASP.NET, AJAX, XHTML, CSS, JavaScript, jQuery, PL/SQL, Web Services (SOAP based), Winforms, Hibernate, Spring, GWT, XML, XSD, XPath, SAX, JAXB, JUnit, JBehave, Mockito, Selenium, StringTemplate, Log4J, Apache Commons API

Database: Oracle, SQL Server, MySQL, Sybase

Application Server: Tomcat, Sun Application Server, JBoss, GlassFish, Jetty, IIS

Development Environments: Eclipse, NetBeans, Visual Studio.

Designing Tools: Enterprise Architect, Microsoft Visio

Version Control: SVN, Perforce

Build Management: Hudson & JCruisemonitor, TeamCity

Bug Tracking Tools: HP Quality Center, Bugzilla, JIRA

Specialties:
1. Agile Test and Behavior Driven Development
2. Continuous Delivery Approach
2. Spring IOC and MVC
3. Web Services

Comments and Discussions

 
-- There are no messages in this forum --