Distributed Transaction (6) Seata TCC Mode-Introduction to TCC Mode

Project source code:  https://gitee.com/benwang6/seata-samples

TCC basic principles

TCC and Seata AT transactions are both two-phase transactions . The main differences between TCC and AT transactions are :

  • TCC has serious intrusion into business code
    . Data operations at each stage must be coded by themselves, and the transaction framework cannot handle it automatically.
  • TCC is more efficient. There is
    no need to add global locks to the data , allowing multiple transactions to manipulate data at the same time.

The first stage Try

Taking account service as an example, the user account amount should be deducted when placing an order:

If the user purchases 100 yuan of goods, 100 yuan will be deducted.

TCC affairs first reserve the deduction amount of 100 yuan, or freeze the 100 yuan first:

Second stage Confirm

If the first stage can be successfully completed, it means that the "deduction amount" business (branch transaction) will definitely be successful in the end. When the global transaction is committed, TC will control the current branch transaction to commit. If the commit fails, TC will try repeatedly until the commit is successful.

When the global transaction is committed, the frozen amount can be used to finally implement business data operations:

The case of multiple transactions concurrent

Multiple TCC global transactions are allowed to be concurrent. When they perform deductions, they only need to freeze their respective amounts:

Seata TCC transaction mode

Seata supports TCC transaction mode. Same as AT mode, the following components are also required to support global transaction control:

  • TC transaction coordinator
  • TM Transaction Manager
  • RM Resource Manager

In the next section, we will take the order business as an example to demonstrate how Seata implements TCC transactions.

Project source code:  https://gitee.com/benwang6/seata-samples

 

Guess you like

Origin blog.csdn.net/abu1216/article/details/110957531