Raft-2PC: Distributed Transaction Processing with Raft and Two-Phase Commit
Overview
This project implements a fault-tolerant distributed transaction processing system using a combination of Raft consensus and Two-Phase Commit (2PC). The system is designed for a simple banking application where clients initiate transactions involving data partitions (shards) replicated across multiple clusters of servers.
- Intra-shard transactions are handled using the Raft protocol for consensus within a shard.
- Cross-shard transactions are coordinated using the Two-Phase Commit (2PC) protocol to ensure atomicity across multiple shards.
System Architecture
The system partitions data into shards, each managed by a cluster of servers:
- Three clusters (C1, C2, C3), each managing a distinct data shard.
- Each shard is replicated across all servers within a cluster for fault tolerance.
- Clients act as transaction coordinators and submit intra-shard or cross-shard transactions.
Transaction Handling
1. Intra-Shard Transactions (Raft Consensus)
For transactions within the same shard:
- A client sends a request to a leader in the cluster.
- The leader checks balance availability and locks necessary data items.
- The Raft protocol is used to achieve consensus within the shard.
- Once committed, the transaction is applied to the data store.
2. Cross-Shard Transactions (Two-Phase Commit)
For transactions across multiple shards:
- The client acts as a 2PC coordinator, contacting leaders of the involved clusters.
- Each cluster executes Raft consensus for their portion of the transaction.
- If all clusters are prepared, the coordinator sends a commit message.
- If any cluster aborts, the entire transaction is rolled back.
Features
✔ Supports concurrent intra-shard and cross-shard transactions
✔ Implements Raft consensus for fault tolerance
✔ Implements Two-Phase Commit (2PC) for cross-shard atomicity
✔ Lock management to prevent conflicts
✔ Performance monitoring: throughput & latency measurement
✔ Transaction logging and failure handling
Running the Project
Dependencies
- Python (latest version)
How to Run
- Clone the repository:
git clone https://github.com/Vamsi995/raft-2pc.git cd raft-2pc
- Starting Clusters:
To start the first cluster we need to start the proxy communication manager, that acts as a proxy between the three servers in one cluster. This is made to simulate network partitions in the cluster.
python communication_manager -cluster 1 -port 8080
To start the servers in the cluster execute the below command
python server1.py -cluster <cluster number> -port <port - should be the same one as the network proxy> candidate_id <server_number>
- Reset the server metadata and datastore:
make clean
- Running the client
python client.py
- Running the transactions: The transactions are kept in the
transactions.csv
file, in the format<source_accountid>,<destination_accountid>,<amount>
Commands & Functions
Transaction Handling
PrintBalance(clientID)
: Prints the balance of a given client.PrintDatastore()
: Displays all committed transactions.Performance()
: Prints throughput and latency metrics.
Example Transactions
Intra-shard Transaction
100,200,3 # Transfers 3 units from client 100 to 200 in the same cluster
Cross-shard Transaction
100,1500,3 # Transfers 3 units from client 100 (Cluster C1) to 1500 (Cluster C2)
Demo of Intra Shard & Cross Shard Transactions
Failure Handling
- Transactions are aborted if:
- Insufficient balance
- Lock conflicts
- Failure to reach consensus in Raft
- A cluster votes to abort during 2PC