Two-phase commit

XA mode

Two phase commit, three phase commit, need the database itself support

Two-phase commit

1. Preparation Stage: The transaction coordinator (transaction manager) sends a Prepare message to each participant (resource manager), and each participant either returns a failure (such as permission validation failure) or executes the transaction locally, writing local redo and undo logs but not committing them, reaching a state of “all is ready”.

2. Commit phase: If the coordinator receives failure messages or timeout messages from participants, he will send a Rollback message to each participant directly. Otherwise, send a Commit message; Participants perform commit or rollback operations on the coordinator’s instructions to release lock resources used in all transactions. (Note: Lock resources must be released at the end)

AT mode (here using THE AT mode of SEate)

The main difference between THE XA mode and THE AT mode is that the AT mode commits a local transaction and releases the local lock during the preparation phase, while the XA mode does not commit a transaction during the preparation phase and thus does not release the local lock, resulting in poor performance

Difference between AT mode and XA mode

Seata implementation principles

Write the isolation

Make sure you get the global lock before committing a phase 1 local transaction.

Unable to commit local transaction because global lock could not be obtained.

Attempts to acquire the global lock are limited to a certain range, beyond which the local transaction is abandoned and rolled back to release the local lock.

A global lock is required before a local transaction commits

After a local transaction is committed, the local lock is released but the global lock is retained (other threads can operate on the local transaction)

To roll back data, compare the data in the UndoLog table with that in the current database. If the data is inconsistent, perform manual operations. CAS operation. If inconsistent, another transaction has modified the data in the table

Submit mechanism

Rollback mechanism

When the commit is successful, you need to delete the data in the additional created table

Branch, global, and Lock

Questions submitted in two phases:

1. A single point of failure. The transaction manager breaks down

2. Data inconsistency. Not all participants receive commit events, resulting in transaction inconsistency

3. The corresponding time is long

Three-stage commit

Solved the two-stage single point of failure

Phase three also presents problems

When phase 3 Docommit is about to return false,TM goes down and RM commits the transaction

TCC

TCC, implemented at the business level, does not require database support

Try commit Cancel is an abbreviation of three words

In the Try phase, subtract and then add a flag bit. (Support resource reservation and intermediate state) calibration is still in the Try phase

Based on message queue + local message table

Half of them require message queuing and idempotency

The local message table is in the same database as the business data table, so local transactions can be used to ensure that the transaction characteristics are met during operations on the two tables, and message queues are used to ensure final consistency.

  1. A message is sent to the local message table after a party to a distributed transaction has written business data. The local transaction guarantees that the message is written to the local message table.

  2. After that, the message in the local message table is forwarded to the message queue. If the forwarding succeeds, the message is deleted from the local message table. Otherwise, the message is forwarded again.

  3. The other party in the distributed transaction reads a message from the message queue and performs the operation in the message.

Note: simple message Hundred send and hundred consumption, there is a problem, if after when performing the writing business data, machine downtime, this will lead to data has been written, but the message was not sent, so you need to write about business data table and the local news writing in the same transaction, can be realized by using Mysql

If the message remains unconsumed, the dead-letter queue can be requeued for human processing