Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Data Consistency

Event Sourcing and CQRS

Store what happened instead of the current state, rebuild state by replaying events, and split reads from writes with CQRS, using a runnable bank account.

Most services store the current state: one row per account holding today's balance. When the balance changes, the old value is overwritten and the history is gone. Event sourcing turns this around. The service stores the sequence of events that happened (Deposited 100, Withdrawn 30), and the current state is simply what you get by replaying them.

CQRS (Command Query Responsibility Segregation) is a companion idea: use one model to handle commands that change things, and separate models, shaped for reading, to answer queries. The two are often combined, because an event log is a natural source for building many read models. Both ideas are powerful and both are easy to overuse, so this lesson also covers when they are worth their cost.

Store facts, derive state

Event log Append-only list of what happened.
Replay Fold the events to get the state.
Time travel Rebuild any past moment.
1 / 2

Events as the source of truth

An event is an immutable fact in the past tense, with the data needed to understand it. The log is append-only: you never edit or delete an event. A correction is a new event (DepositReversed) that sits after the mistake.

To get the current state, you fold (reduce) the events: start from an empty state and apply each event in order. The function that applies one event to a state is usually called apply. It must be a pure function with no side effects, because it will run again every time someone replays the log.

JS A bank account rebuilt from its events

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Slicing the log gave the state at an earlier moment for free. That "time travel" is one reason teams choose event sourcing: audit trails, debugging ("how did we get here?") and answering questions nobody thought to ask when the data was written.

Commands, validation and aggregates

State changes come in as commands ("withdraw 80"). The command side loads the current state by replaying the account's events, validates the command against that state, and, if it is allowed, appends one or more new events. A rejected command appends nothing. The group of events that must stay consistent together (here, one account) is an aggregate, the same word as in domain-driven design.

When two commands race for the same account, the store uses optimistic concurrency: each append says "I expect the log to have N events", and the store refuses it if someone else got there first. The loser reloads and tries again.

JS Commands validated against replayed state

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice what the command handler does not do: it never writes a balance. It only records facts that passed the rules.

CQRS and projections

Replaying a long log for every screen would be slow, and the shape of the events is rarely the shape a screen needs. CQRS says: do not force one model to do both jobs. Build separate read models, also called projections, fed by the events. A bank might have one projection "balance by account" for the app's home screen, another "monthly statement" for PDFs, another "daily totals" for the finance team.

A projection is a function that listens to events and updates a table (or a search index, or a cache) designed for fast reads. You can rebuild any projection from scratch by replaying the whole log into an empty one. That makes projections cheap to change: ship a new version, replay, switch over.

JS Projections, eventual consistency and replay

Press Run (or Ctrl + Enter) to see the console output here.

No output.

For a moment the read model was behind: the query side was empty although the events were already stored. That is eventual consistency, and it is the price of CQRS with asynchronous projections. A user who deposits money and immediately reloads might not see it yet. Typical fixes: show the result of the command from its response, read from the write side for that one user, or wait until the projection reports the event's position.

In a microservice system

Events are not only for one service's internal use. A service can publish its events (usually through an outbox) so other services build their own read models from them, for example a "customer profile" view that combines data from three services. A log-based broker such as Kafka is a natural carrier because consumers can replay from any position, as described in Asynchronous Messaging and Events.

Be careful about the difference between two things:

  • Internal events that form the source of truth and may change shape as the domain model evolves.
  • Integration events that you publish to other teams. These are a public contract and need stable names and versions (see API Versioning and Evolving Contracts).

Costs and when to use it

Event sourcing is not the default choice. It brings real work.

  • Schema evolution. Old events live forever, so event types need versioning and upcasters that translate old shapes when replaying.
  • Slow replays. Long streams are rebuilt from snapshots: a stored state at event number N, then only newer events are applied.
  • Complexity. Developers must think in events, projections and eventual consistency. Simple CRUD screens become harder, not easier.
  • Privacy. "Delete my data" conflicts with an immutable log. Common answers are encrypting personal data per user and discarding the key, or keeping personal data outside the events.
  • Querying. You cannot run an arbitrary query on the log; every question needs a projection.

It pays off where history is the business (ledgers, orders, shipments, audit-heavy domains) and where several different views of the same facts are needed. For a plain admin form, a normal table is better. You can also use CQRS without event sourcing (a separate read database fed by change events), and event sourcing in one service without adopting it everywhere.

Start with the smallest version that solves your problem: keep ordinary tables, publish events via an outbox, and add read models where a query is slow. Move to a full event-sourced aggregate only for the few domains that need the history.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In event sourcing, how do you get the current state of an aggregate?

    The events are the source of truth. State is derived by applying them one after another.
  2. 2. A deposit was entered by mistake. What does an event-sourced system do?

    The log is append-only. History is never rewritten, only extended.
  3. 3. What is optimistic concurrency used for when appending events?

    The writer states the version it saw. If another writer appended first, the store refuses and the command is retried on fresh state.
  4. 4. What is a projection in CQRS?

    Projections turn events into tables or indexes optimized for reading, and can be rebuilt by replaying the log.
  5. 5. What is a consequence of building projections asynchronously?

    The query side processes events after they are stored, so it can be a little behind.

Summary

  • Event sourcing stores immutable events and derives state by replaying them with a pure apply function.
  • Commands are validated against replayed state and, if accepted, append new events; optimistic concurrency guards against races.
  • CQRS separates the write model from read models (projections) that can be rebuilt from the log at any time.
  • Asynchronous projections are eventually consistent, so interfaces must tolerate a short delay.
  • The costs are schema evolution, snapshots, complexity and privacy, so use event sourcing only where history is core to the domain.

Next: Timeouts, Retries and Exponential Backoff.