How Seata’s AT Mode Implements Two-Phase Commit for Distributed Transactions
This article explains Seata’s AT transaction mode, its reliance on the classic two‑phase commit protocol, the roles of TC, TM, and RM, and how snapshot‑based SQL interception ensures consistency and rollback in microservice architectures.
Two-Phase Commit
Two‑Phase Commit (2PC) is a distributed transaction protocol that ensures all participants reach a consistent outcome. In the first, "prepare" phase, the transaction coordinator asks each participant if it can commit; participants respond with "prepared" or "reject" without persisting changes. In the second, "commit" phase, if all participants are prepared, the coordinator sends a commit request, participants persist the changes and acknowledge; otherwise a rollback request is issued.
Advantages of 2PC include simplicity and strong data consistency. Drawbacks are blocking when the coordinator fails, performance overhead due to multiple network round‑trips, and complex failure recovery.
Seata AT Mode
Seata’s AT mode introduces three key roles:
TC (Transaction Coordinator) : a standalone component that maintains global transaction state, receives commit/rollback commands from the TM, and coordinates branch transactions.
TM (Transaction Manager) : embedded in the application, it defines the global transaction boundary, generates a unique XID, and instructs the TC to commit or rollback.
RM (Resource Manager) : communicates with the TC, registers branch transactions, reports their status, and executes commit or rollback on the local resource.
These components work together to achieve distributed transaction processing, as illustrated in the architecture diagram below.
During the first phase, Seata intercepts and parses business SQL, creates a snapshot of the query result, and stores it. The intercepted SQL is then sent through Seata’s JDBC proxy to the actual database, ensuring that the local transaction guarantees data consistency.
In the second phase, the coordinator either deletes the snapshot data and releases row locks on commit, or uses the snapshot to generate reverse SQL for rollback. The DataSourceProxy component provides the necessary JDBC proxy to capture and manage these snapshots.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
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.