Hello everyone, I am a trainee who has been training for N and a half years. Food package. Like.. xx.. xx.. xx.. Basketball. Long training, feel the need to write something, welcome correction, exchange.

An overview of the

TCC is a compensation transaction. The core idea is that for each operation, a corresponding acknowledgement and compensation (undo) operation is registered. TCC is an application – layer, manual – compensated transaction.

Operation process

  • In the Try phase, service systems are tested and resources are reserved.
  • The Confirm phase is used to Confirm the submission of the service system. If the Try phase succeeds and the Confirm phase starts, no error occurs during the Confirm phase by default. If Try is successful, Confirm is successful. If Confirm fails, retry until Confirm succeeds.
  • In the Cancel phase, services that need to be rolled back are cancelled and reserved resources are released.

example

Next door Lao Wang transfers money to his son, Lao Wang is the card of agricultural bank, the son is the card of industrial bank. Suppose that Lao Wang has 10000 pieces in his current card and wants to transfer 3000 pieces to his son, who has 2000 pieces in his current card.

TCC is ideal for success

  1. Agricultural Bank of China checks the status of Lao Wang’s account, deducts 3000 yuan from 10000 yuan in Lao Wang’s card, leaving 7000 yuan, and adds 3000 yuan to Lao Wang’s pre-transfer account. (Try)
  2. Icbc checked the status of the son’s account, and then added 3000 yuan to the son’s pre-transfer account. (Try)
  3. All the above steps were successful. Agricultural Bank of China reduced Lao Wang’s pre-transfer account by 3000 yuan, while ICBC reduced his son’s pre-transfer account by 3000 yuan and increased his son’s credit card by 3000 yuan. (Confirm)
class Transfer{
	String transfer(a){
		// ABC pre-roll out
		nonghang.tryReduce(3000);
		// Icbc pre-roll
		gonghang.tryAdd(3000);
		// Agricultural Bank of China confirms the transfer out
		nonghang.confirmReduce(3000);
		// ICBC confirms the transfer
		gonghang.confirmAdd(3000);
		return "success"; }}Copy the code

TCC failure condition — The Try phase fails

  1. Agricultural Bank of China checks the status of Lao Wang’s account, deducts 3000 yuan from 10000 yuan in Lao Wang’s card, leaving 7000 yuan, and adds 3000 yuan to Lao Wang’s pre-transfer account. (Try)
  2. Icbc checked the status of the son’s account and found that there were problems in the son’s account and the amount transferred failed. (Try)
  3. Son icbc card Try stage failure, then Lao Wang agricultural Bank card need to subtract the deduction account of 3000 pieces, add 3000 pieces to the card. (Cancel)
  4. If Cancel in step 3 fails, you need to retry.
class Transfer{
	String transfer(a){
		// ABC pre-roll out
		boolean nhTrySuccess = nonghang.tryReduce(3000);
		if(! nhTrySuccess) {return "fail";
		}
		// Icbc pre-roll
		boolean ghTrySuccess = gonghang.tryAdd(3000);;
		if(! ghTrySuccess) {boolean nhCancelSuccess = false;
			while(! nhCancelSuccess){// AgBank cancels roll-out
				nhCancelSuccess = nonghang.cancelReduce(3000);
			}
			return "fail";
		}
		// Agricultural Bank of China confirms the transfer out
		nonghang.confirmReduce(3000);
		// ICBC confirms the transfer
		gonghang.confirmAdd(3000);
		return "success"; }}Copy the code

TCC failure – Confirm phase fails

  1. Agricultural Bank of China checks the status of Lao Wang’s account, deducts 3000 yuan from 10000 yuan in Lao Wang’s card, leaving 7000 yuan, and adds 3000 yuan to Lao Wang’s pre-transfer account. (Try)
  2. Icbc checked the status of the son’s account, and then added 3000 yuan to the son’s pre-transfer account. (Try)
  3. All the above steps are successful, agricultural Bank of China reduces Lao Wang’s pre-transfer account by 3000 pieces. At this time, ICBC’s server is suspended, and 3000 pieces are not added to his son’s card. So we think the transaction is successful, but we need to keep trying again to inform ICBC to reduce the pre-transfer account of his son by 3000 pieces and add 3000 pieces to his card.
class Transfer{
	String transfer(a){
		// ABC pre-roll out
		boolean nhTrySuccess = nonghang.tryReduce(3000);
		if(! nhTrySuccess) {return "fail";
		}
		// Icbc pre-roll
		boolean ghTrySuccess = gonghang.tryAdd(3000);;
		if(! ghTrySuccess) {boolean nhCancelSuccess = false;
			while(! nhCancelSuccess){// AgBank cancels roll-out
				nhCancelSuccess = nonghang.cancelReduce(3000);
			}
			return "fail";
		}
		// Agricultural Bank of China confirms the transfer out
		boolean nhConfirmSuccess = false;
		while(! nhConfirmSuccess){ nhConfirmSuccess = nonghang.confirmReduce(3000);
		}
		// ICBC confirms the transfer
		boolean ghConfirmSuccess = false;
		while(! ghConfirmSuccess){ ghConfirmSuccess = gonghang.confirmAdd(3000);
		}
		return "success"; }}Copy the code

conclusion

What is the difference between TCC transactions and 2PC transactions?

It is not difficult to see from the pseudo-code that TCC actually moves the submission and rollback mode of transaction to the application code layer. It divides a large distributed transaction of 2PC into several small local transactions and completes the whole transaction life cycle through coding. Reduces long locking of related resources and improves availability.

What are the flaws in the TCC implementation of pseudocode?

  • It’s a big intrusion into real business code, which only involves 4 lines of code, and the rest is control over compensation and validation processes. It is difficult to determine how much code is required for more complex scenarios.
  • Idempotency check must be performed for Confirm and Cancel interface methods to ensure data consistency.

Areas for improvement

It can abstract out the core idea of the non-business code that is invaded, that is, the compensation method, and form a component similar to the coordinator in 2PC to control the whole compensation process and reduce the code intrusion as much as possible.

The framework of TCC’s core ideas

  • Hmily (simple source code, easy to read)