What is event sourcing?

Event sourcing is a pattern in which a system stores every change to its state as an immutable, ordered event, and works out current state by replaying those events instead of overwriting rows.

Also called event sourcing pattern, event-sourced system, event storeUpdated

A normal database row holds the latest value. When a wallet balance goes from ₹500 to ₹300, the ₹500 is gone unless you built a separate audit table. Event sourcing turns this around. The system stores the facts that changed the balance, MoneyAdded and MoneySpent, and the balance is whatever those facts add up to.

Martin Fowler wrote up the pattern in 2005 as "capture all changes to an application state as a sequence of events". Accountants have worked this way for centuries. A ledger is a list of entries, and a balance is a sum, which is why event sourcing feels natural next to a double-entry ledger.

How it works

  • Each aggregate (a wallet, an order, a loan) has its own stream of events, stored in order with a version number.
  • To handle a command, the system loads the stream, replays it to get current state, checks business rules, and appends new events.
  • Appends are conditional. "Write event 8 only if the stream is still at version 7." If another request got there first, the write fails and is retried. This prevents lost updates without locks.
  • Events are never updated or deleted. Mistakes are fixed with a correcting event, such as RefundIssued.
  • Long streams use snapshots, so a load starts from the state at version 5,000 instead of replaying from zero.
  • Projections consume the events to build read models for queries. This is where CQRS comes in, because an event log is a poor thing to query directly.

Example: a wallet stream

stream wallet-7731
v1  WalletOpened     { customerId: 42 }
v2  MoneyAdded       { amountPaise: 50000, ref: "topup-7f3c" }
v3  MoneySpent       { amountPaise: 20000, ref: "ord_5521" }
v4  MoneySpent       { amountPaise: 9900,  ref: "ord_5530" }
v5  RefundIssued     { amountPaise: 9900,  ref: "ord_5530" }

Replaying gives a balance of 50000 − 20000 − 9900 + 9900 = 30000 paise (₹300). A spend of ₹400 is rejected, because replaying shows only ₹300. Two simultaneous spends both read version 5, but only one append at version 6 succeeds.

Support can now answer "what was the balance on 3 November, and why?" by replaying up to that date. A new fraud feature can be built later by replaying every stream from the start.

Event sourcing vs event-driven architecture

They are often confused. Event-driven architecture is about services communicating through events. Event sourcing is about how a single service stores its own state. You can have either without the other. Internal events in an event store are usually fine-grained and private. Publish separate, stable integration events for other services rather than exposing your internal stream.

Kafka is good for distributing events, but most teams don't use it as the event store. It doesn't offer an efficient per-aggregate stream with conditional appends. Dedicated event stores, or a PostgreSQL table with a unique constraint on (stream_id, version), are the common choices.

When not to use it

  • CRUD-style domains such as settings, catalogues and CMS content, where history has little value.
  • Teams new to the pattern on a deadline. Schema changes to events, replays and projections take real effort.
  • Where personal data must be erasable. Under laws such as the DPDP Act or GDPR, immutable events holding personal data need a plan, for example storing the data separately or encrypting it with a per-customer key you can destroy.

For a ledger design that gives an audit trail without full event sourcing, see design a payments ledger that reconciles.

Any idea in mind? Let's make it real.

Tell us what you're building. You'll hear back from an engineer, not a sales team, usually within one working day.

Book a 30-min call