Architectureevent storeevent streamreplay

Event sourcing

Definition

Event sourcing persists an aggregate as an append-only stream of events; its current state is a fold over those events, and new events are appended with an expected stream version.

In practice

Storing facts rather than snapshots gives a full audit trail, answers "what was the state at time T?", and lets you build new read models by replaying history from position 0. Appending with an expected version gives optimistic concurrency: two writers to the same stream conflict cleanly.

Events are forever, so schema changes need versioning (upcasters), and deleting personal data needs techniques such as crypto-shredding. Long streams make loading slow without snapshots, and queries need projections that lag behind. It suits domains where history matters, not every CRUD table.

Example

Account acc-7 stores Opened, Deposited(100), Withdrawn(30); loading replays them to a balance of 70, and appending Withdrawn(80) with expected version 3 fails if another write landed first.