One minute to understand two-phase commit 2PC

First, the concept


Two-phase commit protocol 2PC (Two phase Commit) refers, in a distributed system, in order to ensure consistency of an algorithm when all nodes during the transaction commits.

 

Second, background


In a distributed system, each node can be aware of their own success or failure of the operation, but can not know success or failure of the other node operations.

When a transaction across multiple nodes, in order to maintain consistency with atomic transactions, the need to introduce a coordinator (Coordinator) to unify control of all participants (Participant) operating results and operating results should indicate whether they are genuine submission (commit) or rollback (rollback).

 

Third, the idea


2PC name implies into two phases , the idea can be summarized as embodiments thereof:

(1) the voting phase (voting phase): Participants will notify the results of operations coordinator;

(2) commit phase (commit phase): After receiving notice of the participants, the participants issued a notice coordinator again, each participant decide whether to commit or rollback based on feedback;

 

Fourth, the defect


Execution algorithm, all nodes are in the blocked state resources held by all the nodes (such as database data, local files, etc.) are in a lockdown.

A typical scenario is:

Before (1) one of the participants issued a circular to all participants and coordinator of all in the blocked state;

(2) Prior notification coordinator, all participants are in a blocked state;

In addition, if a coordinator or a participant crash occurred, in order to avoid the whole algorithm in a state of complete obstruction, often require time-out mechanism to the algorithm continues to move forward, so the efficiency of the algorithm is relatively low at this time.

Overall, 2PC is a more conservative algorithm .

 

Fifth, for example


ABC D four to organize a conference, meeting time determined, we may assume that A coordinator, B Bingding participants.

Voting Phase:

(1) A probucol email to B, if the peripheral eight p.m. meeting time;

(2) A reply time;

(3) B reply time;

(4) propionate delay in reply, this time for this event, B and C are in blocking state, the algorithm can not continue;

(5) propan reply time (or no time);

Commit phase:

Results (1) A coordinator collected feedback to Bingding B (feedback when, and how the result of feedback, depends on time and decisions of the prop in this example);

(2) B received;

(3) propan received;

(4) received butoxy;

 

CONCLUSIONS


2PC inefficient, distributed transactions is difficult to do.

 

END

Published 35 original articles · won praise 17 · views 30000 +

Guess you like

Origin blog.csdn.net/dev666/article/details/104211083