Preview — Pro guide
You are seeing a portion of this guide. Sign in and upgrade to unlock the full article, quizzes, and interview answers.
Sections
Related Guides
Distributed Transactions: 2PC, Saga Pattern, and Compensating Transactions
High-Level Design
Message Queues & Streaming: Kafka, Delivery Semantics, and Consumer Groups
High-Level Design
Databases: Sharding, Indexing & Replication
High-Level Design
Distributed Systems Patterns
High-Level Design
Consistency Models: Strong vs Eventual vs Causal, Linearizability, CRDTs & CAP Theorem
High-Level Design
Event Sourcing and CQRS: Audit Logs, Temporal Queries, and Read/Write Separation
Event sourcing stores state as an immutable event log rather than mutable rows. CQRS separates write and read models for scalability in distributed systems. Together they power audit logs, temporal queries, and high-read systems at Stripe and Microsoft. Covers failure modes and the projection rebuild problem most candidates miss.
What Event Sourcing and CQRS Solve
Traditional CRUD databases store the current state of an entity. An order starts as "pending", becomes "confirmed", then "shipped". The intermediate states are overwritten — you cannot answer "what did this order look like 3 days ago?"
Event sourcing stores every state transition as an immutable event instead: [OrderPlaced, PaymentCharged, ItemShipped, DeliveryConfirmed]
Current state is derived by replaying events from the beginning (or from a snapshot). The event log becomes the source of truth; the current-state table is just a projection.
This solves three production problems:
- Audit and compliance: financial systems must reconstruct any past state (Stripe does this)
- Temporal queries: "what was a user's account balance on March 15th at 2:47pm?"
- Retroactive corrections: if a bug incorrectly processed events, replay them with a fix
CQRS separates the write model (commands → events) from the read model (projections → queries). Writes go through a command handler that validates business rules and emits events. Reads query denormalized projections optimized for specific query patterns.
Event sourcing without CQRS is unusual. Most production systems use both together.