Two distributed transaction solutions

## 1. 2PC
2PC is a two-phase commit protocol, which divides the entire transaction process into two phases: Prepare phase and commit phase, 2 refers to two phases, P refers to the preparation phase, C Refers to the submission phase.

Some relational databases such as Oracle and MySQL in the computer support two-phase submission agreement, as shown below:

  1. Prepare phase: The transaction manager sends a Prepare message to each participant. Each database participant executes the transaction locally and writes a local Undo / Redo log. At this time, the transaction is not committed. (Undo log records the data before modification, which is used for database rollback, and Redo log records the modified data, which is used to write data files after committing transactions

  2. Commit phase: If the transaction manager receives a participant's execution failure or timeout message, it directly sends a Rollback message to each participant; otherwise, it sends a Commit message; the participant is based on the transaction The manager's instruction performs a commit or rollback operation
    and releases lock resources used during transaction processing. Note: The lock resources must be released at the final stage.

The following figure shows the two phases of 2PC, with two cases of success and failure:

Success:image.png

Failure:image.png

## 2. TCC compensation mechanism
TCC is actually the compensation mechanism used. The core idea is that for each operation, a corresponding confirmation and compensation (cancellation) operation must be registered. It is divided into three stages:

  • Try stage is mainly to do business system detection and resource reservation
  • The Confirm phase is mainly to confirm and submit the business system. When the Try phase is successfully executed and the Confirm phase is started, the default Confirm phase is error-free. That is: As long as Try is successful, Confirm must be successful.
  • The Cancel phase is mainly to cancel the business executed in the state of business execution error and need to roll back, and release the reserved resources.image.png

For example: A wants to transfer money to B, the idea is probably:

我们有一个本地方法,里面依次调用 
1、首先在 Try 阶段,要先调用远程接口把 B和 A的钱给冻结起来。 
2、在 Confirm 阶段,执行远程调用的转账的操作,转账成功进行解冻。 
3、如果第2步执行成功,那么转账成功,如果第二步执行失败,则调用远程冻结接口对应的解冻方法 (Cancel)。 

Advantages: Compared with the two-phase submission, the usability is stronger

Disadvantages: The consistency of the data is worse. TCC belongs to a compensation method at the application layer, so programmers need to write a lot of compensation code during implementation. In some scenarios, some business processes may not be well defined and handled by TCC.

## 3. Message final consistency
Message final consistency should be the most used in the industry, and its core idea is to split distributed transactions into local transactions for processing. This idea comes from ebay. We can see some of these details in the following flowchart:image.png

The basic idea is:

The message producer needs to build an additional message table and record the message sending status. Message tables and business data must be submitted in one transaction, which means that they must be in a database. Then the message will be sent to the consumer of the message via MQ. If the message fails to be sent, it will be retried.

The message consumer needs to process this message and complete its own business logic. At this time, if the local transaction processing is successful, it indicates that the processing has been successful. If the processing fails, it will retry execution. If it is a business failure, you can send a business compensation message to the producer to notify the producer to perform rollback and other operations.

The producer and consumer regularly scan the local message table and send the unprocessed or failed messages again. If there is reliable automatic reconciliation logic, this solution is still very practical.

Advantages: A very classic implementation, avoiding distributed transactions and achieving eventual consistency.

Disadvantages: The message table will be coupled to the business system. If there is no packaged solution, there will be a lot of chores to deal with.

Published 15 original articles · praised 0 · visits 77

Guess you like

Origin blog.csdn.net/xrzi2015/article/details/105518978