Two-phase commit agreement

Source: Internet
Author: User

Another important difference between centralized and distributed transactions is that they each need to focus on the wrong attributes. In a centralized system, the error is either good or all wrong (all-or-nothing), which means that either the system is working properly, or the system fails and there is no transaction to complete. However, in a distributed system, there may be partial failures (partial failures) where some nodes are working but some other nodes are faulted.

This local failure is the root cause of many difficult problems in distributed systems. There is such a difficult problem in transaction processing, that is, the consistency of distributed transactions. For example, T may commit on some nodes, and abort on some nodes, resulting in inconsistencies.

A two-phase commit (two-phase commit/2pc) is an algorithm (algorithm) designed to enable all nodes in a distributed system architecture to maintain consistency when committing transactions.
In a distributed system, each node, while aware of its own operation, succeeds or fails, does not know the success or failure of the operation of the other node. So when a transaction spans multiple nodes, you need to introduce a component that acts as a coordinator to unify the results of all nodes (called contributors) and ultimately indicate whether they want to commit the results of the operation (such as writing updated data to disk, etc.).
Therefore, the two-stage algorithm can be summarized as follows: The participants will notify the coordinator of the success or failure of the operation, and then the coordinator based on the feedback from all participants to determine whether the participants to commit or abort the operation.

The two phases of 2PC refer to the voting phase and the decision stage:
Phase I (Submission request phase/voting 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.
Simply put, it is the participants voting whether to continue the next commit operation.

Phase II (submission of the implementation phase/decision Phase):
1) Success
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 finish message to the coordinator node.
The coordinator node completes the transaction after the "complete" message is received by all participant nodes.
2) Failure
If either participant node returns a response message of "Terminate" in the first stage, or the coordinator node cannot get the response message 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 complete message to the coordinator node.
The coordinator node cancels the transaction after the rollback complete message is fed back by all participant nodes.

Note that in the case of failure, there is a situation that is timed out because the message may not arrive because of a failure. Therefore, the process may wait indefinitely. To avoid this problem, you need to use a timeout mechanism.
Let's give a real-life example:
You are a big foodie, found a good place to eat dinner, want to call the best friends to the early adopters. So, you send them a mass text message: "Dear, I sent a good place, and now come out to gather it, the address is xxxxxx", received the information of the small partners, on the road to find the way past, while walking back to the road (write a log), to the text to you, "I arrived", and find a half-day not find the small partners to send text messages to you I can't find the ground, I won't go "(voting stage)
If you receive the information of all the small partners, and send messages to them, "I ordered the Purple Orchid Hall, you Go", the small partners to stop the car (release resources), came to the Purple Orchid Hall, and send you a message "I Come", you see everyone to, obediently order to go.
If you receive a text message that someone can't come to, or someone who hasn't returned a text message for one hours (shutting down or a telecom malfunction), you decide to cancel the dinner and send a text message to everyone saying, "Little friends, someone can't come, you go back." So, everyone down the road to go home (roll back), back home, the car parked good (release resources), send text messages to you, "I arrived", you receive the text message, well, you can go to sleep.

Summary:
2PC is a strong consistency algorithm in distributed case
 
The coordinator has failed. The coordinator needs to log the transaction-related information to the Operation log and synchronize it to the alternate coordinator, and if the coordinator fails, the alternate coordinator can take over the subsequent work. If there is no alternate coordinator, the Coordinator has a permanent failure, and the transaction participants will not be able to complete the transaction and wait.

The algorithm throughput is low, the two-phase commit protocol is the blocking protocol, the execution process needs to lock up other updates, and can not fault tolerance, most distributed storage systems are at a distance, abandon the support of distributed transactions.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Two-phase commit agreement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.