Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Distributed consistency algorithms 2PC and 3PC

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

In order to solve the distributed consistency problem, there are many classical distributed consistency algorithms. This paper will introduce 2PC and 3PC among them. PC stands for Two-Phase Commit, translated as Two-Phase Commit Protocol. PC stands for Three-Phase Commit.

Distributed Systems and Distributed Consistency Problems

Distributed systems are software and hardware systems that run on multiple different networked computers and communicate and coordinate only through message passing.

Distributed consensus problem is the problem of how independent nodes agree on a resolution.

2PC

2PC, two-phase commit protocol, divides the commit process of transactions into two phases: preparation phase and commit phase. The initiator of the transaction is called coordinator, and the executor of the transaction is called participant.

Phase 1: Preparation phase

The coordinator sends the transaction content to all participants, asking if the transaction can be submitted, and waits for all participants to reply.

2. Each participant performs the transaction operation and records the Undo and Redo information in the transaction log (but does not commit the transaction).

3. If the participant succeeds in execution, feedback YES to the coordinator, that is, it can be submitted; if the execution fails, feedback NO to the coordinator, that is, it cannot be submitted.

Phase 2: Submission phase

There were two scenarios: all participants responded YES, or any participant responded no.

When all participants respond YES, the transaction is committed.

When any one of the participants responds NO, the transaction is interrupted.

Submit transaction: (all participants responded YES)

The coordinator issues a formal commit request to all participants (i.e., a Commit request).

Participants execute the Commit request and release the resources occupied during the entire transaction.

3. Each participant feeds back an Ack completion message to the coordinator.

4. After the coordinator receives the Ack message from all participants, the transaction submission is completed.

The following schematic diagram is attached:

Interrupted transaction: (any participant feedback NO)

The coordinator issues a rollback request (i.e., a Rollback request) to all participants.

Participants use the Undo information from Phase 1 to perform a rollback operation and release resources consumed during the entire transaction.

3. Each participant feeds back an Ack completion message to the coordinator.

4. After the coordinator receives the Ack message from all participants, the transaction interrupt is completed.

The following schematic diagram is attached:

2PC defects

Synchronous blocking: The biggest problem is synchronous blocking, that is, all logic involved in the transaction is in a blocking state.

Single point: The coordinator has a single point problem, and if the coordinator fails, the participant will always be locked.

Split brain: In phase 2, if only some participants receive and execute the Commit request, it will lead to inconsistent node data.

Because 2PC has the problems of synchronous block, single point and brain split, an improved scheme of 2PC, i.e. 3PC, appears.

3PC

3PC, a three-phase commit protocol, is an improved version of 2PC, that is, the transaction commit process is divided into three phases: CanCommit, PreCommit, and do Commit.

Stage 1: CanCommit

The coordinator sends a CanCommit request containing the transaction content to all participants, asking if the transaction can be committed, and waits for all participants to reply.

After receiving the CanCommit request, if the participant thinks that the transaction operation can be executed, the feedback is YES and enters the preparatory state, otherwise the feedback is NO.

Stage 2: PreCommit

This phase is divided into two situations:

1. All participants feedback YES, i.e. execute transaction pre-commit.

2, any one participant feedback NO, or wait for a timeout coordinator has not received feedback from all participants, that is, interrupt the transaction.

Transaction pre-commit:(when all participants respond YES)

The coordinator sends a PreCommit request to all participants to enter the preparation phase.

After the participant receives the PreCommit request, it performs the transaction operation and records the Undo and Redo information in the transaction log (but does not commit the transaction).

3. Each participant feeds back an Ack response or a No response to the coordinator and waits for a final instruction.

Interrupted transaction: (No feedback from any participant, or wait timeout before coordinator receives feedback from all participants)

The coordinator sends an abort request to all participants.

Participants interrupt transactions regardless of whether they receive an abort request from the coordinator or a timeout occurs while waiting for the coordinator request.

Stage 3: Do Commit

There are also two situations at this stage:

1. All participants feedback Ack response, i.e. perform real transaction commit.

2, any one participant feedback NO, or wait for a timeout coordinator has not received feedback from all participants, that is, interrupt the transaction.

Submit transaction: (when all participants feedback Ack response)

If the coordinator is working, send a do Commit request to all participants.

After the participant receives the do Commit request, it will formally execute the transaction commit and release the resources occupied during the entire transaction.

3. Each participant feeds back an Ack completion message to the coordinator.

4. After the coordinator receives the Ack message from all participants, the transaction submission is completed.

Interrupted transaction: (No feedback from any participant, or wait timeout before coordinator receives feedback from all participants)

If the coordinator is working, send an abort request to all participants.

Participants use the Undo information from Phase 1 to perform a rollback operation and release resources consumed during the entire transaction.

3. Each participant feeds back an Ack completion message to the coordinator.

4. After the coordinator receives the Ack message from all participants, the transaction interrupt is completed.

Note: After entering Phase 3, either problems with the coordinator or problems with the coordinator and participant network will result in participants not receiving do Commit requests or abort requests from the coordinator. At this point, participants will wait for a timeout before proceeding with the transaction commit.

The attached schematic diagram is as follows:

3PC advantages and disadvantages

Pros: Reduced blocking range, coordinator or participant aborts transaction after wait timeout. The coordinator single-point problem is avoided, and participants continue to commit transactions when the coordinator has a problem in phase 3.

Defect: The split brain problem still exists, i.e., waiting for the final instruction after the participant receives the PreCommit request. If the coordinator cannot communicate with the participant properly at this time, it will cause the participant to continue submitting the transaction, resulting in data inconsistency.

PostScript

Neither 2PC nor 3PC completely solves the distributed consistency problem.

To solve the consistency problem, only Paxos will be summarized separately later.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report