Distributed transaction--Seata

Distributed transactions – Seata

Seata is a distributed transaction solution. Committed to providing high-performance and easy-to-use distributed transaction services, creating a one-stop distributed solution for users. Official website address: http://seata.io/, where documents and podcasts provide a lot of usage instructions and source code analysis.

First, the ACID principle of the transaction

In a distributed system, a business spans multiple services or data sources, and each service is a branch transaction. It is necessary to ensure that the final state of all branch transactions is consistent. Such a transaction is a distributed transaction.

atomicity All operations in the transaction either all succeed or all fail
consistency To ensure the internal integrity constraints and declarative constraints of the database
isolation Transactions operating on the same resource cannot occur concurrently
Persistence All changes made to the database will be permanently saved, regardless of failures

Two, CAP theorem

Distributed system nodes are connected through the network, and there must be a partition problem (P); when partition occurs, the consistency (C) and availability (A) of the system cannot be satisfied at the same time.

Distributed systems have three metrics:

  • Consistency : When a user accesses any node in the distributed system , the data obtained must be consistent.
  • Availability ( availability ): When a user accesses any healthy node in the cluster, it must be able to get a response instead of timeout or rejection.
  • Partition tolerance ( partition fault tolerance )
  • Partition: Due to network failure or other reasons, some nodes in the distributed system lose connection with other nodes, forming an independent partition.
  • Tolerance (fault tolerance): When the cluster is partitioned, the entire system must continue to provide external services.

3. BASE theory

1. BASE theory is a solution to CAP, including three ideas:

  • Basically Available : When a distributed system fails, it is allowed to lose part of its availability, that is, to ensure that the core is available.
  • Soft State ( soft state ): Within a certain period of time, an intermediate state is allowed, such as a temporary inconsistent state.
  • Eventually Consistent : Although strong consistency cannot be guaranteed, data consistency will eventually be achieved after the soft state ends.

2. The biggest problem of distributed transactions is the consistency of each sub-transaction , so we can learn from the CAP theorem and BASE theory:

  • AP mode: Each sub-transaction is executed and submitted separately, allowing inconsistencies in results, and then taking remedial measures to restore the data to achieve final consistency.
  • CP mode: Each sub-transaction waits for each other after execution, commits at the same time, and rolls back at the same time to reach a strong consensus. However, during the waiting process of the transaction, it is in a weakly available state.

4. Distributed transaction model

To solve distributed transactions, each subsystem must be able to perceive each other's transaction status in order to ensure consistent status. Therefore, a transaction coordinator is needed to coordinate each transaction participant (subsystem transaction).

1. Model for solving distributed transactions

  • Branch transaction : Subsystem transaction; a transaction for each subsystem included in a distributed transaction.
  • Global transaction : a collection of associated branch transactions; the entire distributed transaction.

2. The idea of ​​​​solving distributed transactions

  • The idea of ​​final consistency : Each branch transaction is executed and submitted separately. If there is an inconsistency, find a way to restore the data.
  • Strong consistency idea : Do not submit the business after each branch transaction is executed, and wait for each other's results. Then commit or rollback uniformly.

5. Seata Architecture

1. There are three important roles in Seata transaction management :

  • TC (Transaction Coordinator) - Transaction Coordinator : maintains the state of global and branch transactions, and coordinates global transaction commit or rollback.
  • TM (Transaction Manager) - Transaction Manager : Define the scope of a global transaction, start a global transaction, commit or rollback a global transaction.
  • RM (Resource Manager) - Resource Manager : manages resources for branch transactions, talks to TC to register branch transactions and report the status of branch transactions, and drives branch transaction commits or rollbacks.

2. Seata provides four different distributed transaction solutions:

  • XA mode : strong consistency phased transaction mode, sacrificing certain availability and no business intrusion.
  • TCC mode : eventually consistent phased transaction mode, with business intrusion.
  • AT mode : The eventually consistent phased transaction mode, without business intrusion, is also the default mode of Seata.
  • SAGA mode : long transaction mode, with business intrusion.

3. What does the nacos service name consist of?

namespace (namespace) + group (group name) + serviceName (service name) + cluster (area).

4. How does the seata client obtain the cluster name of tc ?

Use the tx-group-servicevalue of the key to vgroupMappingfind it.

Six, XA mode principle

The XA specification is a distributed transaction processing (DTP) standard defined by the X/Open organization . The XA specification describes the interface between the global TM and the local RM. Almost all mainstream databases provide support for the XA specification.

