Module 10: Distributed Transactions & Saga Patterns
🚀 Problem Statement
Reverting a document revision must atomically:
- Create a new document version
- Clone all sections, tasks, risks, and control measures
- Clone comments with remapped entity IDs
- Update the search index
- Invalidate caches
If step 3 fails while steps 1-2 are already committed, orphaned data remains in the system.
🧠 The Engineering Story
The Villain: "The Partial Commit." In a monolith, this was one database transaction. In microservices, each service has its own database. There's no distributed BEGIN...COMMIT.
The Hero: "The Saga Choreographer." Each step publishes an event on success. If a step fails, compensating events undo previous steps (e.g., DocumentCreated → on failure → DocumentDeleted).
The Plot:
- Understand why 2PC (Two-Phase Commit) doesn't scale
- Design Choreography-based Sagas (events trigger next step)
- Design Orchestration-based Sagas (central coordinator)
- Implement compensating transactions for each step
The Twist (Failure): The Phantom Compensation. A compensating action for "clone comments" might be "delete cloned comments." However, if a reply is added to one of the cloned comments before the compensation occurs, deleting the comment will orphan the reply.
Interview Signal: Can choose between choreography vs orchestration sagas and design compensating transactions.
📝 Design Exercise
Design a Saga for the document revision workflow across 3 services (Document Service, Core Engine, Comment Service). Define the happy path, compensating actions for each step, and how to handle the case where a compensating action itself fails.