Databases 14 min read

Understanding Distributed Transactions: From XA to CAP, BASE, 2PC, 3PC, TCC, and Saga

The article explains the necessity of transactions, traces their origins from database XA specifications to modern distributed systems, compares CAP and BASE theories, and reviews mainstream solutions such as two‑phase commit, three‑phase commit, TCC, asynchronous messaging, and the Saga pattern.

Architecture Digest
Architecture Digest
Architecture Digest
Understanding Distributed Transactions: From XA to CAP, BASE, 2PC, 3PC, TCC, and Saga

Transactions solve the vertical consistency problem in distributed systems, ensuring that a series of operations either all succeed or all fail, much like coordinating a group of ants on a rope to move together.

The concept originated in databases with the XA specification, which defines the xa_ and ax_ function families for distributed transaction coordination, and later evolved into two‑phase commit (2PC) as the basic protocol for ensuring atomicity across multiple resource managers.

Two fundamental theories guide distributed system design: the CAP theorem (Consistency, Availability, Partition‑tolerance) and the BASE model (Basically Available, Soft state, Eventually consistent). CAP emphasizes the impossibility of achieving all three guarantees simultaneously, while BASE offers a pragmatic relaxation for scalability.

Key distributed transaction solutions include:

Two‑Phase Commit (2PC) : a coordinator asks participants to prepare and then commit, but can suffer from blocking and single‑point failures.

Three‑Phase Commit (3PC) : adds a "prepare commit" step to reduce blocking and introduce timeout handling, mitigating coordinator failures.

TCC (Try‑Confirm‑Cancel) : replaces a global coordinator with local transactions and logs, avoiding single‑point issues and simplifying recovery.

Asynchronous messaging approaches further decouple services:

Local message tables store pending actions, turning remote transactions into a series of local ones.

When using non‑transactional MQ, a simple pattern is to commit the local transaction after successfully delivering the message. Example code:

try {
    beginTrans();
    modifyLocalData1();
    modifyLocalData2();
    deliverMessageToMQ();
    commitTrans();
} catch (Exception ex) {
    rollbackTrans();
}

Transactional MQs such as RocketMQ (since version 4.3) provide built‑in support for distributed transactions, reducing implementation effort.

The Saga pattern, introduced in 1987, breaks a distributed transaction into a chain of local transactions with compensating actions for rollback, allowing long‑running processes and optional parallel sub‑transactions.

Gossip protocols, popularized by systems like Cassandra, implement BASE‑based data replication and fault detection in peer‑to‑peer topologies.

In conclusion, architects should balance CAP‑based strong consistency solutions with BASE‑based eventually consistent approaches, choosing the most suitable method for the given scenario, ensuring idempotency, and adding retry/rollback mechanisms to improve system resilience.

BASE theoryCAP theoremtccdistributed transactionsTwo-Phase Committhree-phase commitsaga pattern
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.