Capstone Transactions: Saga Compensation State Machine
Holding two-phase commit database locks across independent network microservices destroys concurrency and causes distributed deadlocks. The Capstone engine coordinates multi-step transactions using the Saga Pattern: executing a pipeline of forward operations with reverse compensating rollbacks upon failure 🔄📜🛡️.
class Step:
def __init__(self, action, undo):
self.action = action
self.undo = undo
def run_saga(steps):
done = []
try:
for s in steps:
s.action()
done.append(s)
except Exception:
for s in reversed(done):
s.undo()
return "ROLLED_BACK"
return "COMMITTED"
print("Saga Framework Ready 🚀")
# 113. Capstone Transactions: Saga Compensation State Machine In a high-throughput microservice architecture, attempting to maintain ACID transactions using classic Two-Phase Commit (2PC) is an anti-pattern. 2PC holds row locks on remote databases for multiple network round-trips. If the coordinator or network freezes, all locked tables freeze across the entire enterprise 🛑. ```python # The Saga Pattern: # Forward Steps: [Step 1: Reserve Funds] -> [Step 2: Reserve Stock] -> [Step 3: Ship Order] # | (Fails!) # Compensating Steps: [Undo 1: Release Funds] <- [Undo 2: Release Stock] <---------+ # (Executed in REVERSE order) ``` ### The Invariants of Compensating Transactions 1. **Semantic Undo, Not In-Memory Revert**: You cannot simply "roll back" disk memory when an email was already sent or a bank was charged. The compensating action is an explicit inverse business transaction (e.g. issue a refund). 2. **Strict Idempotency**: If a compensating network call times out, the coordinator will retry it. Calling `refund(tx_101)` twice must never double-refund money! 3. **Audit Trail Durability**: Every transition in the Saga state machine is written to the Write-Ahead Log before execution so that an in-flight rollback survives server restarts 🔄📜🛡️. --- ### Interactive Pop-Quiz 🧠 #### Why must compensating actions in a Saga transaction be strictly idempotent? - [ ] Because network timeouts may trigger compensating actions more than once; executing a refund multiple times must not refund money repeatedly 🛡️ - [ ] Because Python requires functions to be idempotent to compile - [ ] Because non-idempotent functions cannot return strings - [ ] To prevent the operating system from shutting down --- ### Practical Challenge 🎯 Implement `SagaStep(name: str, forward_fn, compensate_fn)` and `SagaCoordinator` with `execute(steps: list[SagaStep]) -> bool`. Execute forward actions in sequence, appending completed steps to an internal stack. If an exception occurs, iterate the completed stack in reverse, invoking each step's `compensate_fn`, and return `False`. Test with 2 succeeding steps and a 3rd failing step, assert rollback order, and print execution log. <details> <summary>Solution</summary> ```python class SagaStep: def __init__(self, name: str, forward_fn, compensate_fn): self.name = name self.forward = forward_fn self.compensate = compensate_fn class SagaCoordinator: def __init__(self): self.history = [] def execute(self, steps: list[SagaStep]) -> bool: self.history.clear() for step in steps: try: step.forward() self.history.append(step) except Exception: self._rollback() return False return True def _rollback(self): for step in reversed(self.history): step.compensate() audit_log = [] s1 = SagaStep("reserve_funds", lambda: audit_log.append("funds_held"), lambda: audit_log.append("funds_released")) s2 = SagaStep("reserve_stock", lambda: audit_log.append("stock_held"), lambda: audit_log.append("stock_released")) s3 = SagaStep("notify_shipper", lambda: (_ for _ in ()).throw(RuntimeError("Carrier Offline")), lambda: audit_log.append("shipper_cancelled")) coord = SagaCoordinator() ok = coord.execute([s1, s2, s3]) assert ok is False assert audit_log == ["funds_held", "stock_held", "stock_released", "funds_released"] print(f"SagaCoordinator verified: reverse compensation completed: {audit_log}") ``` </details>