Two-phase commit
|
In computer science, especially in durability studies, a two phase commit allows data to be written to more than one location and for all operations to be rolled back, if one or more of the operations fails.
The Algorithm
The process is controlled from one of the participants called the Coordinator. The other participants are called Cohorts. The algorithm is initiated by the Coordinator after the last step of the transaction has been reached. The Cohorts then respond with an "OK" or "NOT OK" depending on success.
- Phase 1
- All participants execute the transaction up to the point where they will be asked to commit.
- If successful
- respond "OK" to Coordinator
- If unsuccessful
- either allow a time-out or respond "NOT OK" to Coordinator
- Phase 2
- If all Cohorts respond "OK":
- Coordinator instructs participating resource managers to "COMMIT"
- Participants complete operation and sent acknowledgement to Coordinator
- Coordinator completes transaction when acknowledgements have been received.
- Otherwise (some Cohorts responded "NOT OK", or didn't respond at all):
- Coordinator instructs participating resource managers to "ROLLBACK"
- Participants complete their respective local undos
Disadvantages
The algorithm can block indefinitely in two ways:
- When the Coordinator has sent "COMMIT" to the Cohorts, it will block until all Cohorts have acknowledged. Thus, if a Cohort is permanently down, the Coordinator will block indefinitely.
- When a Cohort has sent "OK" to the Coordinator, it will block until a "COMMIT" or "ROLLBACK" is received. If the Coordinator is permanently down, the Cohort will block indefinitely.