Skip to main content

Preview — Pro guide

You are seeing a portion of this guide. Sign in and upgrade to unlock the full article, quizzes, and interview answers.

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.

40 min read 2 sections 1 interview questions
Event SourcingCQRSAudit LogDomain EventsEvent StoreProjectionsRead ModelWrite ModelTemporal QueryDistributed SystemsKafkaEventually Consistent

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:

  1. Audit and compliance: financial systems must reconstruct any past state (Stripe does this)
  2. Temporal queries: "what was a user's account balance on March 15th at 2:47pm?"
  3. 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.

IMPORTANT

Premium content locked

This guide is premium content. Upgrade to Pro to unlock the full guide, quizzes, and interview Q&A.