[2.2 Distributed transaction (Seata)] This article introduces the introductory knowledge of distributed transaction and the Seata framework

1. Introduction to Seata

  • Seata is a free distributed transaction solution that follows the Apache 2.0 protocol and is open sourced by Alibaba.
  • Seata's goal is to provide a lightweight and simple solution for distributed transaction processing.
  • Seata provides a simple way to solve the difficulties of distributed transactions, such as ensuring data consistency and transaction isolation.
  • Seata provides a convenient way for distributed transaction processing, allowing applications to use traditional transaction models.
  • Seata is an excellent distributed transaction solution that can provide fast, simple and reliable distributed transaction processing for applications.
  • Seata is an open source transaction solution of Alibaba, which aims to solve the consistency problem of distributed transactions.
  • Seata can effectively solve the transaction consistency problem in distributed systems.
  • Through the collaborative work of the three core components of TC, TM and RM and a series of auxiliary components, Seata realizes the management and control of distributed transactions and ensures the consistency and correctness of data.
  • When using Seata to build a distributed system, Seata will automatically manage and control the commit and rollback of transactions for us to ensure data consistency.

2. Three modes of Seata

Currently used popularity situation: AT > TCC > Saga

2.1 XA mode

Seata supports distributed transactions in XA mode, and it is realized by wrapping data sources.

The XA mode adopts the two-phase commit protocol (2PC), and the transaction consistency of multiple databases is realized through the coordinator, which needs to rely on the implementation of the XA interface of the database. During the transaction submission process, if any participant database crashes or the network communication is abnormal, the entire transaction will not be completed, and a manual rollback operation is required.

2.2 Overview

A distributed global transaction, the whole is a two-phase commit model.
insert image description here

A global transaction is composed of several branch transactions. The branch transactions must meet the requirements of the two-phase commit model, that is, each branch transaction needs to have its own: one-stage
prepare behavior
and two-phase commit or rollback behavior

According to the different behavior patterns of the two phases, we divide the branch transactions into

  • Automatic(Branch)Transaction Mode
  • TCC(Branch)Transaction Mode。

2.2.1 TCC mode

TCC mode is to split a large transaction into three small transactions: try, confirm and cancel.
Seata supports distributed transactions in TCC mode.
In TCC mode, applications can manage the processing of distributed transactions in a finer-grained manner.
The TCC mode does not depend on the transaction support of the underlying data resources.
TCC mode refers to the management center that supports the integration of custom branch transactions into global transactions.

stage name alias Principle and division of labor
trial stage One stage, prepare behavior Call custom prepare logic, all business checks and resource reservation
confirmation stage Two-stage commit behavior Call custom commit logic, real business execution
cancel phase Two-stage rollback behavior Call custom rollback logic, resource release

2.2.2 AT mode [the most common]

AT mode is the most common mode.
Seata supports distributed transactions in AT mode.
Seata can be implemented using underlying data sources and frameworks like JDBC.

principle

The commit and rollback of transactions are realized through database locks. When a transaction involves multiple database operations, these operations will be put into the same transaction. If any operation fails, the entire transaction will also fail, and all previous operations will be rolled back.

premise

Based on a relational database that supports local ACID transactions;
Java applications access the database through JDBC.

overall mechanism

Evolution of the two-phase commit protocol:

Phase 1: Business data and rollback log records are committed in the same local transaction, releasing local locks and connection resources; Phase 2
:
① Commit asynchronously and complete very quickly;
② Rollback is reversed through the rollback log in phase 1 towards compensation.

AT mode is based on a relational database that supports native ACID transactions:

The first-stage prepare behavior: in the local transaction, submit the business data update and the corresponding rollback log record; the
second-stage commit behavior: immediately complete successfully, and automatically asynchronously clean up the rollback log in batches;
the second-stage rollback behavior: through the rollback log, Automatically generate compensation operations to complete data rollback.

write isolation

In the first stage, before the local transaction commits, it is necessary to ensure that the global lock is obtained first;
if the global lock is not obtained, the local transaction cannot be submitted;
the attempt to obtain the global lock is limited to a certain range, and if it exceeds the range, it will be abandoned, and the local transaction will be rolled back and released.

3. The core components of Seata

