SkillSprout
Chapter 30 · Lesson 2
Advanced
4 min read

Event Sourcing: Storing Events, Not State

Overview

Normally a database keeps only the latest value, and every update overwrites the past. Event sourcing flips this: you store every change as an event and work out the current state from them. It gives you a complete history for free, at the price of extra complexity.

Detailed Notes

What is event sourcing

In event sourcing, the source of truth is a list of events, facts that already happened, such as "AccountOpened", "MoneyDeposited $100", "MoneyWithdrawn $30". Events are named in the past tense and are append-only: you add new ones but never edit or delete old ones. The store holding them is called an event store. The current state is not saved as the truth; it is calculated from the events.

The problem it solves

With a normal table, a bank balance of $70 tells you nothing about how it got there. Once you overwrite a value, the history is gone. That makes it hard to:

  • Audit what happened and who did it.
  • Find the cause of a bug by seeing the exact steps.
  • Build new views later from old data, because the data was never kept.

How it works

  • Append: each change is written as a new event with an ID, time and data, ordered per entity (for example per account). The event store behaves like the log you saw in Kafka.
  • Replay: to get the current state, start from nothing and apply the events in order: 0 + 100 - 30 = 70. Replaying can also rebuild state as it was at any past time.
  • Snapshots: replaying thousands of events on every request is slow. Periodically you save a snapshot, the computed state at event number N. To load an entity, take the latest snapshot and replay only the events after it. A snapshot is just a speed-up; the events stay the truth.
  • Projections: events can feed read models, which pairs naturally with CQRS. The event store is the write side, and projectors turn events into read databases. To change a read model's shape, build a new projection by replaying all events.
  • Fixing mistakes: because events are never edited, you correct an error with a new event, such as "DepositReversed", like an accountant adding a correcting entry instead of erasing.

Trade-offs:

  • The audit trail, replay and the ability to build new views later are strong benefits.
  • Queries on current state are awkward without projections, so reads are eventually consistent.
  • Events live forever, so changing an event's format later needs care (versioning).
  • The team needs to learn a new way of thinking, and deleting personal data is hard when events are permanent.

A simple example

A bank statement or a Git history. The statement lists every deposit and withdrawal, and the balance is just their sum. Git stores commits (events) and your working files are the state at the latest commit. A snapshot is like a tag you can start from instead of reading every commit.

System design example

An order system stores events: OrderPlaced, PaymentReceived, ItemShipped, OrderCancelled. Each is appended to the event store and also published to a broker. One projection builds the "My orders" page, another feeds analytics, and a third keeps a fraud model updated. When support asks "why was this order cancelled?", the exact events answer it. When the team wants a new report next year, they replay all events into a new projection. A snapshot is saved every 100 events per order so loading stays fast. Event sourcing suits finance, ordering and audit-heavy domains, not simple apps where only the latest value matters.

Summary

  • Store immutable events, not just the current state; state is derived from them.
  • Replaying events rebuilds state at any point in time.
  • Snapshots avoid replaying the full history every time.
  • Fix mistakes with new compensating events, never edits.
  • Gains: audit trail, debugging, new read models; costs: complexity, event versioning, eventual consistency.

Architecture Diagram

Event sourcing with snapshot and projection
Architecture
Commandwithdraw $30
Event storeappend-only log
Current statebalance = $70
Read model

Events are the truth; state and read models are derived from them.

  1. 1A command is validated and becomes a new event.
  2. 2The event is appended to the event store.
  3. 3State is rebuilt by replaying events after the latest snapshot.
  4. 4Projections update read models from the events.

Common Interview Questions

Chapter quiz

CQRS and Event Sourcing · 5 questions

  1. 1.In CQRS, what is a command?

  2. 2.What is the main cost of separating the read and write databases in CQRS?

  3. 3.In event sourcing, what is the source of truth?

  4. 4.Why do you take snapshots?

  5. 5.An event was recorded with a wrong amount. What is the correct fix in event sourcing?

0 of 5 answered

Finished this lesson?

0 of 91 lessons done · 0% of the course