Comprehensive Guide to Distributed Transaction Solutions and Seata AT Mode Implementation
This article provides an in‑depth overview of mainstream distributed transaction solutions—including 2PC, 3PC, TCC, Saga, local message tables, and MQ‑based transactions—explains the theoretical foundations such as CAP and BASE, and offers a step‑by‑step tutorial for setting up Seata’s AT mode with Spring Cloud, Nacos, and MySQL.
Introduction
The author celebrates reaching 10,000 followers and introduces the sixth article of the "Spring Cloud Advanced" series, which will discuss several mainstream distributed solutions and the open‑source Seata framework.
What Is a Distributed Transaction?
A distributed transaction spans multiple services that must cooperate over a network, such as order creation with inventory deduction or bank transfers.
CAP Principle
CAP states that a distributed system cannot simultaneously guarantee Consistency, Availability, and Partition tolerance; trade‑offs must be made.
Consistency Types
Strong consistency, weak consistency, and eventual consistency are described with examples.
BASE Theory
BASE (Basic Available, Soft state, Eventual consistency) balances CAP constraints by accepting eventual consistency.
Distributed Transaction Solutions
Various approaches are covered:
2‑Phase Commit (2PC)
3‑Phase Commit (3PC)
TCC (Try‑Confirm‑Cancel)
Local Message Table
MQ Transaction (reliable message)
Maximum Effort Notification
Saga (Command‑Orchestrated and Event‑Choreographed)
Each solution’s workflow, advantages, and drawbacks are illustrated with diagrams.
What Is Seata?
Seata is an open‑source distributed transaction solution offering AT, TCC, Saga, and XA modes, aiming for non‑intrusive integration and high performance.
AT Mode
AT mode uses a two‑phase commit with automatic undo‑log generation; developers only write business SQL.
Setting Up Seata TC (Coordinator)
Steps include downloading Seata, creating required MySQL tables, configuring Nacos as the registry and config center, pushing configuration via nacos-config.sh , and adjusting database connection settings before starting the TC.
Building Seata Clients (RM)
Three microservices—storage, account, and order—are created using Spring Cloud Alibaba, MySQL, and OpenFeign. Each service defines its own undo_log table, configures Seata with matching Nacos settings, and registers a transaction‑group mapping such as service.vgroupMapping.seata-storage-tx-group=default .
The order service acts as the Transaction Manager (TM) and uses @GlobalTransactional to start a global transaction that calls the storage and account services via Feign.
Testing
Normal execution shows successful commit across all services; introducing a timeout in the account service triggers a rollback, demonstrating Seata’s automatic compensation.
AT Mode Principle Analysis
AT’s first phase records before/after images and row locks in a single DB transaction; the second phase either deletes undo logs on commit or uses before images to restore data on rollback, with dirty‑write checks.
Conclusion
The article summarizes seven distributed transaction solutions, provides a hands‑on Seata AT implementation, and notes that future posts will cover Seata’s TCC and Saga modes.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.