18 - Event Sourcing & CQRS (Command Query Responsibility Segregation)

Executive Summary: Traditional CRUD systems mutate state in place (UPDATE users SET email = '...'), destroying historical intent, temporal context, and audit trails. Event Sourcing persists every state mutation as an immutable, append-only domain event (EmailUpdated, MoneyDeposited). CQRS (Command Query Responsibility Segregation) structurally splits the architecture into two independent models: a write-optimized Command Side enforcing business invariants, and a read-optimized Query Side maintaining denormalized materialized projections. Together, they provide complete auditability, time-travel debugging, and unconstrained read scalability.


🏒 Real-world case studies: how Google & Netflix approach this, in REAL_WORLD_EXAMPLES.md.

⏱️ Prerequisites: What You Need to Know First

To get the most out of this module, make sure you understand:


πŸ§’ The 30-Second Version (ELI5)

Imagine your bank account:


πŸ• Everyday Analogy

Concept Accounting Passbook Analogy Technical Reality in Event Sourcing & CQRS
Command Handing a check to the teller: "Deposit $100" DepositMoneyCommand(accountId, 100)
Domain Event The line stamped in indelible ink in your passbook Immutable event: MoneyDeposited(id, amount, timestamp)
Event Store The official bound leather accounting ledger Append-only database (EventStoreDB, Kafka, PostgreSQL)
Aggregate Root The teller enforcing rules: "You cannot withdraw more than you have" Domain entity verifying business logic against current state
Materialized View The ATM screen showing your latest balance in 1 second Denormalized projection in Redis, Elastic, or PostgreSQL
CQRS Segregating the teller vault (deposits) from the ATM (queries) Splitting Write models (normalized) from Read models (flat)
Snapshot The bank issuing a "Monthly Statement Balance" every 30 days Pre-computed state checkpoint preventing replay of 100k events
Replay Reading the passbook from day one to recount every dollar Re-projecting historical events to rebuild a corrupted database

🧩 The Problem It Solves

The Flaws of Destructive CRUD Updates

In standard CRUD architectures (UPDATE accounts SET balance = 50 WHERE id = 1):

  1. Loss of Intent & History: Who changed the balance? Was it a refund, a fraudulent withdrawal, or a correction? The previous value is permanently destroyed.
  2. Read/Write Impedance Mismatch: The relational schema required to safely process high-concurrency writes (highly normalized with foreign keys and locks) is the exact opposite of what high-speed read queries need (denormalized, flat, aggregated tables).
  3. Locking Bottlenecks: Complex dashboard queries execute heavy SQL JOINs across 10 tables, locking rows and starving transactional write operations.

How Event Sourcing & CQRS Solves This

  1. 100% Complete Audit Trail: Because events are append-only and never deleted, you have an unalterable forensic record of every single business transaction that ever occurred.
  2. Infinite Read Scalability: The Command side only does fast O(1) appends to the event store. The Query side maintains multiple specialized read modelsβ€”e.g., Elasticsearch for full-text search, Redis for real-time balances, and Neo4j for social graphsβ€”all populated asynchronously from the same event stream.
  3. Time-Travel Debugging: You can run an aggregate's state backwards or replay events up to any specific millisecond in the past to reproduce production bugs with exact precision.

πŸ–ΌοΈ Picture It: Architecture & Flow

High-Resolution Event Sourcing & CQRS Animation

Below is an animated trace of a command generating an immutable domain event, persisting to the Event Store, and asynchronously projecting into a high-speed Read Model:

Event Sourcing and CQRS


End-to-End CQRS Flowchart

flowchart LR
    subgraph CommandSide["1. Command Side (Write Model)"]
        ClientWrite["πŸ‘€ Client"] -->|1. Submit Command| CmdHandler["Command Handler<br/>(Validates Invariants)"]
        CmdHandler -->|2. Append Event| Store[("πŸ“œ Event Store<br/>(Append-Only Log)")]
    end

    subgraph EventBus["2. Event Distribution"]
        Store -->|3. Publish Stream| Bus["⚑ Event Bus / Kafka"]
    end

    subgraph QuerySide["3. Query Side (Read Model)"]
        Bus -->|4. Consume| Projector["Background Projector"]
        Projector -->|5. Update View| ReadDB[("πŸ“– Read DB (Redis/SQL)<br/>Denormalized Projections")]
        ClientRead["πŸ‘€ Client"] -->|"6. Query Balance (0.5ms)"| ReadDB
    end

    style CommandSide fill:#161b22,stroke:#58a6ff,stroke-width:2px;
    style EventBus fill:#161b22,stroke:#d29922,stroke-width:2px;
    style QuerySide fill:#161b22,stroke:#3fb950,stroke-width:2px;

