Mastering Seata: How Reverse Compensation Powers Distributed Transactions
This article demystifies distributed transactions using Alibaba's Seata, explaining reverse compensation, core concepts (TC, TM, RM), the two‑phase commit protocol, and detailed walkthroughs of AT, TCC, XA, and Saga modes with code examples and step‑by‑step procedures.
Distributed transactions are often confusing due to many specialized terms; this guide clarifies the topic by focusing on Alibaba's Seata framework.
1. What Is Reverse Compensation
Reverse compensation restores data changed by a committed local transaction using an additional UPDATE statement rather than traditional log‑based rollback. For example, if services A, B, and C participate in a transaction and B commits while C fails, B’s changes are undone by executing a compensating UPDATE.
2. Core Concepts in Seata
TC (Transaction Coordinator) – maintains global and branch transaction states and drives commit or rollback.
TM (Transaction Manager) – defines the scope of a global transaction and initiates, commits, or rolls back the transaction.
RM (Resource Manager) – manages resources (typically databases) for branch transactions, registers branches with TC, and reports status.
TC runs as a separate server; TM and RM are embedded in application clients. The @GlobalTransactional annotation marks the entry point where TM creates a global transaction.
3. Two‑Phase Commit (2PC)
2PC involves a coordinator (TC) and participants (RMs). In the first phase, each participant executes its local transaction and reports success or failure to TC. If all succeed, TC instructs participants to commit; otherwise, it orders a rollback (often implemented as reverse compensation).
4. AT Mode (Automatic Transaction)
AT is a fully automatic mode built on 2PC. It records before‑ and after‑images of data and stores them in an UNDO_LOG table.
update product set name = 'GTS' where name = 'TXC';Steps in the first phase:
Parse the SQL to identify type, table, and conditions.
Query the before‑image (original data).
Execute the update.
Query the after‑image using the primary key.
Insert a rollback log containing both images into UNDO_LOG.
Register a branch lock with TC.
Commit the local transaction (data + undo log).
Report the branch result to TC.
Second‑phase actions:
Rollback : TC requests branch rollback, the RM retrieves the undo log, verifies data consistency, and executes a compensating UPDATE such as update product set name = 'TXC' where id = 1, then reports the result.
Commit : TC requests branch commit; the RM asynchronously deletes the corresponding undo log.
5. TCC Mode (Try‑Confirm‑Cancel)
TCC adds explicit user‑defined methods: prepare, commit, and cancel. The first phase reserves resources (e.g., freezes funds). After all participants report success, TC triggers commit; otherwise, it triggers cancel. TCC does not rely on database transaction support.
6. XA Mode
XA follows the standard X/Open two‑phase commit protocol. The branch executes the business SQL inside an XA transaction, then performs XA PREPARE. In the second phase, TC either issues XA COMMIT or XA ROLLBACK. Unlike AT and TCC, XA performs a true rollback rather than reverse compensation.
7. Saga Mode
Saga provides a long‑running transaction solution by defining a sequence of local actions and corresponding compensating actions. If any step fails, the engine executes the predefined compensating actions to undo previous steps, effectively implementing reverse compensation at the workflow level.
Overall, Seata offers multiple patterns—AT, TCC, XA, and Saga—to handle distributed transactions, each with distinct trade‑offs regarding automation, database dependence, and developer effort.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
