Skip to the content.

The concept of distributed transaction and several implementation ideas


The concept of distributed transaction and several implementation ideas


    After serviceization, the single system is split into multiple service systems,and each service accesses its own database. Our one request operation is likely so span multiple services, and we need to operate the data of multilple databases at the same time. We found that the previously used Database transactions are not easy to use. So how can ensure data consistency again after the split architecture based on microservices?

Some nouns and concepts in distributed transactions

The general ideas of distributed transactions are as follows

Eventually consistent transactions (MQ middleware decoupling)

Some suggestions for improving mysql database security

Advantage:

defect:

Transactional messages (single-respoitory transactions or MQ middleware transactional messages)

Features:

DB:Basically, all mainstream OLTP database products in a single database support transaction consistency, and a single database transaction must have the characteristics of ACID, namely atomicity, consistency, isolation and durability.

MQ:For example, both kafka and rocketMQ support the function of transactional production and consumption of message types. In theory, this can also be guaranteed for transaction security. In fact, this is basically the same thing as the first final consistency.

Two-phase commit(2PC)

    In fact, when it comes to two-phase commit, or three-phase commit and XA transaction (also two-phase commit), there are several roles in it:

Compared with 2PC, 3PC sets a timeout for both the coordinator (Coordinator) and the participant (Partcipant), while 2PC only has a timeout mechanism for the coordinator. What problem does this solve? This optimization point is mainly to avoid the problem that the participants cannot release resources when they cannot communicate with the coordinator node for a long time (the coordinator hangs up), because the participants themselves have a timeout mechanism, which will automatically execute after the timeout. Local commit/rollback is used to release resources. This mechanism also reduces the blocking time and scope of the entire transaction, but performance problems and inconsistency problems are still not fundamentally resolved.

Preparation phase

Some suggestions for improving mysql database security
  1. The business initiator initiates a request to the coordinator to obtain the Connection connection.
  2. The coordinator asks each participant if they can start a new business now, and returns Ready/No.
  3. If the participant can start new business, start a transaction (but not commit).
  4. If the participant cannot start a new business, it will return to the coordinator No. If the coordinator receives the result that any participant returns No or does not receive the result within a certain period of time, it will return to the business initiator saying that this time The request failed (may be retried).

Commit/Rollback phase

Some suggestions for improving mysql database security
  1. At this time, the business initiator can issue a commit/rollback transaction command to the coordinator (in the 2PC stage, the coordinator has the logic of timeout check).
  2. Then the coordinator sends an instruction to the business participant, and the business participant can commit or roll back the local transaction.
  3. The coordinator and the coordinator return the result to the business initiator whether the result of this operation is a success or a failure.

Known issues:

  1. Synchronous blocking: All transaction participants are in a synchronous blocking state while waiting for responses from other participants and cannot perform other operations.
  2. Single point problem: The coordinator plays a very important role in 2PC, and a failure will have a great impact. Especially when a failure occurs in Phase 2, all participants will be in a waiting state and cannot complete other operations.
  3. Data inconsistency: In Phase 2, if the coordinator only sends part of the Commit message and the network is abnormal, then only some participants receive the Commit message, that is to say, only some participants submit the transaction, making the system data inconsistent .
  4. Too conservative: the failure of any node will lead to the failure of the entire transaction, and there is no perfect fault tolerance mechanism.

TCC Distributed transaction

Some suggestions for improving mysql database security

Features:

    It is not coupled with a specific service framework, and is located in the business service layer, not the resource layer, and can flexibly choose the locking granularity of business resources. TCC operates local transactions for each service resource, and the data is locked for a short time, which can be It has good scalability and can be said to be designed for independently deployed SOA services.

Some frameworks for distributed microservice transactions in Java

    Some frameworks commonly used in distributed microservice transactions in Java include Hmily, Byte-TCC, Tcc-Transaction, Seata, etc.


back