What is event sourcing
Instead of storing where you are, store how you got there.
Event sourcing persists every change as an immutable event in an append-only log, rather than overwriting the current state. Current state is derived by replaying those events, and projections build read-optimized views. You gain a perfect audit trail and the ability to time-travel, at the cost of more complexity and eventual consistency.
A command wants to change state
Something happens in the domain — a customer places an order, cancels it, or updates an address. In a traditional system you would find the current record and overwrite it, destroying whatever was there before. Event sourcing takes a different path: it treats the change as a fact to be recorded, not a value to be replaced.
Append an immutable event
Rather than updating a row, the system appends an event like OrderPlaced or AddressChanged to an append-only log. The event is immutable — it describes something that already happened, so it is never edited or deleted, only followed by more events. This log of facts, in order, becomes the single source of truth for the entire system.
Rebuild state by replaying
Because state is no longer stored directly, current state is derived by replaying the events in order and applying each one. Replaying every deposit and withdrawal, for example, reconstructs the exact current balance. To avoid replaying millions of events each time, systems periodically save snapshots and replay only the events since the last snapshot.
Build projections
A projection is a consumer that reads the event stream and builds a read model shaped for a particular query — a current-orders table, a search index, an analytics rollup. Because the same events can feed many projections, you can create as many purpose-built read views as you need, and rebuild any of them from scratch by replaying the log. The write model and read models are now cleanly separated.
Audit and time-travel
Since every change is preserved forever, the log is a perfect audit trail: you can answer not just what the state is, but exactly how and when it got there. You can also time-travel — replay events only up to a chosen point to reconstruct the state as it existed at any past moment. This is invaluable for debugging, compliance, and analytics.
Accept eventual consistency
The read models are updated by consuming events after they are written, so for a brief window a projection can lag behind the log. The system is eventually consistent: a write is durable immediately, but the query view catches up a moment later. Deciding whether that small delay is acceptable is central to choosing event sourcing at all.
The idea — store the journey, not the destination
Almost every application you've built stores the *current state* of things. A user's row holds their current email; an order's row holds its current status. When something changes, you overwrite the old value with the new one. This is the CRUD model — create, read, update, delete — and it is simple and effective, but it throws away history. Once you overwrite status = shipped with status = delivered, the fact that it was ever "shipped," and *when*, is gone forever.
Event sourcing inverts this. Instead of storing current state and mutating it, you store the full, ordered sequence of events that led to that state, and you never overwrite anything. The current state is not stored at all — it is *derived* by replaying the events. The classic intuition is a bank account: rather than keeping a single balance field, you keep every deposit and withdrawal, and the balance is simply their sum. The list of transactions is the truth; the balance is a calculation. Event sourcing applies that ledger discipline to any domain.
The append-only log and immutable events
At the center of an event-sourced system is the event store: an append-only log of events. Two properties make it powerful.
First, events are immutable. An event records something that *already happened* — OrderPlaced, ItemAddedToCart, AddressChanged — and you cannot change the past. So events are never updated or deleted; a mistake is corrected by appending a *new* compensating event (a Refund follows a Charge), not by editing the old one. This immutability is what gives the log its integrity: it is a permanent, tamper-evident record of facts.
Second, events are ordered. The sequence matters, because applying Deposited $100 then Withdrew $30 yields a different result than the reverse. The log preserves this order, so replaying it always reproduces the same state deterministically.
An important nuance is that events should capture *intent and meaning*, not just data diffs. CustomerMovedHouse carries far more business value than a generic address_updated, because it tells you *why* the change happened — and that richness is exactly what makes the log valuable for analytics and audit later.
Rebuilding state and building projections
If you only store events, how do you answer "what is the balance right now?" You replay: start from the beginning, apply each event in order, and the final result is the current state. This is elegant but has an obvious performance problem — replaying millions of events on every read is far too slow.
The standard fix is snapshots. Periodically, the system saves the fully-computed state at a point in time, so to get current state you load the latest snapshot and replay only the handful of events that came after it. Snapshots are pure optimization; the log remains the source of truth and could always rebuild everything from scratch.
For serving queries, event-sourced systems use projections (also called read models). A projection is a consumer that subscribes to the event stream and builds a data structure optimized for a specific query — a flat "current orders" table, a search index, a dashboard aggregate. The key benefit is that you can build *many different* projections from the *same* events, each shaped for its purpose, and you can rebuild any projection at any time by replaying the log. If you realize you need a new report next year, you just write a new projection and replay history through it — data you didn't know you'd want is already there. This clean split between the write-side log and the read-side projections is precisely why event sourcing pairs so naturally with CQRS.
The superpowers — audit and time-travel
Two capabilities fall out of event sourcing almost for free, and for some systems they are the entire reason to adopt it.
The first is a perfect audit trail. Because every state change is stored permanently as a discrete, meaningful event, you can answer not just *what* the current state is, but exactly *how, when, and in what order* it got there. For domains with regulatory or financial requirements — banking, healthcare, accounting, anything that must prove its history — this is enormously valuable. The audit log isn't a bolt-on feature you have to remember to write; it *is* the database.
The second is time-travel. Since state is derived by replaying events, you can replay only up to a chosen moment and reconstruct exactly what the state looked like at that past point. Want to know a customer's account status as of last New Year's Eve? Replay the events up to that timestamp. This is a superpower for debugging ("what did the system actually see when this bug occurred?"), for analytics, and for reproducing production issues. No CRUD system can do this, because CRUD deleted the information the moment it overwrote a value.
Trade-offs and when to use it
Event sourcing is powerful but genuinely harder, and using it where it isn't warranted is a common and costly mistake.
- Complexity. You give up the simple, familiar "just update the row" model. Rebuilding state, managing snapshots, and reasoning about a system where the current state is implicit all raise the cognitive and operational load.
- Schema evolution. Events are immutable and live forever, but your code changes. Handling old event versions that were written years ago — and still replaying them correctly — is an ongoing challenge that needs deliberate versioning strategy.
- Eventual consistency. Projections are updated by consuming events *after* they're stored, so read models lag the write side by a small window. A user might not instantly see their own change reflected in a query view. You must design for this delay.
- Querying. You cannot easily run an ad-hoc "current state" query against a log of events — that's what projections are for, which means you have to anticipate your read needs.
Given these costs, event sourcing is *not* a default. Reach for it when history is a first-class requirement — auditing, compliance, financial ledgers — when you need temporal queries or the ability to derive new views from past data, or when the domain is naturally a sequence of events (order lifecycles, workflow state machines). For a simple CRUD app where nobody will ever ask "how did this get here?", a plain database is the right, cheaper answer. The discipline is matching the pattern to a domain that actually benefits from remembering everything.
Store the sequence of state-changing events, not just the latest state — the log is the source of truth.
The event log is append-only and immutable: events are facts that already happened and are never edited or deleted.
Current state is rebuilt by replaying events from the beginning (often accelerated with snapshots).
Projections consume the event stream to build read models tailored to specific queries.
You get a complete audit trail and time-travel — reconstruct the state at any past moment.
Trade-offs: more complexity, schema evolution challenges, and eventual consistency between write and read sides.
Quick quiz
In event sourcing, what is the source of truth?
Why are snapshots used in event-sourced systems?
How CQRS works
Reads and writes want different things — so give them different models.
How Apache Kafka works
A distributed commit log that decouples who produces data from who consumes it.
Batch vs stream processing
Process the data after it piles up, or the instant it arrives.
Key terms in domain-driven design
A shared language and a toolkit of patterns for modeling complex business domains.