🗄️Key Takeaways
- 1Shared database is the #1 microservice anti-pattern — creates tight coupling and coordination problems
- 2Saga pattern: chain of local transactions with compensating actions for rollback
- 3API Composition: aggregate data from multiple services at the application layer
- 4CQRS: separate optimized read models from write models — powerful for complex query requirements
Managing Data Across Service Boundaries
The hardest part of microservices isn't building services — it's managing data across them. Without shared databases, how do you query data spanning multiple services? How do you maintain consistency across service boundaries? These patterns answer those questions.
Data Patterns
A saga is a sequence of local transactions. Each service does its work and publishes an event. If any step fails, execute compensating transactions to undo previous steps.
Choreography: services react to events autonomously. Simple but hard to track end-to-end.
Orchestration: central coordinator directs the workflow. Easier to reason about but creates a SPOF.
Example: Order → Reserve Inventory → Charge Payment → (fail) → Refund → Release Inventory.
Advantages
- •Saga pattern enables distributed transactions without 2PC
- •CQRS enables highly optimized read models
- •CDC provides real-time data sync without code changes
Disadvantages
- •Saga compensations are complex to implement correctly
- •CQRS adds significant architectural complexity
- •Eventual consistency requires careful UI/UX handling
🧪 Test Your Understanding
In a Saga orchestration, what coordinates the workflow?