It includes three core components: TC (Transaction Coordinator), TM (Transaction Manager) and RM (Resource Manager), and some auxiliary components: AT, TCC, Saga.

insert image description here

Seata's distributed transaction processing scheme can be divided into four main coordinators:

components Coordinator component explanation
TC coordinator, transaction coordinator The core component of the distributed transaction Seata, the role: to coordinate the work of all participants and handle the submission and rollback of distributed transactions, responsible for managing and coordinating the processing of the entire distributed transaction, it can manage the registration of resources and coordinate multiple RMs work. After TC receives the registration request of the global transaction, it will assign a globally unique XID to the transaction. During the execution of the global transaction, TC also maintains the state of all participants, and submits the transaction when the global transaction is executed successfully, otherwise rolls back the transaction.
TM coordinator, transaction manager The core component of distributed transaction Seata, its role: manage branch transactions, including the creation, submission, rollback and other operations of branch transactions. TM can also split global transactions to support huge data sets and high concurrent access. When an application needs to perform distributed transaction processing, TM is responsible for binding all operations into one transaction and ensuring the integrity and reliability of transaction processing.
RM coordinator, resource manager RM is used to manage the submission and rollback of local transactions, and uses the local resource manager to manage resource control. RM will assign a local unique TXID to each transaction, and use this TXID to coordinate the processing of resources. RM is responsible for managing the underlying resources in the application, such as databases, message queues, and caches. When distributed transaction processing is required, RM will register resources with TC and process various transaction requests of RM.
AT AT coordinator, autocommit mode In AT mode, the application must update it into the central coordinator as it performs each operation. When a distributed transaction needs to be undone, the AT coordinator will undo all operations according to the order of operations.

4. How Seata works

Let's take a closer look at how Seata works

4.1 The first step: the start of the global transaction

When an application starts, Seata creates a global transaction for the application and assigns a globally unique XID.
The global transaction includes multiple branch transactions (ie RM), and is coordinated and managed by TC.

4.2 The second step: registration of branch transactions

Before a branch transaction can participate in a global transaction, it needs to register with the TC. When RM sends a "registration" request to TC, TC will allocate a local unique TXID for the branch transaction and return the TXID to RM. At this point, RM can use this TXID to participate in the local transaction controlled by the global transaction.

4.3 The third step: Execution of branch transactions

During the execution of branch transactions, RM monitors the execution of its local transactions and reports the execution results to TC. During this period, Seata will manage and control the global transaction to ensure its execution consistency. If all branch transactions are executed successfully, TC considers that the global transaction is executed successfully, otherwise TC rolls back the entire global transaction.

4.4 Step 4: Commit or rollback of global transactions

When all branch transactions are executed, TC will decide whether to commit or rollback the global transaction according to the corresponding coordination rules. If TC decides to commit, Seata will submit the global transaction to TM for processing. The TM notifies the corresponding RM to submit the local transaction according to the global transaction information. If TC decides to roll back, Seata will call RM to roll back all branch transactions and global transactions.

5. Features and advantages of Seata

Seata is a distributed transaction solution with the following advantages, the following are the main features of Seata:

5.1 High availability and data consistency

Seata provides reliability and data consistency guarantees for the processing of distributed transactions, and can guarantee the integrity of distributed transactions and data consistency at any time. Seata is scalable vertically and horizontally and supports running on various hardware and operating system platforms.

5.2 Fault tolerance

Seata provides highly available distributed transaction processing and supports fault tolerance when exceptions occur.

5.3 Simplicity

Seata is very simple to use and can easily implement distributed transactions in the application's workflow.

5.4 High Performance

Seata's distributed transaction processing has high performance and can handle a large number of distributed transaction requests quickly and reliably.

5.5 Open source community support

Seata is a free and open source software with strong open source community support and a broad user base.

5.6 Integration with Spring Framework

Seata can be easily integrated into Spring applications, which is very beneficial for applications that require distributed transaction processing.

1. Introduce transaction dependencies,

2. Introduce annotation: @Transaction

Seata's AT mode? ? ? ?

Undo log, roll back the log table.

insert image description here

What is the TCC mode?

Guess you like

Origin blog.csdn.net/wstever/article/details/131185126