2PC
2PC is a distributed algorithm that ensures all participants in a distributed transaction either commit or abort the transaction, maintaining atomicity across systems. It is divided in two phases: prepare and commit.
In the prepare phase the coordinator (Transaction Manager) asks all participants (Resource Managers) to prepare to commit. Participants will reply with a yes, if they can commit, or a no.
In the commit phase, if any participant has replied “No”, the coordinator will instruct all participants to abort, while if all participants replied “Yes” it will instruct them to commit
Warning
This approach ensures atomicity but locks participants during the commit process leading to contention and has a single point of failure in the coordinator/Transaction Manager
Warning
If the coordinator crashes after a prepare message, all participants will be stucked forever
3PC
In three-phase commits, an additional phase is added before committing (pre-commit). If the coordinator crashes after a prepare but before sending a pre-commit, the nodes participants can abort after a timeout and avoid getting stucked
Warning
Try-Confirm-Cancel (TCC)
TCC is a distributed transaction pattern that divides a business transaction into three distinct steps to ensure consistency across microservices.
In the try phase the services reserve necessary resources and perform initial check, while in the confirm phase the actual transaction is executed idempotently. If the transaction doesn’t succeed, in the cancel phase the resources are released
Reduces the likelihood of resource contention compared to 2PC, Provides a structured approach to handle distributed transactions without locking resources for extended periods.
Important
Try-Confirm-Cancel is an application level transcation protocol equivalent to the saga pattern except that there is a reservation phase