MODULO SQL & Database Design Master SeriesLAB

Three Phase Commit: Ensuring Atomicity in Distributed Transactions

Introduction

In a distributed database system, transactions often span multiple nodes or sites. Ensuring the atomicity of such transactions is crucial to maintain data consistency and integrity. The Three Phase Commit (3PC) protocol is a widely used algorithm that guarantees atomicity in distributed transactions. In this masterclass, we will delve into the details of the 3PC protocol, its components, and its implementation.

What is Three Phase Commit?

The Three Phase Commit protocol is a distributed transaction protocol that ensures atomicity by dividing the commit process into three phases:

  1. Prepare: In this phase, the transaction manager (coordinator) sends a prepare message to all participants (cohorts) involved in the transaction. Each cohort checks if it can commit to the transaction and sends a response back to the coordinator.
  2. Vote: The coordinator collects the responses from all cohorts and decides whether to commit or abort the transaction. If all cohorts vote to commit, the coordinator sends a commit message to all cohorts. Otherwise, it sends an abort message.
  3. Commit or Abort: Based on the coordinator's decision, each cohort either commits or aborts the transaction.

Components of Three Phase Commit

The following components are involved in the 3PC protocol:

  • Coordinator (Transaction Manager): The coordinator is responsible for managing the transaction and ensuring its atomicity. It sends prepare, commit, and abort messages to the cohorts.
  • Cohorts (Participants): Cohorts are the nodes or sites involved in the transaction. They receive prepare, commit, and abort messages from the coordinator and respond accordingly.

Three Phase Commit Protocol

The 3PC protocol involves the following steps:

Phase 1: Prepare

  1. The coordinator sends a prepare message to all cohorts.
  2. Each cohort checks if it can commit to the transaction. If it can, it sends a "yes" vote to the coordinator. Otherwise, it sends a "no" vote.
  3. The cohort logs the prepare message and the vote response to ensure recoverability in case of a failure.

Phase 2: Vote

  1. The coordinator collects the votes from all cohorts.
  2. If all cohorts vote "yes", the coordinator decides to commit the transaction. Otherwise, it decides to abort the transaction.
  3. The coordinator logs the decision to ensure recoverability in case of a failure.

Phase 3: Commit or Abort

  1. Based on the coordinator's decision, each cohort either commits or aborts the transaction.
  2. If the decision is to commit, each cohort applies the changes made during the transaction.
  3. If the decision is to abort, each cohort rolls back the changes made during the transaction.

Example Walkthrough

Let's consider a simple example to illustrate the 3PC protocol:

Suppose we have a distributed database system with two nodes, Node A and Node B. We want to transfer $100 from an account on Node A to an account on Node B. The transaction involves two operations:

  • Debit $100 from the account on Node A.
  • Credit $100 to the account on Node B.

The 3PC protocol for this transaction would work as follows:

Phase 1: Prepare

  1. The coordinator sends a prepare message to Node A and Node B.
  2. Node A checks if it can debit $100 from the account. If it can, it sends a "yes" vote to the coordinator.
  3. Node B checks if it can credit $100 to the account. If it can, it sends a "yes" vote to the coordinator.

Phase 2: Vote

  1. The coordinator collects the votes from Node A and Node B.
  2. Since both nodes vote "yes", the coordinator decides to commit the transaction.

Phase 3: Commit

  1. The coordinator sends a commit message to Node A and Node B.
  2. Node A debits $100 from the account.
  3. Node B credits $100 to the account.

SQL Queries/Examples

To illustrate the 3PC protocol, let's consider a simple example using SQL queries. We'll use a distributed database system with two nodes, Node A and Node B.

-- Create tables on Node A and Node B
CREATE TABLE NodeA.Accounts (
    AccountID INT,
    Balance DECIMAL(10, 2)
);

CREATE TABLE NodeB.Accounts (
    AccountID INT,
    Balance DECIMAL(10, 2)
);

-- Insert data into the tables
INSERT INTO NodeA.Accounts (AccountID, Balance)
VALUES (1, 1000.00);

INSERT INTO NodeB.Accounts (AccountID, Balance)
VALUES (2, 500.00);

-- Start a transaction
BEGIN TRANSACTION;

-- Debit $100 from the account on Node A
UPDATE NodeA.Accounts
SET Balance = Balance - 100.00
WHERE AccountID = 1;

-- Credit $100 to the account on Node B
UPDATE NodeB.Accounts
SET Balance = Balance + 100.00
WHERE AccountID = 2;

-- Prepare the transaction
PREPARE TRANSACTION;

-- Vote on the transaction
-- Node A votes "yes"
-- Node B votes "yes"

-- Commit the transaction
COMMIT TRANSACTION;

In this example, we start a transaction, debit $100 from the account on Node A, credit $100 to the account on Node B, prepare the transaction, vote on the transaction, and commit the transaction.

Advantages and Disadvantages

The 3PC protocol has several advantages:

  • Atomicity: The 3PC protocol ensures atomicity in distributed transactions.
  • Consistency: The 3PC protocol ensures consistency across all nodes involved in the transaction.
  • Durability: The 3PC protocol ensures durability by logging the prepare, vote, and commit messages.

However, the 3PC protocol also has some disadvantages:

  • Complexity: The 3PC protocol is complex and requires additional overhead to manage the prepare, vote, and commit phases.
  • Performance: The 3PC protocol can impact performance due to the additional overhead of the prepare, vote, and commit phases.

Conclusion

In conclusion, the Three Phase Commit protocol is a widely used algorithm that ensures atomicity in distributed transactions. The protocol involves three phases: prepare, vote, and commit or abort. The 3PC protocol has several advantages, including atomicity, consistency, and durability. However, it also has some disadvantages, including complexity and performance impact.

⚔️ YOUR MISSION

Mission: Implementing Three Phase Commit

Your mission is to implement the Three Phase Commit protocol in a distributed database system. You will need to design and implement the coordinator and cohort components, as well as the prepare, vote, and commit phases.

Objectives:

  • Implement the coordinator component to manage the transaction and ensure atomicity.
  • Implement the cohort component to participate in the transaction and respond to prepare, vote, and commit messages.
  • Design and implement the prepare, vote, and commit phases to ensure atomicity and consistency.

Deliverables:

  • A written report describing the design and implementation of the 3PC protocol.
  • A working prototype of the 3PC protocol implemented in a distributed database system.

Evaluation Criteria:

  • Correctness: Does the implementation ensure atomicity and consistency in distributed transactions?
  • Performance: Does the implementation optimize performance and minimize overhead?
  • Scalability: Does the implementation scale well in a distributed database system?
SQL practice editor
Read the exercise prompt
-- Create tables on Node A and Node B
CREATE TABLE NodeA.Accounts (
    AccountID INT,
    Balance DECIMAL(10, 2)
);

CREATE TABLE NodeB.Accounts (
    AccountID INT,
    Balance DECIMAL(10, 2)
);

-- Insert data into the tables
INSERT INTO NodeA.Accounts (AccountID, Balance)
VALUES (1, 1000.00);

INSERT INTO NodeB.Accounts (AccountID, Balance)
VALUES (2, 500.00);

-- Start a transaction
BEGIN TRANSACTION;

-- Debit $100 from the account on Node A
UPDATE NodeA.Accounts
SET Balance = Balance - 100.00
WHERE AccountID = 1;

-- Credit $100 to the account on Node B
UPDATE NodeB.Accounts
SET Balance = Balance + 100.00
WHERE AccountID = 2;

-- Prepare the transaction
PREPARE TRANSACTION;
WAITING FOR SIGNAL...
BACKNEXT STAGE