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

What are the 2PC and 3PC of the database

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what is the 2PC and 3PC of the database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Database transaction has four characteristics of ACID. In essence, distributed transaction should also meet the basic characteristics of transaction (ACID), but the expression form of distributed transaction is very different from that of local transaction.

One: distributed transaction-- consistency protocol 2PC

2PC (Two-Phase Commit abbreviation) is a two-phase commit protocol, which divides the whole transaction process into two phases: preparation phase (Prepare phase) and commit phase (commit phase). 2 refers to two phases, P refers to preparation phase, and C refers to commit phase. Used to solve the problem of distributed transaction consistency.

The commonly used Oracle and MySQL support 2PC protocol. The two phases of 2PC protocol are as follows:

Preparation phase (Prepare phase): the transaction manager sends Prepare messages to each participant, and each database participant executes the transaction locally and writes a local Undo/Redo log, at which time the transaction is not committed. (the Undo log records the data before modification and is used for database rollback, while the Redo log records the modified data and writes to the data file after the transaction is committed.)

Commit phase (commit phase): if the transaction manager receives an execution failure or timeout message from a participant, a Rollback message is sent directly to each participant; otherwise, a Commit message is sent; the participant performs a commit or rollback operation according to the instructions of the transaction manager, and releases the lock resources used in the transaction. Note: lock resources must be released in the final phase.

1.1 processes successfully executed by 2PC

Preparation phase:

The transaction manager makes transaction inquiries. The transaction manager (coordinator) sends the transaction content to the database participant, asks if the transaction commit operation can be performed, and waits for the participant to respond.

The participant performs the transaction. (write local Undo and Redo logs)

Responses to participants' feedback queries

Submission phase:

The transaction manager sends a commit (commit) request.

The participant formally commits the transaction and releases the transaction resources occupied during the execution of the transaction.

Participants feed back the transaction commit result and send ACK messages.

After receiving ACK messages from all participants, the transaction manager completes the transaction.

1.2 2PC process for failed execution (interruption of transactions)

The preparation phase is the same as the steps that were successfully performed:

The transaction manager makes a transaction query, and the transaction manager (coordinator) sends the transaction content to the database participant, asks if the transaction commit operation can be performed, and waits for the participant to respond.

The participant performs the transaction. (write local Undo and Redo logs)

Responses to participants' feedback queries

Submission phase:

The transaction manager sends a rollback (Rollback) request when a participant execution failure or timeout message is received.

After receiving the Rollback request, the participant uses the Undo log recorded in the preparation phase to roll back the transaction and release the transaction resources occupied during the execution of the transaction.

Participants feed back the result of transaction commit. After completing the transaction rollback, the participant sends Ack information to the coordinator.

After receiving the Ack information from all participants, the coordinator completes the transaction interruption.

1.3Pros and cons of 2PC

Advantages: the principle is simple and easy to implement.

Disadvantages:

Synchronous blocking

During the execution of the 2PC protocol (commit), all database participants participating in the transaction operation are in a blocking state, that is, each participant cannot do anything else while waiting for a response from another participant.

Single point problem

The transaction manager is very important during the commit phase, and if it goes down, all database participants are in the process of locking database resources all the time.

Data inconsistency transaction manager sent commit commands to some database participants during the commit phase, but some participants did not send them, and then went down again, and only some database participants updated their data, resulting in data inconsistencies.

The over-conservative transaction manager does not have a well-designed fault-tolerant mechanism in the commit (commit) phase, and the failure of any node will lead to the failure of the whole transaction.

Second: distributed transaction-- consistency protocol 3PC

3PC, whose full name is "three phase commit", is an improved version of 2PC. It divides the "submit transaction request Prepare" process of 2PC into two (CanCommit and PreCommit), and forms a transaction protocol composed of CanCommit, PreCommit and doCommit.

2.1 3PC step CanCommit

1) the coordinator makes transaction inquiries

The coordinator sends a CanCommit request with transaction content to all participants, asking if the transaction commit operation can be performed, and starts waiting for a response from each participant.

2) the participant feedback the transaction inquiry to the coordinator.

After receiving the CanCommit request including the transaction content from the coordinator, under normal circumstances, if they think they can perform the business smoothly, they will feedback the Yes response and enter the preparatory state, otherwise they will feedback the No response.

PreCommit

After the coordinator gets the response from all the participants, the participant feedback on CanCommit is Yes, performing transaction pre-commit:

1) the coordinator sends a pre-submission request (issues a preCommit request and enters the prepared phase)

2) the participant performs transaction pre-commit (after the participant receives the preCommit request, the transaction operation is performed and the Undo and Redo information is recorded in the transaction log. )

3) each participant gives feedback on the results of the transaction execution to the coordinator (if the participant successfully performs the transaction operation, then feedback Ack)

After the coordinator gets the response from all the participants, the participant feedback on CanCommit is No, interrupting the transaction:

1) the coordinator sends an interrupt request: (the coordinator sends an abort request to all participants. )

2) interrupt the transaction (whether receiving an abort request from the coordinator or waiting for the coordinator request timed out, the participant will interrupt the transaction)

DoCommit

The DoCommit phase completes the real transaction commit or transaction rollback.

When an ACK confirmation message is received in the second PreCommit phase, the transaction commit is completed:

1) the coordinator sends the submit DoCommit request (the coordinator will transition from the pre-submitted state to the submitted state and send the doCommit request to all participants)

2) the participant commits the transaction (after receiving the DoCommit request, the participant will formally perform the transaction commit operation, and release the transaction resources occupied in the whole transaction execution process after the commit is completed. )

3) each participant feedback the result of the transaction commit to the coordinator (if the participant successfully completes the transaction commit, then feedback the Ack response)

4) complete the transaction (after the coordinator receives the Ack messages from all participants, the transaction is completed. )

If no ACK acknowledgement message is received during the timeout interrupt in the second PreCommit phase, the transaction interrupt is completed:

1) the coordinator sends an interrupt request (the coordinator sends an abort request to all participant nodes)

2) the participant performs a transaction rollback (performs the transaction rollback based on the recorded Undo information and releases the resources consumed during the transaction execution after the rollback is completed)

3) each participant feedback the result of the transaction rollback to the coordinator (after completing the transaction rollback, the participant sends an Ack message to the coordinator. )

4) interrupt the transaction (after the coordinator receives the Ack message from all participants, the transaction is interrupted. )

Note: during the DoCommit phase, there may be downtime of the coordinator and network failure of the coordinator and participants; as a result, the participant will not receive the DoCommit request or Abort request from the coordinator, and the participant will continue to commit the transaction after the request times out.

2.2 comparison between 3PC and 2PC

In 2PC, only the coordinator has a timeout mechanism (if the participant does not receive a message within a certain period of time, it fails by default); both the coordinator and the participant in 3PC have set the timeout mechanism (participants have their own timeout mechanism, and the local commit is automatically carried out after the timeout to release resources to prevent synchronous blocking problems in 2PC.)

3PC sets a buffer phase (PreCommit) relative to 2PC to ensure that the status of each participant node is consistent before the last stage of DoCommit.

This is the end of the content of "what is the 2PC and 3PC of the database". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report