Ali Seata's distributed transaction implementation

1. Seata's transaction mode

Seata provides four transaction modes in different business scenarios:

AT mode: The first-stage execution of each branch transaction, the second-stage commit and rollback, are all automatically completed by the Seata framework. AT mode is a distributed transaction solution without any intrusion into the business.
TTC mode: Compared with AT mode, TCC mode is intrusive to business codes, but TCC mode does not have the global lock of AT mode, and TCC performance will be much higher than AT mode. Applicable to scenarios with high performance requirements such as core systems.
SAGA mode: Sage is a long-term transaction solution, transaction-driven, and uses business scenarios with process review, such as: the financial industry, which requires layer-by-layer review.
XA mode: XA mode is a distributed strong consistency solution with low performance and less use.

2. Component roles in Seata

TC (Transaction Coordinator): It is the server side, the transaction coordinator, which should be deployed separately, maintain the status of global and branch transactions, and drive global transaction commit or rollback.
TM (Transaction Manager): It is the client side, which is integrated by the business system and defines global transactions.
RM (Resource Manager): It is the client side, which is integrated by the business system and manages resources for branch transaction processing.

3. AT mode

1. Automatic Transaction is a non-intrusive distributed transaction solution.
The three major roles are TM, RM and TC. Among them, TM and RM are integrated with the business system as the client, and TC is deployed independently as the server.

In the microservice system, each service cannot mutually perceive whether the transaction is successfully executed. At this time, a special service is needed to coordinate the running status of each service. This service is called TC (Transaction Coordinator), transaction coordinator.

2. The Seata AT transaction manages the global transaction in two stages:
the first stage: execute each branch transaction
the second stage: control the final commit or rollback of the global transaction

3. Execution sequence

  1. Execute each branch transaction
    ①Start TM (Transaction Manager, Transaction Manager), and TM applies to TC to open a global transaction.
    At this time, TC will generate a global transaction ID, called XID, and send XID back to TM.
    ② Start a RM (Resource Manager, resource manager), and pass the XID to the RM.
    RM is responsible for managing branch transactions (that is, local transactions of microservices), and communicates with TC to report the execution status of branch transactions and receive commit or rollback instructions for global transactions.
    ③RM will first use the XID to register the branch transaction with TC, and bring the branch transaction into the corresponding global transaction jurisdiction.
    ④ Execute the branch transaction of the operation DB. Once the branch transaction is executed successfully, RM will report the transaction status.
    ⑤ After TC receives the transaction status reported by RM, it will pass the status information to TM.
    ⑥Assuming that other services are called in the business, RM is also started in the called service, and the XID is passed, and the XID is used to register with the TC and be included in the global transaction jurisdiction.
    After the local transaction is successfully executed, the status is reported, and the TC will send the status to the TM.

  2. Control the final commit or rollback of the global transaction
    Now, TM (Global Transaction Manager) has collected the success status of all branch transactions, it will make a decision,
    determine that the global transaction is successful, and send a commit request of the global transaction to TC.
    It is determined that the global transaction fails, and a rollback request of the global transaction is sent to TC.

4. TCC mode

The tcc mode can be mainly divided into three stages:
Try: do business inspection and resource reservation
Confirm: confirm and submit
Cancel: execute branch transaction business cancellation when business execution error needs to be rolled back, and release reserved resources

Five, saga mode

The Saga mode is a long-term transaction solution provided by SEATA. In the Saga mode:
each participant in the business process submits a local transaction. When a participant fails, the previous successful participants are compensated.
One-stage forward service and Compensation services in the second stage are realized by business development.

Six, XA mode

XA mode is another non-intrusive distributed transaction solution that Seata will open source:

Entrusting snapshot data and row locks to the database through XA instructions to complete
the XA mode is a solution for distributed strong consistency, but it has low performance and is rarely used.

Guess you like

Origin blog.csdn.net/qgbihc/article/details/115176973