“This is the 22nd day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

concept

In distributed system, if transactions are used, distributed transactions will be involved, such as ordering business, ordering is divided into placing orders and reducing inventory,

Placing an order and destocking are two separate services. If placing an order succeeds but destocking fails, an oversold situation will occur. In this case, a distributed transaction will be used to solve this situation.

Distributed transaction solutions

XA scheme

XA schemes are also called two-phase commit transaction schemes, such as corporate consortiums, where a chair is responsible for organizing the consortiums.

In the first stage, the chairman will ask everyone in the team a week in advance whether they will go or not. At this time, the chairman will wait for everyone’s answer. If everyone is OK, he can decide to go together.

The second stage is the group building.

Two-phase commit, the concept of a transaction manager, is responsible for the coordination of multiple databases (explorer) transaction, the transaction manager will ask whether the database is ready, if each database is ok, just formally submitted transactions, perform operations on the database, if there is a database of answer is not ok, will all rollback transaction.

This distributed transaction scheme is suitable for distributed transactions across multiple libraries in a single block application, but it is not suitable for high concurrency scenarios because it relies heavily on the database level to handle complex transactions.

TCC scheme

TCC stands for Try, Confirm, or Cancel

This actually uses the concept of compensation, which is divided into three stages:

  • Try phase: This phase checks the resources of each service and locks or reserves the resources.

  • Confirm phase: This phase refers to the actual operations performed in the various services.

  • Cancel phase: If the business method execution of any of the services fails, there is a need to compensate by performing a rollback of the business logic that has been successfully executed.

For example, when transferring money across banks, it involves distributed transactions between two banks. If TCC scheme is used, the idea is as follows:

  • Try stage: first put two bank accounts to it frozen funds do not allow operation.

  • Confirm stage: Perform the actual transfer operation, deduct the funds of bank A and increase the funds of bank B

  • Cancel stage: If the operation of any bank fails, it needs to be rolled back to compensate. For example, the deduction of bank A succeeds and the capital increase of bank B fails, the capital of bank A’s account needs to be added back.

This scheme is also rarely used, because the transaction rollback actually relies heavily on your own code to roll back and compensate, resulting in huge compensation code.

This scheme is suitable for scenarios with high consistency requirements, which belong to the core of the core of the system, such as common capital scenarios, and TCC scheme can be used.

Reliable message final consistency scheme

Implement transactions based on MQ, such as Alibaba’s RocketMQ support for message transactions.

  1. System A sends A Prepared message to MQ. If the prepared message fails to be sent, the operation will be cancelled.

  2. If the message is sent successfully, the local transaction proceeds, telling MQ to send an acknowledgement message if it succeeds, and telling MQ to roll back the message if it fails.

  3. If an acknowledgement message is sent, system B receives the acknowledgement message and executes the local transaction.

  4. Mq automatically regularly polling all prepared news callback interface, you ask if the transaction fails, local news so I didn’t send a confirmation message, that is to continue to retry or rolled back, generally speaking here can check the database to see if a local transaction before execution, if rolled back, here will be rolled back, avoid the possible local transaction execution is successful, The confirmation message failed to be sent.

  5. If the transaction for system B fails, it is retried, automatically retrying until it succeeds. If it is not possible, either roll back the important fund class, such as the local roll back of SYSTEM B, try to inform A to roll back, or send an alarm to manually roll back and compensation.