Event Sourcing

ES

Escrito por Darjus Kazakevic · revisado el

A full history of every change, at the price of versioning it forever.

A normal table keeps only the current state. You know the balance is €40, but not that it was €90, or why it changed. Event sourcing stores the facts instead: Deposited, Withdrawn, Corrected. The balance is computed by replaying them, so the audit trail is the storage model itself.

The costs come later. Events never change, so a new schema means upcasting old events (converting them to the new shape when read), not migrating rows. Queries need projections, which lag behind. Long streams need snapshots. Use it where history is a real requirement, not by default.

En esencia — Save each change as an immutable event in an append-only log, and rebuild the current state by replaying the events in order.

Cuándo recurrir a él

Recurre a él cuando

  • History is a requirement: audits, disputes, “what was it on the 3rd?”
  • The business already talks in events: deposited, shipped, refunded
  • Several read models must be built, and rebuilt, from the same facts

Reconsidéralo cuando

  • CRUD screens where nobody asks how a value got there
  • As the default store for every aggregate, “just in case”
  • Personal data that must be erased on request, with no plan for it

Conceptos clave

Event store
An append-only database of events, grouped into streams, usually one per aggregate (account-42). Each event has a version inside its stream. You only append and read; you never update or delete.
Projections
Code that reads events and builds a read model, such as a balances table or a search index. Projections lag a little behind the store. You can delete one and rebuild it from the first event.
Snapshots
A saved copy of an aggregate's state at version N. Loading reads the snapshot and replays only the events after it. Worth adding when a stream grows past a few hundred events.
Upcasting
Old events stay in the store forever in their old shape. An upcaster converts an old version to the current one when it is read, so domain code only sees the latest shape.

Reglas de diseño

Keep apply() pure and free of rules
apply() replays events written years ago. Put new business rules where the decision is made, before the event is written. A rule added to apply() quietly changes the past.
Version every event from the first one
Add a version field to the first event you write. Then a schema change is a small upcaster from v1 to v2. Without one, you must guess the shape of every event already stored.
Append with an expected version
Two requests can change one stream at once. Pass the version you loaded, and the store rejects the append if another writer got there first. This optimistic check stops lost updates.
Name events as business facts
Write FundsWithdrawn, not AccountUpdated. A fact the business recognises still makes sense in five years. A row-change event only copies the table and loses the reason.

Errores habituales

  • Business rules inside apply(). Replaying old events then gives a different balance from the one users saw.
  • Events like AccountUpdated that copy the row. The log grows, but nobody can tell why anything changed.
  • Busy streams with no snapshots. Loading one aggregate takes seconds, then requests time out.
  • Publishing raw internal events to other services. Their shape becomes a public contract you can no longer change.