Current Location: Home> Latest Articles> Comprehensive PHP Tutorial on Implementing Distributed Transactions with TCC

Comprehensive PHP Tutorial on Implementing Distributed Transactions with TCC

gitbox 2025-06-27

Overview

This article explains how to implement the distributed transaction TCC pattern using PHP. TCC stands for Try-Confirm-Cancel, a common approach to ensure transaction consistency in distributed systems. The process includes three stages: trying, confirming, and canceling the transaction.

What is Distributed Transaction TCC

The TCC pattern is a solution to ensure transaction consistency in distributed systems. While traditional single-node transactions rely on ACID properties to maintain consistency, distributed systems involve multiple independent services, making transaction management more complex. TCC addresses this by dividing the transaction into distinct phases.

Try Phase

In the Try phase, the system attempts to execute the business operation and records the status. Only if the try succeeds does the process move on to the Confirm phase; otherwise, it triggers the Cancel phase.

Confirm Phase

The Confirm phase finalizes the transaction by persisting the changes made during the Try phase. This operation is idempotent, meaning it can be safely executed multiple times without side effects.

Cancel Phase

The Cancel phase rolls back the actions performed during the Try phase, restoring the system to its original state. This operation is also idempotent to ensure repeated executions do not affect the final state.

Implementing Distributed Transaction TCC

Next, we demonstrate a simple PHP example implementing the TCC pattern involving two service nodes, A and B, participating in a cross-node transaction.

Preparation

Start by preparing code for the two nodes. Frameworks like Laravel can be used to build these nodes, each responsible for different transaction roles.

Creating the Transaction Manager

class TransactionManager
{
    public function try()
    {
        // Execute business operation and record state during this phase
    }
    public function confirm()
    {
        // Confirm the operation and persist data during this phase
    }
    public function cancel()
    {
        // Cancel the operation and revert state to before the transaction started
    }
}

Implementing Business Logic

class NodeAController
{
    public function transfer()
    {
        $transactionManager = new TransactionManager();
        $transactionManager->try();
    }
}
class NodeBController
{
    public function confirm()
    {
        $transactionManager = new TransactionManager();
        $transactionManager->confirm();
    }
    public function cancel()
    {
        $transactionManager = new TransactionManager();
        $transactionManager->cancel();
    }
}

Orchestrating the Transaction Flow

class TransactionController
{
    public function handle()
    {
        try {
            $nodeAController = new NodeAController();
            $nodeAController->transfer();
            $nodeBController = new NodeBController();
            $nodeBController->confirm();
        } catch (Exception $e) {
            $nodeBController->cancel();
        }
    }
}

Conclusion

This article demonstrated how to implement the distributed transaction TCC pattern in PHP, clarifying the responsibilities and implementations of the Try, Confirm, and Cancel phases. The TCC pattern effectively ensures transaction consistency in distributed environments.

In real-world applications, you can further enhance and customize TCC by adding compensation mechanisms and message queues to improve system robustness and reliability.

We hope this guide helps readers deepen their understanding of the TCC distributed transaction pattern and apply it flexibly in practical projects to build resilient distributed service architectures.