Analysis & Answer
What are distributed transactions
- Distributed transaction means that transaction participants, transaction supporting servers, resource servers, and transaction managers are located on different nodes of different distributed systems.
- The key to distributed transaction processing is that there must be a way to know everything a transaction does anywhere, and committing or rolling back a transaction must produce consistent results (all committed or all rolled back)
Common distributed transaction solutions
1. Two-phase commit based on XA protocol (strong consistency)
XA is a two-phase commit protocol defined by X/Open DTP organization. XA model includes four parts: application program (AP), transaction manager (TM), Resource Manager (RM) and Communication Resource Manager (CRM). As the global scheduler, the transaction manager is responsible for committing and rolling back individual local resources. XA implements distributed transactions as follows:
XA pros and cons
- Low efficiency, the cost of the preparation phase is persistent, the cost of the global transaction state is persistent, and the performance of the local transaction is about 10 times different;
- Failure recovery and isolation problems occurred prior to submission.
- Acts on the resource layer with little intrusion into the business
2. Message transaction + final consistency
The so-called message transaction is A two-phase commit based on message middleware, which separates A distributed transaction into A message transaction (local operation of system A + sending message) + local operation of system B. A Message transactions in the system guarantee that either local operations and outbound messages will succeed, or both will fail. The operation of system B is message-driven. If the local operation of SYSTEM B fails, the message will be redelivered until the operation of system B succeeds. In this way, the distributed transaction between A and B is realized in A disguised way. The principle is as follows:
Pros and cons of message transactions
- If B keeps executing without success, consistency is broken.
- Message-oriented two-phase commit is often used in high-concurrency scenarios.
3. TCC compensation mode + final consistency
The so-called TCC programming pattern is also a variant of two-phase commit. TCC provides a programming framework that divides the entire business logic into three parts: Try, Confirm, and Cancel operations.
- A) In the Try stage, it mainly detects and reserves resources for the service system. This stage is mainly completed:
- Complete all business checks (consistency).
- Reserve required service resources (quasi-isolation).
- Try Attempts to execute the service.
- B) The Confirm stage is mainly to Confirm and submit the business system. If the Try phase succeeds and the Confirm phase starts, the Confirm phase does not fail by default. If the Try succeeds, Confirm succeeds.
- C) In the Cancel phase, services that need to be rolled back due to errors in service execution are cancelled and reserved resources are released.
Taking online orders as an example, inventory will be deducted in the Try stage, and order status will be updated in the Confirm stage. If the order update fails, inventory will be restored in the Cancel stage.
TCC pros and cons
- The recognition and compensation logic and idempotent support are artificially realized by code, which can not be reused well. The business coupling degree is high, which increases the development cost.
- Performance improvement: The granularity of resource lock control becomes smaller for specific services and the entire resource is not locked.
- Final data consistency: Based on the idempotency of Confirm and Cancel, the transaction is confirmed or cancelled to ensure data consistency.
- Reliability: the single point of failure of XA protocol coordinator is solved, and the whole business activity is initiated and controlled by the main business party. The business activity manager is also changed into multi-point and introduced into cluster.
Reflection & Expansion
TCC distributed transaction solution is suitable for fixed execution time and short business, such as the three core services of Internet financial enterprises: transaction, payment and accounting.
Which way to go ultimately depends on the business scenario. For different business technology selection is also a very important ability!
Expand the thinking
- What is the difference between XA transactions and TCC?
- If you could optimize XA, how would you optimize it?
- What is TCC and how does it work?
Brush interview: one-stop solution to interview problems, if you have good interview knowledge or skills to share!