Event Sourcing
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
Frequently Asked Questions
From the Blog
Async Python: asyncio Explained for Beginners
Async Python lets a single thread handle hundreds of concurrent I/O operations — making it essential for web APIs, database calls, and AI integrations. This guide explains coroutines, the event loop, await, gather, and real patterns you'll use in FastAPI, httpx, and LLM streaming.
Read More Learn Through HobbiesLearn JavaScript Through Music: Build a Playlist App
Building a music player is one of the best JavaScript projects for beginners — it covers DOM manipulation, event listeners, the Fetch API, and the Web Audio element in a context that's genuinely fun. By the end you'll have a working Spotify-style playlist app you can embed anywhere.
Read More