Distributed

Two-Phase Commit (2PC)

Description

Two-Phase Commit (2PC) is a standardized protocol that coordinates all the processes that participate in a distributed atomic transaction on whether to commit or abort (rollback) the transaction. It ensures that either all databases are updated or none are, providing strong consistency.

Where to use

  • Financial transactions across multiple databases.
  • \n
  • Legacy systems requiring immediate consistency.

Real World Example

Transferring funds between two different bank databases (Shards). Both shards must confirm they are ready to commit before the transaction is finalized.

Code Example

# Code Concept for Two-Phase Commit (2PC)\n# This pattern is architectural.\n\nclass Service:\n    def execute(self):\n        # Implementation logic\n        pass