6.1 Seata's XA mode

The work of the first stage of RM :

  1. Register branch transaction to TC
  2. Execute branch business sql but do not submit
  3. Report execution status to TC

The work of the second phase of TC :

  • TC detects the transaction execution status of each branch
  • If all succeed, notify all RMs to commit the transaction
  • If there is a failure, notify all RMs to rollback the transaction

The work of the second phase of RM :

  • Receive TC instructions, commit or rollback transactions

6.2 Advantages and disadvantages of seata's XA

Advantages of XA mode :

  • Strong consistency of transactions, meeting the ACID principle
  • Commonly used databases are supported, the implementation is simple, and there is no code intrusion

Disadvantages of XA mode :

  • Because the database resources need to be locked in the first stage and released only after the end of the second stage, the performance is poor
  • Relying on Relational Databases to Realize Transactions

6.3 Implementing the XA pattern

Seata's starter has completed the automatic assembly of the XA mode, and the implementation is very simple. The steps are as follows:

  1. Modify the application.yml file (each microservice participating in the transaction), and enable the XA mode.
  2. Add the @GlobalTransactional annotation to the entry method that initiates the global transaction.
  3. Restart the service and test.

Seven, AT mode principle

The AT mode is also a phased commit transaction model , but it makes up for the long resource locking period in the XA model.

Phase 1 RM work:

  1. register branch transaction
  2. Record undo-log (data snapshot)
  3. Execute business sql and submit
  4. report transaction status

The work of RM at the time of phase 2 submission :

  • delete undo-log

The work of RM during phase 2 rollback :

  • Restore data to before update according to undo-log

The biggest difference between AT mode and XA mode :

  • The XA mode does not commit transactions in the first stage, and locks resources; the AT mode commits directly in the first stage, without locking resources.
  • The XA mode relies on the database mechanism to achieve rollback; the AT mode uses data snapshots to achieve data rollback.
  • Strong consistency in XA mode; final consistency in AT mode

7.1 Dirty write problem in AT mode

In the case of concurrency, transaction one executes the first phase of RM

  • 1.1. Get the DB lock and save the snapshot
  • 1.2. Executing business
  • 1.3. Submit the transaction and release the DB lock

At this time, the DB lock is released, transaction 2 gets the DB lock, and executes the first stage of RM

  • 1.1. Get the DB lock and save the snapshot
  • 1.2. Executing business
  • 1.3. Submit the transaction and release the DB lock

Later, the DB lock is acquired by transaction 1, and the data is restored according to the snapshot. At this time, the snapshot is restored, resulting in the useless operation of transaction 2, causing data security problems, and isolation is not guaranteed.

7.2 Write isolation in AT mode

Global lock : TC records the transaction that is currently operating a certain row of data. This transaction holds a global lock and has the right to execute.

In the case of concurrency, transaction one executes the first phase of RM

  • 1.1. Get the DB lock (database lock), save the snapshot
  • 1.2. Executing business
  • Acquire the global lock at this time
  • 1.3. Submit the transaction and release the DB lock

At this time, the DB lock is released, transaction 2 gets the DB lock, and executes the first stage of RM

  • 1.1. Get the DB lock and save the snapshot
  • 1.2. Executing business
  • At this time, the global lock is acquired, but since transaction 1 has been acquired and retried, the default is 30 times,
    with an interval of 10 milliseconds
  • 1.4. Task timeout, rollback and release DB lock

At this time, the business

  • wait for DB lock
  • 2.1. Obtain the DB lock and restore data based on the snapshot
  • release the global lock

Since the waiting time of the DB lock is longer than that of the global lock, transaction 1 can wait for the timeout of the transaction 2 task, roll back and release the DB lock, but there are still limitations. The global lock can only work together on the transactions of seata, not the management of global transactions of seata You can also modify the fields of seata.

Solution

  • 1.1. Transaction 1 acquires the DB lock and saves the snapshot --> before-imag

  • 1.2. Transaction 1 executes business s --> after-image (generate a snapshot after execution)

  • In this extreme case, the non-seata management global transaction modifies the fields of seata, commits the transaction, and releases the DB lock

  • At this time, transaction 1 obtains the DB lock in 2.1., restores the data according to the snapshot, compares whether the field data of the database has been modified at this time, compares it with the after-image, and then releases the global lock.

  • 2.2. Record abnormalities, send warnings, and manually intervene

