Explain the two-phase commit principle and its significance in ensuring data integrity in distributed transactions.
sobes.tech AI
Answer from AI
Two-Phase Commit (2PC) is a protocol that ensures the atomicity of distributed transactions, where multiple resources (e.g., databases) must commit changes in a coordinated manner.
Working principle:
-
Prepare phase: The transaction coordinator sends a prepare request to all participants. Each participant checks if it can perform the transaction and reserves necessary resources, responding with 'ready' or 'not ready'.
-
Commit phase: If all participants respond with 'ready', the coordinator sends a commit command. If at least one participant is not ready, the coordinator sends a rollback command.
Significance:
- Ensures data integrity by guaranteeing that either all participants commit the changes or none do.
- Prevents data inconsistency in distributed systems.
Example: When transferring money between accounts in different banks, two-phase commit guarantees that the debit and credit happen simultaneously or not at all.