The coder is on the ropes

I know you are depressed, struggling in your heart, and the gap between ideal and reality, but I can’t bear it for you. You need to experience, think, understand, summarize, and move on.

preface

We say in front of the two stage distributed transaction model, respectively is 2 PCS and 3 PC, 2 PC model of its efficiency is lower, and there will be a transaction block, so the introduction of the 3 PCS model, 3 PCS model on the basis of 2 PC model is improved, avoid the problem of the transaction, but for 2 PC and 3 PC models, they are still blocked, That is to say the current affairs in the process of execution, other issues will be blocked, so, in fact, their efficiency is not high, if for the concurrency value not hair of the system, you can choose them, but under the scenario of high concurrency, with 2 PC model and 3 PCS model, obviously not, so today we introduce the model of TCC.

TCC explain

T is a Try, the first C is Confirm, and the second C is Concel. Engineers need to code these three processes themselves, so TCC has a great intrusion on business, and its function is to help us coordinate. However, the actual logic needs to be encoded by ourselves, but TCC is more efficient and does not have transaction blocking, which is suitable for high concurrency scenarios.

In-depth analysis

We use an ordering process to illustrateTCCThe normal order will include the functions of order creation, inventory reduction, account balance deduction, points increase and so on. Therefore, these subsystems will be called. Some systems use HTTP to make remote calls, while others use RPC to make callsBusiness MicroServices.StockMicroservice.OrderMicroservice.IntegralMicroservice.AccountMicroserviceAnd call other microservices uniformly through RPC in business microservices.

To use TCC, you need to design three parts of the business code, namely Try stage, Confirm stage, Concel stage.

Try

We can see from the literal meaningTryIt means to try. In this stage, resources are reserved and engineers need to design by themselvesTryThe quality of stage code design, to a large extent, affects the results of the whole distributed transaction, so the design ability and thinking ability of engineers have a certain test.

  • Withholding inventory

Doesn’t really deduct inventory in this step, it is called a preliminary deduction inventory, first to check whether the inventory deductions, such as I order quantity to 2, the inventory is 1, so obviously can’t deduct, if the stock is 10, so that can be deducted, but does not really deduct inventory at this time, we need to design, That need to be designed according to your own business scenarios, such as freezing can create a new inventory table, because the order quantity to 2, inventory is 10, so when performed 10-2, inventory of 8, we will update inventory of 8, and then inserted into a table in inventory freeze deductions record, record is a user, the number of orders, then deduct inventory this step is completed.

Pre-created orders do not actually create orders, we can change the state of the order to Create. This state value is only used to represent the state of the order. This state is not the state of the real order, but is used for the use of distributed transactions, and is not an attribute in the life cycle of the goods.

  • Pre-increment integral

Instead of actually increasing the score, we can add a frozen score field to the score record. For example, if the score balance is 100 and 20 points need to be increased, then the balance value becomes 120(100+20) and the frozen score is 20.

Deduct balance We add a freeze field to the account table, for example, if the account balance is 1000 and 200 needs to be deducted this time, then the balance becomes 800(1000-200) and the frozen amount is 200.

So here, we’re done with the Try phase, and what we found is that what we’re doing in the Try phase is we’re reserving resources.

Confirm

If the Try stage all business is done successfully, there are no errors, then in Confirm stage will perform all branch, at this stage the only do is to commit the transaction (completed our own defined logic), can’t again to check the data, such as whether inventory sufficient, because the first phase has been examined and approved.

Because the inventory in the inventory table has been reduced in the Try stage, only the deduction record is saved in the inventory frozen table, so this step needs to delete the records of the frozen table through the user Id.

  • Set the order status to Created

The Try phase sets the order state to Create, at which point you need to set the order state to Created to indicate that the order transaction has completed.

The increment of the integral is already done in the Try phase, just a frozen integral is reserved, so we need to update the frozen integral, update it to zero, which means that the increment of the integral is done.

  • Deduct the balance

The deduction balance is already done in the Try phase, but only a frozen amount is reserved, so the frozen amount needs to be updated to 0, indicating that the deduction balance transaction has completed.

At this point, THE Confirm of TCC is completed. The only thing to do in the Confirm stage is to execute the task without any data verification.

Cancel

In the Confirm phase, all operations in the Try phase are normal without errors. If an operation is abnormal or a resource error occurs, the operation will enter the Cancel phase. The Cancel phase will roll back all operations in the Try phase, that is, restore data to the original stage.

  • Restore the inventory

Restore inventory is to query the frozen inventory in the inventory frozen table, and then add the inventory in the inventory table (inventory table inventory = inventory table inventory + frozen inventory), 8 + 2 = 10, and then delete the frozen inventory record, on behalf of the transaction rollback success.

TryThe stage order status is being created, then because inTryPhase one of the branch transactions failed, so the order state needs to be set to Cancelled (this state is not in the order life cycle), but is designed for the transaction, indicating that the transaction was successfully rolled back.

  • Restore the integral

Add the frozen integral plus the integral balance (the integral balance = the integral balance + the frozen integral), then update the frozen integral to the integral balance, and then update the frozen integral to 0, representing the transaction rollback success.

Use the frozen balance plus the frozen balance (balance = balance + frozen balance) to update the frozen balance to 0, indicating that the transaction is successfully rolled back.

This completes the Cancel phase, which is used to restore individual transactions based on data from the Try phase.

conclusion

Completed the analysis of the TCC, we can see that there is no obstruction between the TCC affairs, but the transaction was in large part to the success of the master in the developer’s hand, because it is not like 2 PC model framework is entirely by the framework to help us to complete the transaction commit and rollback, in TCC mode, transaction commit rollback are by us to write code to implement the business, What TCC helps us do is to schedule tasks, perceive whether branch transactions are normal, and then commit or roll back according to the results. Therefore, the quality of our code directly affects whether TCC transactions are successful.

In the Try phase, if all branches are normal, the transaction will almost always succeed in the Confirm phase. If the Try phase fails, the transaction will almost always succeed in the Cancel phase. If the Confirm phase and Cancel fail, the TCC framework either retries or manual processing is required, so data consistency is not completely guaranteed.

That’s all for today. Thanks for watching, and I’ll see you next time.