Seata Distributed Transactions Explained: Compensation, 2PC, AT, TCC, XA, Saga
This article introduces Seata's approach to distributed transactions, covering the concept of reverse compensation, core components (TC, TM, RM), the two‑phase commit protocol, and detailed walkthroughs of Seata's AT, TCC, XA, and Saga modes with practical examples and diagrams.
Hello, I am SanYou.
Today we will explore the topic of distributed transactions, focusing on Alibaba's Seata framework.
1. What is Reverse Compensation
Reverse compensation is a technique used in distributed transactions where, instead of rolling back via traditional database redo logs, an update SQL statement restores the data to its previous state.
For example, if services A, B, and C are involved and B commits successfully but C fails, B's changes are undone by executing a compensating update.
2. Core Concepts
Seata defines three core components:
TC (Transaction Coordinator) – maintains global and branch transaction states and drives commit or rollback.
TM (Transaction Manager) – defines the scope of a global transaction, starts it, and triggers commit or rollback.
RM (Resource Manager) – manages resources for branch transactions, registers branches with TC, and drives branch commit or rollback.
TC runs as a separate server, while TM and RM are embedded in the application.
In Seata, a global transaction is demarcated with the @GlobalTransactional annotation, which is applied on the TM side.
3. Two‑Phase Commit (2PC)
2PC involves two stages: the prepare phase where each participant reports its outcome to the coordinator, and the commit/rollback phase where the coordinator decides based on the collected results.
In Seata, the various transaction modes are extensions of this basic 2PC protocol.
4. AT Mode
AT (Automatic Transaction) is a fully automatic rollback mode built on 2PC.
Phase 1 (Prepare): parse SQL, capture before‑image, execute the update, capture after‑image, write an undo log, acquire a global lock, commit the local transaction, and report to TC.
Phase 2 (Commit/Rollback): if all branches succeed, TC instructs commit; otherwise, TC triggers rollback, which uses the undo log to execute a compensating update.
Example SQL:
<code>update product set name = 'GTS' where name = 'TXC';</code>Rollback uses the stored undo log to run:
update product set name = 'TXC' where id = 1;Developers only need to add an @GlobalTransactional annotation and create an undo_log table.
5. TCC Mode
TCC (Try‑Confirm‑Cancel) is a manual two‑phase mode where the developer implements three methods: prepare , commit , and rollback . It does not rely on underlying database transaction support.
6. XA Mode
XA follows the standard X/Open two‑phase commit protocol, providing true rollback semantics via the database's XA support.
Phase 1 executes the business SQL within an XA branch and performs prepare . Phase 2 either commits or rolls back the XA branch based on the coordinator's decision.
7. Saga Mode
Saga provides a long‑running transaction solution where each step has a compensating action. It resembles a workflow engine: successful steps continue forward, while failures trigger the predefined compensating steps.
In summary, Seata offers multiple transaction modes—AT, TCC, XA, and Saga—each built on the two‑phase commit foundation, allowing developers to choose the most suitable strategy for their microservice architecture.
Sanyou's Java Diary
Passionate about technology, though not great at solving problems; eager to share, never tire of learning!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.