Notesstream🌱 seedling

Event Sourcing

Also: Event-Sourced · Event Store · Event Log as Source of Truth

Store every change to application state as an immutable, past-tense event in an append-only log, and derive current state by replaying the events. The log is the system of record; tables are views of it.

Why it exists

Mutable rows keep only the latest value and lose why it changed. An event log keeps the full history: audit, debugging, and new views built from old data.

How it works

  • A command ("withdraw 500") is validated against current state and may be rejected. If accepted, it produces events ("Withdrew 30"), facts that are never updated or deleted.
  • Current state is a fold over the events for an entity (aggregate). Projections build read models from the same log.
  • Mistakes are corrected by appending a compensating event, like a reversing entry in a ledger.
  • Snapshots persist state every N events so a rebuild replays only the tail.
  • Old event versions are upcast to the current shape on read, because history is never rewritten.

Trade-offs

Gains Costs
complete audit trail, temporal queries every event version must be readable forever
new read models by replaying history eventual consistency between writes and reads
events express domain intent, not row diffs replay must suppress side effects (emails, payments)
append-only writes deleting personal data conflicts with immutability

In practice

  • Event stores such as KurrentDB (formerly EventStoreDB) and Axon Server; many teams use Postgres or Kafka instead.
  • Usually paired with CQRS: the event log is the write model, and projections are the read models.

Pitfalls

  • Applying it to a whole system, including simple CRUD, where it adds cost and no value.
  • Fine-grained "field changed" events that are really CDC in disguise and carry no intent.
  • Erasure: plan for Crypto-Shredding (per-subject keys) from day one.

Recall

These cards are also in your review deck, scheduled with spaced repetition.