A transaction is the process of a conversation , The impact on context is consistent , Or all the changes have been made , Or all the changes are removed . It's either birth , Or die . There is no middle unpredictability .
Schrodinger's cat . Transaction is to ensure the integrity and accuracy of business data .
Distributed transaction , The two common methods are two-stage submission and compensation .
The typical two-stage submission is XA, There is a transaction coordinator , Tell you all , All ready to submit , Reply , It's all ready , And then the coordinator tells you , Submit together , Everyone submitted it .
Compensation is easy to understand , Deal with the business first , And then it's timed or called back , Check that the status is consistent , If not, adopt a strategy , Force state to an end state （ It is generally a failure state ）, Then the world is at peace . The typical one is the punch operation .
When you're ready , If there is no problem , Received submission , Everyone started to submit .
This is the time , For example, for databases , Yes redo Journal .
If a database goes down at this time , So when it restarts , Check first , They will also submit the last operation . So the data from each point are consistent .
problem 1： such as Many services that a business needs to call are write operations , If one of the written services fails , What should I do? ? hypothesis 4 Let's write it , Yes 2 A write failed .
Taobao and other websites generally do this , If 4 Every success is success , Then when submitting this time 4 All writes are set to an intermediate state , The introduction may be inconsistent . then 4 After the execution is completed , Check this in a callback or scheduled task 4 Are the data consistent , If they are consistent, they are all set to success , If they are inconsistent, they are all set to fail .
In the complex business interaction process , Strongly consistent distributed transactions are not recommended . The best way to solve distributed transaction is not to consider distributed transaction . Just like the question , The distributed transaction process is decomposed into several intermediate states , Things in intermediate state are not allowed to be operated by users directly , Wait for the same state to succeed , Or it fails when it detects inconsistencies . It decouples the process of strong consistency .
In general, quasi real-time is a good way . It's about money , It can be done sometimes .
Taobao table s Complete one order processing , It's not a problem, is it .
Banks are not all strongly consistent . It's also bad , It will also correct .
Especially when multiple systems are involved , We buy air tickets, for example , After payment , Payment completion status only , And then back to the user , We'll refresh the page in a few minutes , Will see that the ticket has been issued , Order completion status .
This is the time , If we ask for all the processing , They are all strongly consistent , It's been a long time . The page will die there for a few minutes , To complete the transaction , Return to user .
There must be a problem , Paid , But the ticket didn't come out in the end . Then there's no way , Discuss exchange or refund .
Failed to change Taobao's order to invoice , Send a message to the payment notifying the refund .
When it's slow , It is possible that the tickets are issued by hand , At this time, a ticket can be issued for half an hour , If the requirements must be strong consistency , Where do all processing threads hang , The system is long gone .
The best way to solve distributed transaction is not to consider distributed transaction . split , Big business process , Into a few small business processes , Then consider the final consistency .
problem 2： Distributed transactions are developed by you , Or the database comes with it ? kimmking：
1, As long as a processing logic can guarantee or succeed , Or it's like doing nothing , It's business . Database transactions ,MQ There are also affairs .
You can even write a program to generate two files , Either it's all generated , Or they're all deleted without leaving a trace , This is a business .
2, Distributed transactions have a XA standard , realization XA Interface transactions , You can join a distributed transaction , cover XA Container management .
3, Compensation methods , Specific analysis is needed , There is no framework for all kinds of situations .