Distributed conformance algorithms 2PC and 3PC

Source: Internet
Author: User
Tags ack

In order to solve the distributed consistency problem, many classical distributed consistency algorithms are produced, and the 2PC and 3PC are introduced in this paper. 2PC is two-phase commit, translated into two-phase commit protocol. 3PC is Three-phase commit, translated into a three-phase commit agreement.
?

Distributed systems and distributed consistency issues

?
A distributed system, which is a hardware and software system running on several different networked computers, and communicates and coordinates only through message delivery.
Distributed consistency issues, the question of how to agree on a resolution between nodes that are independent of each other.
?

2PC

?
2PC, two phase commit protocol, the commit process of the transaction is divided into two stages for processing: the preparation phase and the commit phase. The initiator of the transaction is called the coordinator, the performer of the business is called the participant.
?
  Phase 1: Preparation phase
1. The coordinator sends transactional content to all participants, asking if the transaction can be submitted and waiting for all participants to reply.
2. Each participant performs a transactional operation by logging the undo and redo information into the transaction log (but not committing the transaction).
3. If the participant succeeds, give the coordinator feedback Yes, that is, it can be submitted; if execution fails, feedback to the Coordinator no, that is, cannot be submitted.
?
  Phase 2: Commit phase
?
This stage can be divided into two situations: All participants are feedback Yes, or any one participant feedback No.
When all participants have feedback Yes, the transaction is committed.
When any one participant feedback no, the transaction is interrupted.
?
Commit TRANSACTION: (all participants have feedback Yes)
1. The facilitator issues a request for formal submission of the transaction to all participants (i.e., a commit request).
2. The participant executes the commit request and frees the resources occupied during the entire transaction.
3, each participant feedback to the Coordinator ACK completed message.
4. When the coordinator receives an ACK message from all participants, it completes the transaction submission.
?
Attached as follows:
?

?
Interrupt transaction: (any one participant feedback no)
1. The facilitator issues a rollback request (that is, the rollback request) to all participants.
2. Participants use the Undo information in Phase 1 to perform a rollback operation and to release resources that were consumed during the entire transaction.
3, each participant feedback to the Coordinator ACK completed message.
4. When the coordinator receives an ACK message from all participants, it completes the transaction interruption.
?
Attached as follows:
?

?

Defects in 2PC

?
1, synchronous blocking: The biggest problem is synchronous blocking, that is, all the logic involved in the transaction is in a blocking state.
2, Single point: The coordinator has a single point of issue, if the coordinator fails, participants will remain locked.
3. Brain fissure: in Phase 2, if only some of the participants receive and execute a commit request, the node data will be inconsistent.
?
Because of the 2PC existence such as upper synchronous blocking, single point, brain crack problem, so there is a 2PC improvement program, that is, 3PC.
?

3PC

?
3PC, three-phase commit protocol, is an improved version of 2PC, will be the transaction submission process is divided into Cancommit, Precommit, do commit three stages to deal with.
?
   Stage 1:cancommit
1. The facilitator issues a CANCOMMIT request containing transactional content to all participants, asking if the transaction can be committed and waiting for all participants to reply.
2, if the participant receives the CANCOMMIT request, if it thinks can perform the transaction operation, the feedback Yes and enters the preparatory state, otherwise feedback No.
?
   Stage 2:precommit
?
This phase is divided into two scenarios:
1, all participants feedback yes, that is, to perform a transaction pre-submission.
2, any one participant feedback no, or wait timeout after the coordinator has not yet received feedback from all participants, that is, the interruption of the transaction.
?
Transaction Pre-commit: (When all participants have feedback Yes)
1. The facilitator sends a PRECOMMIT request to all participants and enters the preparatory stage.
2. After the participant receives the PRECOMMIT request, the transaction operation is performed, and the undo and redo information is logged in the transaction log (but the transaction is not committed).
3, each participant feedback the ACK response or no response to the coordinator, and wait for the final instruction.
?
Interrupt transaction: (any one participant feedback no, or wait timeout after the coordinator has not yet received feedback from all participants)
1. The coordinator sends an abort request to all participants.
2. The participant interrupts the transaction regardless of the abort request issued by the Coordinator, or when a timeout occurs while waiting for the coordinator to request it.
?
   phase 3:do Commit
?
There are two scenarios for this phase:
1. All participants are feedback ACK response, that is, to perform a real transaction commit.
2, any one participant feedback no, or wait timeout after the coordinator has not yet received feedback from all participants, that is, the interruption of the transaction.
?
Commit TRANSACTION: (when all participants have feedback ack response)
1. If the coordinator is in a working state, a do commit request is issued to all participants.
2. When a participant receives a do commit request, it formally executes the transaction commit and releases the resources that were consumed during the entire transaction.
3, each participant feedback to the Coordinator ACK completed message.
4. When the coordinator receives an ACK message from all participants, it completes the transaction submission.
?
Interrupt transaction: (any one participant feedback no, or wait timeout after the coordinator has not yet received feedback from all participants)
1. If the coordinator is working, send an abort request to all participants.
2. Participants use the Undo information in Phase 1 to perform a rollback operation and to release resources that were consumed during the entire transaction.
3, each participant feedback to the Coordinator ACK completed message.
4. When the coordinator receives an ACK message from all participants, it completes the transaction interruption.
?
Note: After entering phase three, no matter the coordinator or the Coordinator and the participant network, the participant cannot receive a do commit request or abort request issued by the Coordinator. At this point, the participant resumes the transaction commit after waiting for a timeout.
?
Attached as follows:
?

?

Advantages and disadvantages of 3PC

?
Pros: Reduces the blocking range, and the coordinator or participant interrupts the transaction after the wait time-out. Avoids a single point of contact for the facilitator, and the participant continues to commit the transaction when there is a problem with the coordinator in Phase 3.
?
Defect: The problem of brain fissure persists, that is, when the participant receives the PRECOMMIT request and waits for the final instruction, if the coordinator cannot communicate with the participant normally, it will cause the participant to continue committing the transaction, resulting in inconsistent data.
?

Postscript

?
No matter 2PC or 3PC, the distributed consistency problem cannot be completely resolved.
To solve the problem of consistency, only Paxos, the follow-up will be summarized separately.

Distributed conformance algorithms 2PC and 3PC

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.