With the popularity of microservice architecture, a large-scale business system is often composed of several subsystems, and these subsystems have their own independent databases. Often a business process needs to be completed by multiple subsystems, and these operations may need to be completed in one transaction. In microservice systems, these business scenarios are ubiquitous. At this time, we need to implement cross-database transaction support through some means on the database, which is what everyone often calls "distributed transaction".
What the CAP theory says is: in a distributed system, only two of the requirements of C, A, and P can be met at most.
2.1, the meaning of CAP
C: Consistency consistency
Whether multiple copies of the same data are the same in real time.
Availability: within a certain period of time & when the system returns a clear result, the system is called available.
P: Partition tolerance
Distribute the same service in multiple systems to ensure that one system is down and there are still other systems that provide the same service.
2PC: It is called a two-phase commit protocol. One of the difficulties of distributed systems is how to ensure that multiple nodes under the architecture maintain consistency when performing transactional operations. To achieve this goal, the establishment of the two-phase submission algorithm is based on the following assumptions:
When the corresponding messages obtained by the coordinator node from all participant nodes are "agree":
If the response message returned by any participant node in the first stage is "abort":
Three-phase commit agreement
The three-phase submission has three stages: CanCommit, PreCommit, and DoCommit.
Try Confirm Cancel
This process does not execute the business, but completes the consistency check of all the businesses, and reserves all the resources required for execution
This process really starts to execute the business. Since the consistency check has been completed in the Try phase, this process is executed directly without any checks. And in the process of execution, the business resources reserved in the Try phase will be used.
If the business execution fails, it will enter the Cancel phase, which will release all occupied business resources and roll back the operations performed in the Confirm phase.
Code: https://github.com/yu199195/hmily Video: https://www.iqiyi.com/w_19rwkrfu69.html
Jta: java Transaction api
Message queue: delayed, repeated consumption