Skip to main content

Event Sourcing

Updated 3 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/event-sourcing

In short

Event sourcing is a design pattern that stores every change to an application's state as an immutable event and rebuilds current state by replaying them.

What is event sourcing?

Event sourcing is an architectural pattern in which you store what happened, not just the end result. Instead of updating a row to hold the latest balance, the system appends events such as AccountOpened, MoneyDeposited, and MoneyWithdrawn to an append-only log called an event store. The current state is calculated by replaying those events in order.

Events are immutable: once written, they are never edited or deleted, so mistakes are fixed by adding a new correcting event. Because replaying thousands of events on every read would be slow, systems periodically save snapshots of the state and replay only the events that came after the latest snapshot. They also build read models, or projections, which are tables optimized for queries and kept up to date by listening to new events.

A bank statement is a good analogy: your balance is not a single number you must trust blindly, but the sum of every deposit and withdrawal listed on the statement. Event sourcing is popular in finance, accounting, order processing, and any domain that needs a full audit trail, the ability to answer 'what did this look like last Tuesday?', or the option to rebuild data in new shapes later.

Event sourcing is often confused with event-driven architecture. Event-driven architecture is about services communicating by publishing and reacting to events, while event sourcing is about using events as the storage model for a service's own data, and you can use either without the other. It is frequently paired with CQRS, which separates the write side that records events from the read side that serves queries. The trade-offs are real: event formats must evolve carefully, and read models are often eventually consistent, meaning they may briefly lag behind the latest events.

At a glance

Event sourcing for a bank account. Instead of storing only the balance, the system appends every change to an event log: account opened, 100 deposited, 30 withdrawn, 50 deposited. Replaying the events in order gives the current balance of 120, and the same events also feed a read model such as a monthly statement. A new event is simply appended at the end.event log (append only)1AccountOpened2Deposited 1003Withdrew 304Deposited 50next event is appended herereplay in orderBalance: 1200 + 100 − 30 + 50projectMonthly statementa read model
The log of events is the source of truth; the balance is calculated from it, so the full history is never lost and new views can be built from it later.

Key takeaways

  • State changes are stored as an append-only sequence of immutable events.
  • Current state is rebuilt by replaying events, often starting from a snapshot.
  • It provides a complete audit trail and makes it possible to reconstruct past states.
  • It is often combined with CQRS, but it is not the same as event-driven architecture.
  • Evolving event formats and eventually consistent read models add complexity.

Example

Rebuilding state by replaying eventstypescript
type AccountEvent = { type: "MoneyDeposited" | "MoneyWithdrawn"; amount: number };

// The event store only ever appends; past events are never changed
const events: AccountEvent[] = [
  { type: "MoneyDeposited", amount: 100 },
  { type: "MoneyWithdrawn", amount: 30 },
  { type: "MoneyDeposited", amount: 50 },
];

// Rebuild the current balance by replaying every event in order
const balance = events.reduce(
  (total, e) => (e.type === "MoneyDeposited" ? total + e.amount : total - e.amount),
  0,
);
console.log(balance); // 120

Readers ask

What is the difference between event sourcing and event-driven architecture?

Event sourcing stores a service's own data as a log of events, while event-driven architecture uses events to let separate services communicate. A system can use one without the other, though they are often combined.

What is an event store?

An event store is a database or log designed to append events and read them back in order, usually grouped per entity, such as all events for one account. It can be a specialized database or an ordinary table used in an append-only way.

When should you not use event sourcing?

Avoid it for simple create, read, update, and delete applications where history has little business value. It adds complexity around event versioning, replays, and eventually consistent reads, which only pays off when auditability or questions about past states really matter.

See also

Sources

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings