2PC

A phase:

The coordinator of the transaction issues a prepare request to all transaction participants. After receiving the request, the participant performs the local transaction update, but does not commit for the time being. The completion message is returned to the transaction coordinator, who receives the message and starts phase 2.

Stage 2:

After receiving the completion message from all participants, the transaction coordinator commits the local transaction through all participants. The transaction participants Commit the local transaction, release resources, and feedback the completion message to the transaction coordinator. When all are complete, the entire distributed transaction is complete.

Problems: 1. Performance Problems The XA protocol pursues strong consistency. Only when all nodes are ready, a notification is submitted. If the coordinator node fails, the entire transaction will fail 3. Data inconsistencies In phase 2, if participants of some transactions do not receive commit messages and some transactions receive commit messages because of network problems, data inconsistencies between nodes may occur

3PC

Three phase CanCommit

The transaction participant does not receive a COMMIT message. The CanCommit timeout mechanism is introduced and the commit is automatically performed locally. Fixed 2PC coordinator single point of failure, but still didn’t fix performance and inconsistencies.

TCC

try

Review transactions before committing and reserve resources

commit

Resources reserved in the try phase will be executed.

cancel

Cancel service operations and release resources reserved for the try phase.

RocketMQ transaction

1. The sender sends a half message first. After the local transaction is successfully executed, the sender submits a half-message. In this case, the consumer can send the message.

Problem: If sending a preliminary message succeeds, the local transaction succeeds, but sending an acknowledgement message fails. This is A problem, because user A successfully deducted the money, but the money service did not subscribe to the confirmation message, so the money cannot be added. There is a data inconsistency here.

MQ provides a status check mechanism that periodically iterates through half-messages in the Commitlog. When the half-messages are checked, the COMMITlog queries whether the local transaction is successfully committed. If the half-messages are successfully committed, the commitlog can consume the half-messages.

Seata

AT mode

TM applies to TC for enabling a global transaction, and TC returns a unique global transaction XID.RM registers the branch transaction with TC, adds it to the global transaction XID management, and returns the branch transaction branceId. 3.RM performs the local transaction, operates the database, and records the mirror before and after data operation. 4.RM calls two branch transactions remotely after successful execution, propagating the XID to the other branch transaction 5. The other branch continues to register the local transaction with the TC and joins the global transaction corresponding to the XID, returning the branch transaction branchId 6. Perform local transactions, operate the database, record the mirror before and after the data operation, record it together with XID branchId into the undo_log table 7. After the global transaction link is processed, TM initiates the submission or rollback of the global transaction to TC. 8.TC manages all branch transactions and decides whether to roll back