LearningTech

Distributed

Compensating Transactions

Description

A compensating transaction is a transaction that undoes the effects of a series of steps that have already been committed. It is used primarily in the SAGA pattern to revert state if a long-running workflow fails.

Where to use

  • Rollback mechanisms in distributed workflows.
  • \n
  • SAGA Pattern implementations.

Real World Example

Booking a Flight and Hotel. If the Hotel booking fails, a Compensating Transaction is triggered to Cancel the Flight booking which was already committed.

Code Example

# Code Concept for Compensating Transactions
# This pattern is architectural.

class Service:
    def execute(self):
        # Implementation logic
        pass