Interview was asked distributed transaction (2pc, 3pc, TCC), this explanation is OK!

Time:2021-2-26

This article is included in personal blog: http://www.chengxy-nds.top Sharing of technological resources and common progress

I still remember when I first started to write Java, the first project I contacted was a business system of State Grid, which was said to have invested 500 million yuan in R & D. in its heyday, the number of R & D personnel once reached 500. The project adopts the most popular SSH (struts + Spring + Hibernate) framework at that time. The typical three-tier architecture (controller > Service > Dao) is simple and crude. The code written by all people is put into a large project, and the size of the project file reaches several hundred M. solving code conflicts is the biggest workload at that time.

However, the dramatic thing is that five people went online at the same time on the test day, and the project collapsed… Ah! You can never imagine the anger of Party A, the ancestors of everyone in the project team are being asked.
Interview was asked distributed transaction (2pc, 3pc, TCC), this explanation is OK!
Said some useless, always think of this thing in my mind, do not say not happy, let’s listen to it as a joke, let’s get down to business


introduction

Two days ago, a student’s message was posted on the official account, which said that the interview was hung on this topic. Nowadays, with the popularity of microservice architecture, the interview questions are gradually upgrading. It is no longer a simple question about SSH framework knowledge and data structure in earlier years.High concurrencyHigh availabilityDistributed service governancedistributed file system Distributed XXXIt doesn’t matter whether the project is actually used or not. The key is that you have to understand. Is there always a feeling that you can’t learn?


What is distributed transaction?

Let’s take a look at the definition of distributed transaction on Baidu: distributed transaction refers to that the participants, servers supporting transactions, resource servers and transaction managers are located on different nodes of different distributed systems.
Interview was asked distributed transaction (2pc, 3pc, TCC), this explanation is OK!
Er ~ I’m even more confused after reading it. Let’s draw a simple picture to let you understand. For example, when the business volume of the system is very small“one-stop”The system can fully meet the existing business needs, all businesses share a database, the entire order process may only use one method to operate the database under the same transaction.

At this point, all operations are in a transaction, either all committed or all rolled back.
Interview was asked distributed transaction (2pc, 3pc, TCC), this explanation is OK!

But as business continues to grow“one-stop”The system can not bear the huge traffic gradually, so it is necessary to divide the database into different databases and tables to split the business service(SOA)The order center, user center and inventory center are separated. This results in isolation between businesses, each business maintains its own database, data exchange can only be RPC calls.

When you place an order again, you need to operate the order dB and inventory dB at the same time to create an order and deduct inventory. The two-step operation must be successful at the same time, otherwise it will cause business chaos. At this time, we can only ensure the data consistency of our own services, but we can’t guarantee whether the operation of calling other services is successful. Therefore, in order to ensure the data consistency of the whole order process, we need distributed transaction intervention.

Interview was asked distributed transaction (2pc, 3pc, TCC), this explanation is OK!

Before talking about distributed transaction, let’s recall the basic concept of transaction: a transaction is a program execution unit, in which all operations are either executed successfully or failed.

There are four basic characteristics of a transaction, which we often say(ACID)。

Atomicity(atomicity): a transaction is an integral whole. All operations in the transaction either succeed or fail.

Consistency(consistency): before and after the execution of the service, the data must be consistent from one state to another (a transfers money to B, but B does not receive it when a deducts money).

Isolation(isolation): multiple concurrent transactions are isolated from each other and cannot interfere with each other.

Durablity(persistence): after the transaction is completed, the changes to the database are permanently saved and cannot be rolled back.

The above knowledge points are repeated concepts, which must be memorized in an interview.

Distributed transaction solution

When there are difficulties, there must be solutions. Nothing can defeat a clever programmer.

Xa protocolIt is a distributed transaction protocol based on databaseTransaction managerandLocal resource managerTransaction managerAs a global scheduler, it is responsible for giving unified orders to all local resource managersSubmitperhapsRollBACK . Second order commit protocol(2PC)And third order commit protocol(3PC)It is derived from this agreement. nowadaysOracleMysqlAnd other databases have been implementedXa interface

1. Two stage submission (2pc)

Two paragraph submissionAs the name suggests, there are two stages of submission: the first stage, preparation stage (voting stage); the second stage, submission stage (execution stage).
Interview was asked distributed transaction (2pc, 3pc, TCC), this explanation is OK!

The picture above comes from the Internet. If there is any infringement, please delete it

Next, take the example of single deduction inventory to briefly describe the principle of two-stage submission (2pc)

We talked about service-oriented business before(SOA)In the future, a single order process will use multiple services, and each service can not guarantee the success of other services. At this time, a global role is needed(coordinator )For each service(participant)Coordination.

Interview was asked distributed transaction (2pc, 3pc, TCC), this explanation is OK!

