Skip to content

Event Sourcing Primer

What Is Event Sourcing?

Event Sourcing stores state as a sequence of events rather than as a current snapshot. Instead of updating a row in a database, you append an event describing what happened.

The Core Pattern

Command → Decide → Events → Evolve → State
  1. A command expresses intent (“Add item to cart”)
  2. The decide function checks business rules against current state
  3. On success, it produces events (“Item was added”)
  4. The evolve function applies events to produce new state
  5. The new state is available for the next decision

Why Events?

  • Audit trail: Every state change is recorded
  • Time travel: Replay events to reconstruct any historical state
  • Decoupling: Other systems react to events, not commands
  • Debugging: The exact sequence of facts is always available

Why Formal Verification?

Runtime frameworks check correctness through tests. Weltenwanderer checks correctness through proofs.

The evolve function — a left-fold over events — is structurally a finite automaton when the state type is finite. All properties of finite automata are decidable. The compiler exploits this to verify exhaustiveness, consistency, and postconditions without executing code.

Architecture Decisions

Further Reading