Skip to main content

What is event sourcing?

Event sourcing is an approach to data storage in which the system stores not the current state of an object, but the sequence of all events that led to that state.

The essence

Instead of the usual "overwrite the data" model, the system records every change as an event:

"Order created", "Item added", "Order paid".

The current state can be obtained by replaying all the events in order.

Example

A regular database stores:

javascript
Order: {id: 1, status: "paid"}

Event sourcing stores events:

javascript
1. OrderCreated 2. ItemAdded 3. PaymentReceived

The "paid" state is computed when needed.

Why this is needed

  1. Full history of changes: you can precisely reconstruct what happened and when.
  2. Audit and tracing: suits financial, logistics, and legal systems.
  3. Easy to react to events: other services can subscribe to them.
  4. Deferred computation: you can recompute the state when the logic changes.

Drawbacks

  • Harder to implement: requires replaying events and building state projections.
  • Data grows faster.
  • Not always convenient for simple CRUD applications.

Summary:

Event sourcing stores the history of changes, not the result. This makes the system transparent, reproducible, and well suited to event-driven architecture, but it requires carefully managing the event stream and the storage.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.