πŸ”’ Walk-Through with Real Numbers

Let's look at the performance math of Event Sourcing write throughput vs. Snapshotting intervals:

1. Write Throughput: Sequential Append vs Relational UPDATE

2. Snapshotting Frequency Math

Suppose a popular e-commerce inventory item undergoes 1,000,000 stock adjustments per year:


βš™οΈ How It Works Under the Hood

1. The Anatomy of an Event Store

An Event Store is typically built using specialized engines (like EventStoreDB) or modeled inside PostgreSQL/DynamoDB with strict table constraints:

*(The event store schema is defined in [schema.sql](./schema.sql); run [demo.py](./demo.py) for the executable simulation with Optimistic Concurrency Control.)*

Optimistic Concurrency Control (OCC)

When two users concurrently attempt to modify the same account:

  1. Both read the current stream version (V = 4).
  2. User A appends MoneyWithdrawn with version = 5. The write succeeds.
  3. User B attempts to append MoneyWithdrawn with version = 5.
  4. The database primary key constraint (stream_id, version) rejects User B's insert with a unique constraint violation!
  5. User B's command handler catches the error, reloads the aggregate with fresh events, and re-evaluates whether sufficient balance remains.

2. Materialized Views & Multiple Projections

The real power of CQRS is that a single event stream can populate dozens of completely different, purpose-built read projections:

                                  β”Œβ”€β”€> [Redis] ─────────────> Balance View (Key-Value)
                                  β”‚
[Event Store] ──> [Projectors] ───┼──> [Elasticsearch] ─────> Full-text Audit Search
                                  β”‚
                                  └──> [PostgreSQL / Redshift] -> Monthly Analytics & Tax Reports

βš–οΈ Trade-Offs & Decision Matrix

Dimension Traditional CRUD Architecture Event Sourcing + CQRS
Auditability & History None (overwritten in place) 100% Complete & Immutable
Write Performance Slower (locking, normalized updates) Fastest (O(1) sequential appends)
Read Complexity Complex SQL JOINs across tables Trivial O(1) flat denormalized queries
Consistency Strong / Immediate consistency Eventual Consistency (read projections lag by 10–50ms)
System Complexity Low (familiar to all engineers) High (requires DDD, event stores, idempotency, projections)
Schema Evolution ALTER TABLE migrations Event upcasting and versioned schemas

🚫 Common Mistakes & Production Pitfalls

  1. Using Event Sourcing for Simple CRUD Applications:

    • Pitfall: Building a simple blog or admin settings page using Event Sourcing and CQRS.
    • Result: Extreme over-engineering. Writing commands, aggregates, events, projectors, and read views for a system with 10 writes a day adds massive architectural drag with zero benefit. Use CRUD where historical intent doesn't matter.
  2. Treating Events as Ephemeral Messages Instead of State:

    • Pitfall: Deleting events from the event store after 7 days like a standard message queue.
    • Result: Total disaster. In Event Sourcing, the Event Store is the primary database of record. If you delete past events, you destroy your source of truth!
  3. Breaking Event Schema Backward Compatibility:

    • Pitfall: Renaming a field in a payload from user_name to username without migration logic.
    • Result: When historical events from 2 years ago are replayed, deserialization crashes on missing fields!
    • Fix: Use Event Upcasting (transforming old event JSON into current version schema in memory during deserialization) or enforce strict Protobuf/Avro backward-compatibility rules.

πŸ’» Try It Yourself: Python Simulation

The companion simulation script demo.py implements:

  1. A complete Bank Account Aggregate with command validation (OpenAccount, Deposit, Withdraw).
  2. An append-only Event Store enforcing optimistic concurrency control.
  3. A denormalized Read Model Projector maintaining instant balance summaries.
  4. Live disaster recovery: Deleting the read database and rebuilding it from historical events with 100% accuracy.

Run it locally in your terminal:

python demo.py

🌍 Where You'll See It in Production


🎯 Top Interview Questions & Deep Answers

