2

"How to Fix Suspension Problems in TCC"!

A Java programmer who has worked for 4 years, went to JD.com for an interview and was asked this question.

Hello everyone, I'm Mic, a Java programmer who has been working for 14 years

What is the interviewer looking for in this question? How should we answer?

Problem Analysis

TCC is a solution to distributed transaction problems, and is generally asked more when applying for Internet companies.

In fact, in the transaction solution of TCC, in addition to the hanging problem, there are also empty rollbacks and idempotency to be considered.

But we use some mature frameworks, such as Seata, when we apply them. These frameworks themselves help us solve it.

As a result, most people do not know the meaning of this question.

The so-called TCC is actually (Try-Confirm-Cancel), which is to split a transaction into two phases, similar to the traditional XA transaction model.

The Try phase is to implement business checks and reserve necessary business resources.

Confirm, to actually execute the business logic, only needs to use the business resources reserved in the try phase for processing.

Cancel, if the transaction execution fails, the resources reserved in the try phase are released through the cancel method.

image-20220803203513145

In the TCC transaction mode, we manage multiple transactions through a transaction coordinator, and each transaction executes the try method first.

When the try method of all transaction participants is successfully executed, the confirm method is executed to complete the execution of the real logic. Once any transaction participant has an exception, the transaction rollback is triggered through the cancel interface to release the resources occupied by the Try phase.

img

Obviously, this is an implementation of eventual consistency, so when Try is executed successfully, it must be ensured that Confirm is executed successfully.

When the Try execution fails, it must be ensured that Cancel achieves resource release.

In the interview question, the hanging problem is mentioned, which means that when the TCC executes the Try interface and the network times out, the TCC triggers the Cancel interface to roll back. The interface executes before the Try interface.

As a result, the resources reserved by the Try interface cannot be released. This situation is called suspension.

That's the background of the TCC suspension issue, and it's really a must-know detail for every full-fledged advanced developer.

Because it may cause more serious production accidents.

After understanding the background, how should we solve it? Let's take a look at the expert's answer below.

Expert:

For the suspension problem, I think it is only necessary to ensure that the Try interface is not allowed to be executed after the Cancel interface is executed.

Therefore, in the Try interface, we can first determine whether the Cancel interface has been executed. If it has been executed, it will not be executed.

Whether the judgment has been executed, a transaction control record can be inserted into the transaction control table to mark the rollback status of the transaction.

Then in the Try interface, you only need to read this state to judge.

Summarize

Well, today's sharing ends here.

If you like my work, remember to like, favorite, and follow! ! !

file

Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source for Mic带你学架构 !
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!

跟着Mic学架构
810 声望1.1k 粉丝

《Spring Cloud Alibaba 微服务原理与实战》、《Java并发编程深度理解及实战》作者。 咕泡教育联合创始人,12年开发架构经验,对分布式微服务、高并发领域有非常丰富的实战经验。