An order request came throughcoordinator For everyoneparticipantsend outPrepareMessage to execute the local data script without committing the transaction.

Ifcoordinator I got itparticipantFailure message or timeout, directly to eachparticipantsend outRoll backMessage; otherwise, sendCommitNews;participantaccording tocoordinator To release all the resources occupied in the process of transaction processing, it is obvious that2PCAll operations are successful or failed.

Disadvantages of 2pc

Two phase commit seems to provide atomic operation, but it has serious defects

  • Data inconsistency caused by network jitter:In the second stagecoordinator towardsparticipantsend outcommitAfter the command, once the network jitter occurs at this time, it will cause a part of the network jitterparticipantGot itcommitRequest and execute, but others are not receivedcommitRequestedparticipantUnable to perform transaction commit. This leads to data inconsistency in the whole distributed system.
  • Synchronization blocking caused by timeout: 2PCAll participant nodes in areTransaction blockingWhen aparticipantNode communication timeout, the restparticipantWill block passively, occupy resources and cannot be released.
  • Risk of single point of failure:Due to heavy dependencecoordinator , oncecoordinator There is a fault, and at this timeparticipantThey are still in the state of locking resources and cannot complete the transactioncommitOperation. Although a new coordinator will be elected after the failure of the coordinator, it is impossible to solve the problem caused by the previous onecoordinator Caused by downtimeparticipantThe problem of being blocked.
2. Three stage submission (3pc)

Three stage commit (3pc) is an upgrade optimization of two stage commit (2pc),3PCstay2PCIn the first and second phases of the project, a preparation phase is inserted. It ensures that the state of each participant node is consistent before the final submission phase. At the same time, the time-out mechanism is introduced in both coordinator and participantparticipantNot received for various reasonscoordinator After the commit request of, it will commit the local transaction, and will not block and wait all the time. It is solved2PCSingle point of failure problem, but3PCStill can not fundamentally solve the problem of data consistency.

Interview was asked distributed transaction (2pc, 3pc, TCC), this explanation is OK!

The picture above comes from the Internet. If there is any infringement, please delete it

The three stages of 3pc areCanCommitPreCommitDoCommit

CanCommit: the coordinator sends the cancommit command to all participants, asking if the transaction commit operation can be performed. If all the responses are yes, the next phase will be started.

PreCommitcoordinator To allparticipantsend outPreCommitAfter the participant receives the precommit request, if the participant successfully executes the transaction operation, it returns theYesResponse and enter the final commit phase. Once one of the participants is sent to the coordinatorNoIf the coordinator does not receive the response from the participants, the coordinator will send the response to all the participantsabortThe participant accepts the abort command to interrupt the transaction.

DoCommit: in the first two stages, the response feedback of all participants was positiveYESAfter that, the coordinator sends theDoCommitIf the coordinator does not receive the ACK response from the participant, it will send it to all participantsabortRequest command, interrupt the execution of the transaction.

3. Compensation transaction (TCC)

A lot of beginners are always confusedTCC2PC3PCThese concepts are confused. In fact, they are confusedTCCAnd2PC3PCIt’s just a solution to implement distributed transaction.

TCC(try confirm cancel) is also known asCompensation servicesTCCAnd2PCThe idea is very similar, transaction processing process is also very similar, but2PCIt is applied at the DB level, while TCC can be understood as applied at the application level2PCWe need to write business logic to implement.

TCCIts core idea is to register a corresponding try and cancel for each operation.

Take the single deduction inventory and explain its three operations:

Try stage:

When placing an order, try operation is used to deduct inventory reserved resources.

Confirm phase:

Confirm to execute the business operation and initiate the purchase request based on the reserved resources.

Cancel phase:

As long as one of the related businesses involved fails to reserve resources, the reservation requests of all business resources will be cancelled.

Interview was asked distributed transaction (2pc, 3pc, TCC), this explanation is OK!

The picture above comes from the Internet. If there is any infringement, please delete it

Disadvantages of TCC:

  • Strong application intrusion: due to the business level of TCC, every operation needs to havetryconfirmcancelThree interfaces.
  • Development is difficult: the amount of code development is large, and data consistency should be ensuredconfirmandcancelThe interface must also be idempotent.

summary

This paper briefly introduces the concepts of 2pc, 3pc and TCC. If there are any mistakes, please correct them gently. Distributed transaction has always been a hot topic in the interview, and it is also a necessary knowledge point for advanced Senior Java engineers.

Recommended Today

Third party calls wechat payment interface

Step one: preparation 1. Wechat payment interface can only be called if the developer qualification has been authenticated on wechat open platform, so the first thing is to authenticate. It’s very simple, but wechat will charge 300 yuan for audit 2. Set payment directory Login wechat payment merchant platform( pay.weixin.qq . com) — > Product […]