What is a business?
A reliable, independent unit of work that consists of a set of operations.
What is acid?
The atomic Operation unit of a (atomicity) transaction, the modification of the data, either all executed or not executed;
C (consistency) at the beginning and completion of the transaction, the data must be in a consistent state, the relevant data rules must be applied to the modification of the transaction to ensure the integrity of the data, the end of the transaction, all the internal data structure must be correct;
I (isolation) guarantees that the transaction is not executed by an independent environment with external concurrency operations;
After the D (persistent) transaction is completed, modifications to the data are permanent and can be maintained even if the system fails;
What is base?
Ba:basic availability basic business availability;
S:soft state flexibility;
E:eventual consistency final consistency;
What's the cap?
C (consistency) consistency refers to the atomic nature of the data, in the classic database through the transaction to ensure that when the transaction is completed, regardless of success or rollback, the data will be in a consistent state, in a distributed environment, consistency refers to multiple node data is consistent;
A (availability) service remains in a usable state, and when a user makes a request, the service can return the result within a certain amount of time;
P (partition tolerance) in distributed applications, it may be because of some distributed causes the system can not function, good partition tolerance, so that the application is a distributed system, but it seems to be a normal operation of the whole;
For any shared system, you can have up to two of these features. Any combination of both has a suitable scenario, and the real system should be a mixture of acid and base.
What's the two-phase commit?
1, the first phase (Submission request phase)
The coordinator node asks all contributor nodes if they can perform the commit operation and begins to wait for the response from each participant node.
The contributor node performs all transaction operations up to the start of the query and writes the undo information and redo information to the log.
Each participant node responds to a query initiated by the coordinator node. If the transaction operation of the contributor node is actually successful, it returns a "consent" message and returns an "abort" message if the transaction operation of the contributor node fails to actually execute. Sometimes the first stage is also called the voting stage, that is, whether the participants vote to continue the next commit operation.
2. Phase II (Submission of implementation phase)
2.1 Successes
When the coordinator node obtains the corresponding message from all participant nodes as "consent":
The coordinator node makes a "formal commit" request to all participant nodes.
The Contributor node formally completes the operation and releases resources that are consumed throughout the transaction.
The contributor node sends a "finished" message to the Coordinator node.
The coordinator node completes the transaction after it has been "finished" with feedback from all participant nodes.
2.2 Failure
When the response message returned by either contributor node in the first stage is abort, or the coordinator node fails to get response messages for all contributor nodes before the first phase of the query times out:
The Coordinator node issues a "rollback operation" request to all contributor nodes.
The contributor node takes advantage of the previously written undo information to perform a rollback and frees the resources that were consumed during the entire transaction period.
The contributor node sends a "rollback" message to the Coordinator node.
The coordinator node cancels the transaction after the "rollback" message is fed back by all participant nodes.
Sometimes the second stage is also called the completion stage, because the coordinator must end the current transaction at this stage, regardless of the outcome.
3, two the shortcomings of the phase submission
The biggest disadvantage of the two-phase commit algorithm is that it is in the middle of execution and the nodes are in a blocking state. That is, when the node waits for the other party's corresponding message, it will not do anything. In particular, when a node is already in possession of a resource, in order to wait for the other node's response message to fall into a blocking state, when the third node tries to access the resources that the node occupies, the node will also be stuck in a blocking state.
In addition, when the coordinator node indicates that the contributor node commits such actions, such as a crash in the participant node, the Coordinator will always be unable to obtain the response information of all participants, which is the Coordinator's own time-out mechanism to take effect. However, when the time-out mechanism comes into effect, the facilitator instructs the participant to roll back the operation. Such a strategy looks more conservative.
The XA protocol for distributed transactions?
The XA protocol is a specification of distributed transaction processing by X/open organization, which mainly defines the interface between transaction manager and local resource manager, and currently the major database vendors such as Oracle and DB2 provide XA support, and the XA protocol uses two-phase commit to manage distributed transactions.
What about local affairs?
A transaction has a resource manager (such as a DBMS) for local administration.
The advantage is that it supports ACID properties, high efficiency, reliability and simple application system programming.
The disadvantage is that the distributed environment is not supported, and the smallest unit of isolation is determined by the resource manager, such as a row of records for the database.
Spring's transaction template (Template method callback)
Best practices
1, based on the final consistency of the message
The message system provides two aspects of support, one is to send a message to provide transaction support, and the other is to send a successful message to ensure successful delivery, so that the use of the message system to achieve final system consistency
2, System power and other operations
Repeated calls result in the same business results as the one that was generated once.
Implementation of one is the business operation itself to achieve power, and the second is the system cache requests and processing results, after detecting duplicate requests, automatically return the previous results.
3. Transaction compensation mechanism
http://iamzhongyong.iteye.com/blog/2076896
Introduction to several concepts about transactions (GO)