100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Event Sourcing

AdvancedTechnique4.7K learners

Event sourcing is an architectural pattern in which every change to an application's state is captured as an immutable sequence of events, and the current state is derived by replaying those events, rather than storing only the latest…

Definition

Event sourcing is an architectural pattern in which every change to an application's state is captured as an immutable sequence of events, and the current state is derived by replaying those events, rather than storing only the latest state directly.

Overview

In conventional application architectures, a database table typically stores only the current state of an entity — updating a record overwrites the previous values, and history is lost unless separately logged. Event sourcing inverts this: instead of persisting current state, the system persists an append-only, ordered log of domain events (such as `OrderPlaced`, `OrderShipped`, `OrderCancelled`) that represent every meaningful change that has ever happened to an entity. The entity's current state is not stored directly; it is computed on demand by replaying all of its events in order, folding each one into an accumulator (a pattern conceptually identical to a functional `reduce`). This approach, popularized in the enterprise architecture community by Martin Fowler and closely associated with Domain-Driven Design (and often paired with CQRS, Command Query Responsibility Segregation), provides a complete, auditable history of everything that has ever happened in the system — useful for compliance, debugging, and analytics, since you can answer not just 'what is the current state' but 'how did we get here' and 'what did the state look like at any prior point in time.' Because events are immutable and appended rather than overwritten, event sourcing also naturally supports temporal queries, retroactive bug analysis, and rebuilding read models or projections from scratch if a bug in a projection is later fixed. The pattern comes with significant tradeoffs. Replaying a long event stream for every read is expensive, so systems typically use snapshots (periodically persisted state, so replay only needs to resume from the latest snapshot) and materialized read-model projections that are kept up to date as events arrive, queried directly instead of ever replaying the full stream. Event sourcing also complicates schema evolution — old events must remain interpretable as the event schema evolves, requiring versioning strategies (upcasting old event formats, weak schemas) — and it introduces eventual consistency between the write-side event log and read-side projections. It is best suited to domains where auditability, historical reconstruction, or complex business-event history genuinely matter (financial ledgers, order management, inventory systems), and is often considered overkill for simple CRUD-heavy applications.

Key Concepts

  • Persists an append-only, immutable log of domain events instead of current state
  • Current state is derived by replaying (folding/reducing) events in order
  • Provides a complete, auditable history of every change ever made
  • Enables temporal queries — reconstructing state as of any past point in time
  • Typically paired with CQRS, separating write-side events from read-side projections
  • Uses snapshots to avoid replaying entire event streams on every read
  • Supports rebuilding read-model projections from scratch after fixing projection bugs
  • Introduces eventual consistency and schema-evolution complexity as tradeoffs

Use Cases

Financial ledgers and accounting systems requiring a full audit trail
Order management and inventory systems needing complete change history
Regulatory/compliance-heavy domains where every state change must be traceable
Debugging production issues by replaying the exact sequence of events that led to a bug
Building multiple read-model projections (analytics, search, reporting) from one event source
Collaborative or versioned systems needing undo/point-in-time state reconstruction

Frequently Asked Questions

From the Blog