Q1: "What is the core difference between Event Sourcing and Event-Driven Architecture (EDA)?"

Interviewer Goal: Disqualifying candidates who confuse architectural patterns.

Model Answer: "While both patterns utilize events, they solve completely different architectural concerns:

  1. Event-Driven Architecture (EDA) is an inter-service communication pattern. Services publish events (e.g., via Kafka or SNS) to notify other services that something occurred. The publishing service may still use traditional relational CRUD tables internally to store its own state.
  2. Event Sourcing is an internal data persistence pattern. Instead of storing current state in a database row, the service persists every mutation as an append-only sequence of domain events. The event log is the definitive source of truth. In summary: EDA is about how separate systems communicate; Event Sourcing is about how a single system persists its own state. They are often combined, but one does not imply the other."

Q2: "How does CQRS handle eventual consistency when a user performs a write and immediately reads the result?"

Interviewer Goal: Testing practical user-experience engineering under eventual consistency.

Model Answer: "In CQRS, writing to the Command side and projecting to the Read side is asynchronous, typically introducing 10–50 ms of replication lag. If a user deposits $100 and the browser immediately redirects to the dashboard, the read database might not have applied the projection yet, showing the old balance.

To solve this in production:

  1. Command Returns Version Token: The command handler returns the newly committed version or event ID (e.g., version = 5).
  2. Read-With-Version Polling: The frontend requests the read model passing expected_version=5. If the projection is still at version 4, the read API either holds the connection briefly (long polling for up to 100 ms) or queries the primary aggregate.
  3. Optimistic Client UI: The web/mobile frontend updates the local UI state optimistically upon receiving the command 200 OK, completely hiding the projection delay from the user."

Q3: "What is an Aggregate Root in Event Sourcing, and why is it the only entity that can validate business invariants?"

Interviewer Goal: Assessing Domain-Driven Design (DDD) rigor and transactional boundary modeling.

Model Answer: "An Aggregate Root is a cluster of associated domain objects treated as a single unit for data changes. It defines the consistency boundary for transactions:


Q4: "How do you handle schema evolution and changing requirements in an immutable Event Store over 5 years?"

Interviewer Goal: Evaluating long-term operational viability and backward-compatibility maintenance.

Model Answer: "Because events in the Event Store are immutable, historical records written 5 years ago cannot be modified with an ALTER TABLE migration. We handle schema evolution using three production patterns:

  1. Weak Schema / Additive Changes: New fields are always made optional with safe defaults. Old fields are never deleted.
  2. Upcasting (In-Memory Transformation): When reading an old event from the database (e.g., UserRegistered_v1), an upcaster intercepts the raw JSON and transforms it into UserRegistered_v2 in memory before handing it to the application. The on-disk storage remains untouched, but the application code only deals with the latest schema.
  3. Event Migration Streams: For major structural overhauls, a migration script reads Stream A and writes a brand new Stream B, pointing future consumers to the new stream."

Q5: "What are Snapshots in Event Sourcing, and how do they prevent performance degradation as logs grow?"

Interviewer Goal: Checking performance optimization and memory bounding skills.

Model Answer: "In Event Sourcing, loading an entity requires replaying all historical events from version 1 to version N. For long-lived aggregates (like an active bank account with 50,000 transactions), replaying 50,000 events on every single command takes hundreds of milliseconds, crippling write throughput.

Snapshots solve this:


βœ… Check Yourself: Flash Quiz

1. True or False: In Event Sourcing, can you issue an UPDATE or DELETE SQL statement to alter a past event in the Event Store?
False. Event Stores are strictly append-only and immutable. If a mistake was made (e.g. accidentally depositing $1,000 instead of $100), you never alter the original MoneyDeposited event. Instead, you append a new Compensating Event (e.g. DepositReversed or CorrectionApplied), exactly as accountants do in a ledger.
2. In CQRS, what is the primary role of a "Projector"?
A Projector is a consumer that listens to the stream of Domain Events emitted by the Write side, transforms them, and updates denormalized Materialized Views (Read Models) so queries can be served instantly without complex SQL joins.
3. How does Optimistic Concurrency Control (OCC) prevent two concurrent users from overwriting each other in an Event Store?
By checking the expected_version. The database enforces a unique composite primary key on (stream_id, version). If both users read version 5 and try to append an event at version 6, the first writer succeeds, and the second writer's query fails with a unique constraint violation, prompting a retry.