← Back to System Design, page 5

system-design-0089 · Medium mid

What are event sourcing and CQRS, how do they work together, and when are they worth the extra complexity?

Event sourcing stores the sequence of events that changed an entity (OrderPlaced, ItemAdded, OrderShipped) as the source of truth and rebuilds current state by replaying them. CQRS separates the write model, which validates commands and appends events, from one or more read models built from those events and shaped for queries. Together they give full history, audit and flexible new views, but add eventual consistency, event versioning and replay costs, so they suit complex domains, not simple CRUD.

Full answer

Event sourcing

Instead of a row saying "balance = 900", store:

  1. AccountOpened {id: 7}
  2. Deposited {amount: 1000}
  3. Withdrawn {amount: 100}

Current state is the result of applying events in order. To handle a command ("withdraw 200"), the system loads the account's events, rebuilds state, checks rules (enough balance) and appends a new event. Appends use optimistic concurrency: "append only if the stream is still at version 3", so two concurrent commands cannot both succeed on stale state.

Snapshots (state at version 500) avoid replaying thousands of events each time.

CQRS

Command Query Responsibility Segregation means using different models for writes and reads.

  • Write side: small aggregates that enforce invariants and emit events.
  • Read side: projections that consume events and maintain views such as "orders by customer" in SQL, "search by product" in a search index, or a dashboard counter in Redis.

You can use CQRS without event sourcing (read replicas, materialised views), but event sourcing makes CQRS natural, because events are the feed for every projection.

Benefits

  • Complete audit trail: every change and why, in order. Useful for finance, compliance, and debugging ("what did this order look like on Tuesday?").
  • New read models can be built later by replaying history.
  • Events integrate naturally with other services.

Costs

  • Eventual consistency between writes and read models: a user may not see their change in a list immediately. Return the result from the write side, or wait for the projection to reach the event's version, for read-your-writes.
  • Event schema evolution: old events live forever. Version events and write upcasters that convert old versions when reading.
  • Replays of large streams take time; plan for rebuilding projections in parallel.
  • Deleting personal data conflicts with immutable logs; use crypto-shredding (encrypt personal fields per user and delete the key).
  • More moving parts: event store, projectors, monitoring of projection lag.

When to use

Good fit: complex business rules, strong audit needs, many different read views, collaboration domains (orders, payments, bookings, inventory). Poor fit: simple CRUD screens, small teams without the need for history. Many systems use event sourcing for one or two core areas only.

By level

Fresher: Explain that instead of saving only the current value, the system saves every change as an event and can rebuild the value. A fresher should describe a separate read view for listing data.

Mid: Describe optimistic appends, snapshots, projections and eventual consistency, and when CQRS appears without event sourcing. Give fit criteria.

Senior: Discuss schema evolution with upcasters, projection rebuilds, read-your-writes strategies, crypto-shredding and partial adoption. Expect a clear cost-benefit judgement.

Key points

  • Event sourcing stores events as the source of truth and replays them for state
  • Optimistic concurrency on stream version for appends
  • CQRS separates the write model from query-shaped read models
  • Projections are eventually consistent and can be rebuilt
  • Costs: schema evolution, replay time, deletion of personal data

Common mistakes

  • Using event sourcing for simple CRUD
  • Ignoring event versioning
  • Expecting read models to be updated instantly
  • Storing personal data in immutable events with no deletion plan

Follow-ups

  • How do you give users read-your-writes with CQRS?
  • How would you rebuild a projection with 2 billion events?
  • How does crypto-shredding work?

Tips

  • Use a bank account event list as the example
  • Separate event sourcing from CQRS clearly
  • Be honest about when not to use them

Evaluation rubric

  • Correct definitions and how they combine
  • Concurrency and snapshots
  • Benefits and costs balanced
  • Sensible fit guidance