Apache Seata (Simple Extensible Autonomous Transaction Architecture) is a distributed transaction framework providing AT, TCC, Saga, and XA modes for microservices.

Transaction Modes

Mode Description Performance Consistency
AT Automatic (undolog-based) High Strong
TCC Try-Confirm-Cancel High Strong
Saga Long-running, compensating Highest Eventual
XA Two-phase commit Low Strongest

Setup

  <dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>
  
  seata:
  enabled: true
  application-id: order-service
  tx-service-group: my-tx-group
  registry:
    type: nacos
    nacos:
      server-addr: localhost:8848
  config:
    type: nacos
    nacos:
      server-addr: localhost:8848
  

AT Mode (Automatic)

Seata intercepts SQL and generates undo logs automatically:

  @Service
public class OrderService {
    @GlobalTransactional(name = "create-order", rollbackFor = Exception.class)
    public Order createOrder(CreateOrderRequest request) {
        Order order = orderRepository.save(new Order(request));
        inventoryClient.reserveStock(request.getItems());  // remote call
        paymentClient.charge(order.getTotal());             // remote call
        return order;
    }
}
  

How AT works:

  1. Parse SQL, register before/after images
  2. Execute business SQL
  3. On commit: async delete undo log
  4. On rollback: apply undo log to reverse changes

Requires undo_log table in each database:

  CREATE TABLE undo_log (
    id BIGINT NOT NULL AUTO_INCREMENT,
    branch_id BIGINT NOT NULL,
    xid VARCHAR(100) NOT NULL,
    context VARCHAR(128) NOT NULL,
    rollback_info LONGBLOB NOT NULL,
    log_status INT NOT NULL,
    log_created DATETIME NOT NULL,
    log_modified DATETIME NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY ux_undo_log (xid, branch_id)
);
  

TCC Mode

Explicit Try-Confirm-Cancel for fine-grained control:

  @LocalTCC
public interface InventoryTccService {
    @TwoPhaseBusinessAction(name = "reserveStock", commitMethod = "confirm", rollbackMethod = "cancel")
    boolean tryReserve(BusinessActionContext context,
                       @BusinessActionContextParameter(paramName = "productId") String productId,
                       @BusinessActionContextParameter(paramName = "quantity") int quantity);

    boolean confirm(BusinessActionContext context);
    boolean cancel(BusinessActionContext context);
}

@Service
public class InventoryTccServiceImpl implements InventoryTccService {
    @Override
    public boolean tryReserve(BusinessActionContext ctx, String productId, int quantity) {
        // Freeze stock (set frozen_quantity += quantity)
        return inventoryRepository.freezeStock(productId, quantity);
    }

    @Override
    public boolean confirm(BusinessActionContext ctx) {
        // Deduct frozen stock (stock -= quantity, frozen -= quantity)
        return inventoryRepository.confirmFreeze(getProductId(ctx), getQuantity(ctx));
    }

    @Override
    public boolean cancel(BusinessActionContext ctx) {
        // Release frozen stock (frozen_quantity -= quantity)
        return inventoryRepository.releaseFreeze(getProductId(ctx), getQuantity(ctx));
    }
}
  

Saga Mode (State Machine)

For long-running business processes:

  @StateMachineEngine
public class OrderStateMachine {
    // States: CreateOrder → ReserveStock → ProcessPayment → Complete
    // Each transition has a compensate action
}
  

Seata vs Manual Saga

Feature Seata AT Manual Saga
Setup Undo log table + TC server Custom code
SQL changes Automatic interception Manual
Rollback Automatic via undo log Manual compensate
Performance Good (AT mode) Best (no overhead)
Control Less Full

Best Practices

  • Use AT mode for quick adoption with minimal code changes
  • Use TCC when you need fine-grained control over resource locking
  • Use Saga mode for long-running processes (hours/days)
  • Deploy Seata Server (TC) in HA mode for production
  • Monitor undo log table size — failed transactions accumulate logs
  • Consider manual Saga for simpler domains — Seata adds operational complexity