2PC
2 phase commit
In two-phase commit, phase 1 is prepare, when receive commit request, it sends two all participants Ready? And wait for ok back. Then phase 2 is commit and wait for OK, then send SUCCESS to application server. If there is ABORT, it will send back to application server with ABORT and send ABORT to participants in phase 2.
The improvement is using Early Prepare Voting(using when it is the last one, return the result is the transaction result), Early Acknowledgement After Prepare (If all votes to commit a ten, it can send acknowledgement to client before phase 2 finishes). Two-Phase Commit will block if coordinator fails after the prepare message is sent, until coordinator recovers.
pharse1
Coordinator
- Precommit (write to log and.or atomic storage)
- Send request to all participants
- Coordinator blocks waiting for ALL replies
Participant
- Wait for request
- Upon request, if ready:
- Precommit
- Send coordinator YES
- Upon request, if not ready:
- Send coordinator NO
pharse2
Coordinator
- This is the point of no return!
- If all participants voted YES then send commit to each participant
- Otherwise send ABORT to each participant
Participant
Wait for "the word" from the coordinator
- If COMMIT, then COMMIT (transaction becomes visible)
- If ABORT, then ABORT (gone for good)
Another real-world atomic commit protocol is three-phase commit (3PC). This protocol can reduce the amount of blocking and provide for more flexible recovery in the event of failure.