7.3 Advantages and disadvantages of AT mode

Advantages of AT mode :

  • Complete the direct submission of transactions in one stage, release database resources, and have better performance
  • Using global locks to achieve read-write isolation
  • No code intrusion, the framework automatically completes rollback and commit

Disadvantages of AT mode :

  • The soft state between the two phases belongs to the final consistency
  • The snapshot function of the framework will affect performance, but it is much better than XA mode

Eight, TCC mode principle

The TCC mode is very similar to the AT mode, and each stage is an independent transaction. The difference is that TCC implements data recovery through manual coding . Three methods need to be implemented:

  • Try: resource detection and reservation
  • Confirm: Complete the resource operation business; it is required that the Try must be successful and the Confirm must be successful
  • Cancel: Reserved resources are released, which can be understood as the reverse operation of try

8.1 Example of TCC mode

A business that deducts user balances. Assuming that the original balance of account A is 100, the balance needs to be deducted by 30 yuan.

  • Phase 1 (Try): Check whether the balance is sufficient, if it is sufficient, the frozen amount will be increased by 30 yuan, and the available balance will be deducted by 30 yuan
  • Phase 2: If you want to submit (Confirm), the frozen amount will be deducted by 30
  • Phase 2: If you want to roll back (Cancel), the frozen amount will be deducted by 30, and the available balance will be increased by 30

8.2 Advantages and disadvantages of TCC mode

Advantages of TCCs :

  • Complete the direct submission of transactions in one stage, release database resources, and have good performance
  • Compared with the AT model, there is no need to generate snapshots, no need to use global locks, and the performance is the strongest
  • Does not rely on database transactions, but relies on compensation operations, which can be used for non-transactional databases

Disadvantages of TCC :

  • There is code intrusion, and it is too troublesome to manually write try, confirm and cancel interfaces
  • Soft state, transactions are eventually consistent
  • It is necessary to consider the failure of Confirm and Cancel, and do idempotent processing

8.3 Empty Rollback and Service Suspension of TCC

  • Empty rollback : When the try phase of a branch transaction is blocked, it may cause the global transaction to time out and trigger the cancel operation of the second phase. The cancel operation is executed before the try operation is executed, and the cancel cannot be rolled back at this time.

  • Business suspension : For a business that has been rolled back empty, if you continue to execute try in the future, it will never be possible to confirm or cancel.

  • The try operation after an empty rollback should be prevented to avoid suspension. To execute the try operation, it is necessary to first determine whether a rollback has occurred, and it will end if it occurs; to execute the cancel operation, it is also necessary to first determine whether the try operation has been executed.

ps: The TCC mode is similar to the AT mode. There is no need to modify the AT mode of the data source proxy in the yml file. You only need to write and implement three methods to implement the TCC mode.

Nine, Saga mode

Saga mode is a long transaction solution provided by SEATA . It is also divided into two stages:

  • Phase 1: Submit local transactions directly
  • Phase 2: If it succeeds, do nothing; if it fails, it will roll back by writing compensation business

9.1 Advantages and disadvantages of Saga mode

Advantages of Saga mode

  • Transaction participants can implement asynchronous calls based on event-driven, high throughput
  • Submit transactions directly in one phase, no locks, good performance
  • It is easy to implement without writing the three stages in TCC

Disadvantages of Saga mode

  • The duration of the soft state is uncertain and the timeliness is poor
  • No locks, no transaction isolation, dirty writes

10. Comparison of four modes

SHAH AT TCC SAGA
consistency strong consistency Weak consistency Weak consistency eventually consistent
isolation completely isolated Based on global lock isolation Isolation based on resource reservation no isolation
code hacking none none Yes, there are three interfaces to write Yes, to write state machine and compensation business
performance Difference good very good very good
Scenes Services with high requirements for consistency and isolation Most distributed transaction scenarios based on relational databases can Transactions with high performance requirements. There are transactions in which the non-relational database participates. The business process is long and the business process has multiple participants including other companies or legacy system services, which cannot provide the three interfaces required by the TCC model

->微服务技术栈高级篇--分布式事务--Seata课程视频

Advanced Day2-01-Theoretical Basis of Distributed Transaction_哔哩哔哩_bilibili

<-

记录每一个学习瞬间

Guess you like

Origin blog.csdn.net/qq_51601665/article/details/130165630