Alternatives: Event Sourcing and CQRS
What you'll learn: How to avoid distributed transaction coordination by rethinking data storage and query models entirely.
The Problem with Distributed Transactions
As you've seen, distributed transactions are expensive and fragile—they block on coordinator failures and require complex coordination protocols. What if instead of trying to keep multiple databases perfectly synchronized with 2PC or 3PC, we fundamentally changed how we store and query data?
Event Sourcing: The Append-Only Log
Event Sourcing treats every state change as an immutable event appended to a log. Instead of storing current account balances, you store every deposit and withdrawal event. The current state is derived by replaying the event log.
Analogy: Rather than maintaining a single "current score" in a sports game, you keep a detailed play-by-play record. Want the score? Replay all the scoring events.
This eliminates update conflicts—events are facts that happened, never modified. Each service can independently process events from the log without distributed locks.
CQRS: Separate Read and Write Models
Command Query Responsibility Segregation (CQRS) splits your system into two paths:
- Command side: Validates and appends events to the log
- Query side: Maintains materialized views optimized for reads, updated asynchronously from events
You avoid distributed transactions because writes just append events locally, while read models eventually catch up by consuming those events—classic eventual consistency from your earlier lessons.
Example flow: A payment service appends "PaymentCompleted" event → inventory service reads that event → updates its own "reserved items" view independently. No coordinated commit needed.
Key Takeaway: Event Sourcing and CQRS sidestep distributed transaction complexity by making events the source of truth and accepting eventual consistency between independent, event-driven components.