Database - Distributed Transaction

1. Distributed transactions

Distributed transactions refer to transaction operations involving multiple independent resources or services distributed in different nodes or systems. It requires all related operations to be either successfully committed or rolled back to maintain data consistency.

The principle of distributed transactions is based on the requirements of ACID (Atomicity, Consistency, Isolation, and Persistence) characteristics, and the consistency of transactions is guaranteed through the cooperation between the Coordinator and Participants. 

The working principle is as follows:
1. The initiator of the transaction acts as the coordinator to coordinate the execution of the entire distributed transaction.
2. The coordinator communicates with each participant to negotiate and execute the individual operations of the transaction.
3. The participant performs the corresponding operation and returns the result of the operation to the coordinator.
4. The coordinator collects the operation results of all participants, and if all operations are successful, the transaction is committed; if any operation fails, the transaction is rolled back.

The main purpose of using distributed transactions is to ensure the consistency and integrity of data in a distributed environment. In a distributed system, each node may have an independent database or service, and the use of distributed transactions can ensure the atomicity of multiple operations and avoid data inconsistency or loss.

Distributed transactions are applicable to the following situations:
1. Transaction operations across multiple databases or services need to ensure data consistency.
2. In a high-concurrency environment, it is necessary to ensure the atomicity and consistency of multiple operations.
3. Business processes across multiple systems need to maintain data integrity.

2. Two-phase commit

Two-Phase Commit (2PC) is a commonly used distributed transaction protocol for coordinating and managing the commit and rollback of distributed transactions.

The steps of the two-phase commit are as follows:
1. Prepare Phase (Prepare Phase): The coordinator sends a prepare request to all participants, and the participants perform the preparation operation of the transaction, and return the result of ready or failed to the coordinator.
2. Commit Phase: According to the preparation results of all participants, if all participants are ready, the coordinator sends a commit request to all participants; if any participant fails to prepare, the coordinator sends a request to all participants Actor sends a rollback request.
3. Completion Phase: After receiving the commit request, the participant executes the commit operation of the transaction and releases resources; after receiving the rollback request, the participant executes the rollback operation of the transaction and releases resources.

Distributed transactions can call different databases such as Oracle and MySQL at the same time, but it is necessary to ensure that the database itself supports distributed transactions and corresponding protocols, such as XA protocol. The application program needs to use the transaction management mechanism and interface of the corresponding database to realize the management and control of distributed transactions and ensure the coordination and consistency of operations between various databases.

3. XA protocol

XA is a protocol and interface standard for implementing distributed transactions. XA (eXtended Architecture) defines the communication protocol between the database transaction manager (Transaction Manager) and the database service (Resource Manager), which is used to ensure the atomicity, consistency, isolation and persistence of multiple database operations in a distributed environment sex.

The principles of the XA protocol are as follows:
1. At the beginning of a distributed transaction, the transaction manager acts as a coordinator (Coordinator) to create a global transaction and generate a global transaction ID.
2. The coordinator communicates with each participant's database service, and the participant associates its local transaction with the global transaction.
3. During the execution of the transaction, the coordinator calls the prepare operation of the participant through the XA interface, and the participant prepares the state of the transaction but does not submit it yet.
4. The coordinator sends a commit request to each participant after receiving the successful feedback of the prepare operation from all participants, and the participant commits the local transaction and releases the resources.
5. If any participant's prepare operation fails or the coordinator receives a rollback request, the coordinator will send a rollback request to all participants, and the participants cancel the local transaction.

XA is used in the case of distributed transactions, usually involving multiple databases or resources, for example: transaction operations across databases, transaction operations across distributed systems, etc. When a transaction needs to perform operations on multiple databases or resources at the same time, and requires these operations to be either successfully submitted or rolled back, XA needs to be used to manage and control distributed transactions.

 

For distributed transactions using database XA, follow these steps:
1. Initialize the XA transaction manager and create a global transaction ID.
2. Start the XA transaction and mark the beginning of the transaction.
3. Execute the operations of the XA transaction on each participant's database connection, associating each database operation with a global transaction.
4. Commit or rollback the XA transaction, and the coordinator sends the corresponding command to each participant.
5. Participants commit or rollback their local transactions according to the commands received.

 

In distributed transactions, both Oracle and MySQL support the XA protocol, so XA transactions of Oracle and MySQL can be called simultaneously through the XA interface. Applications need to use the XA driver and XA interface of the corresponding database to manage and execute distributed transactions, and ensure the correct implementation of the XA protocol between the coordinator and participants.

 

 4. Seata and XA

Seata is an open source distributed transaction solution designed to simplify the development and management of distributed transactions. It provides an efficient and reliable way to handle distributed transactions across multiple databases, message queues, and other resources, and is closely related to the data XA protocol.

Seata realizes the support for distributed transactions by integrating with the XA protocol of the database. The XA protocol defines the communication protocol between the Coordinator and the Participants to ensure the atomicity, consistency and persistence of multiple database operations in a distributed environment.

The calling principle of Seata is as follows:
1. As the client of Seata, the application starts a global transaction through the API provided by Seata and obtains a global transaction ID.
2. During transaction execution, the application marks all involved database operations as branch transactions (Branch Transaction), and associates branch transactions with global transactions.
3. Seata's coordinator is responsible for coordinating the execution of global transactions and each branch transaction.
4. In the transaction commit phase, the application calls Seata's commit interface, and the coordinator sends a commit request to all branch transactions and waits for the response of the branch transactions.
5. If all branch transactions are committed successfully, the coordinator sends a global commit request; if any branch transaction fails to commit, the coordinator sends a global rollback request.
6. After the branch transaction receives the commit request, it executes the commit or rollback of the local database operation.

The main purpose of using Seata is to simplify the development and management of distributed transactions. It provides a unified transaction programming model, hides the complexity of distributed transactions, and enables developers to write distributed transactions like local transactions. Seata provides high-performance and reliable distributed transaction support to ensure data consistency and integrity.

Seata is suitable for the following situations:
1. Transaction operations across multiple databases or services need to ensure data consistency and integrity.
2. In a high-concurrency environment, complex distributed transaction scenarios need to be processed.
3. It is necessary to centrally manage and monitor the status and execution of distributed transactions.

In summary, Seata is a distributed transaction solution that is closely related to the data XA protocol. By integrating the XA protocol, Seata supports distributed transactions and simplifies the development and management of distributed transactions. It provides high-performance and reliable transaction support, suitable for complex distributed environments and scenarios requiring data consistency.

Guess you like

Origin blog.csdn.net/summer_fish/article/details/130984426