Event Sourcing
Store every change as an immutable event in an append-only log, and derive the current state by replaying them, instead of overwriting rows.
Description
A normal database keeps only the latest state: the account balance is 120, and how it got there is gone. With event sourcing, the events are the source of truth: AccountOpened, MoneyDeposited 100, MoneyWithdrawn 30, MoneyDeposited 50. They're appended to an event store and never changed or deleted.
Current state is computed by replaying an entity's events, usually helped by periodic snapshots. The log gives you a complete audit trail, the ability to answer "what was the state on March 3rd?", and new read models built later by replaying history. Queries usually go to separate projections, which makes CQRS a natural partner.
System design
Event Sourcing system design diagram
- Banking app (client).
- Account service (service).
- Event store (database): append-only log.
- Projector (function).
- Balance view (database): read model.
- Step 1: Banking app → Account service: withdraw 30.
- Step 2: Event store → Account service: load events, rebuild state.
- Step 3: Account service → Event store: append MoneyWithdrawn.
- Step 4: Event store → Projector: new events.
- Step 5: Projector → Balance view: balance = 120.
- The customer withdraws 30.
- The service loads the account's events (or the latest snapshot plus newer events) and replays them to know the current balance.
- If the withdrawal is allowed, it appends a MoneyWithdrawn event. Nothing is updated or deleted.
- Projectors receive new events from the store.
- They update read models such as the current-balance view. A brand-new view can be built at any time by replaying the whole log.
- AWS
- DynamoDB (append-only table + Streams) or Amazon MSK/Kinesis as the log; EventStoreDB on EC2.
- Azure
- Cosmos DB with change feed; Event Hubs for streams.
- Google Cloud
- Firestore or Spanner append-only tables with change streams; Pub/Sub for distribution.
When to use it
Use it when
- A full, trustworthy history of changes is a requirement: finance, ledgers, compliance, healthcare.
- The business cares about how things happened, not just the end state, for example analysing abandoned carts.
- You want to build new views or fix projection bugs by replaying past events.
- You need temporal queries or the ability to rebuild state at any point in time.
Avoid it when
- It's a CRUD application where the history doesn't matter. The complexity is high and constant.
- The team is new to event-driven design and the deadline is tight.
- You need to hard-delete personal data on request. Possible (crypto-shredding), but it needs planning up front.
Pitfalls
- Event schema changes: old events live forever, so every version must stay readable. Version events and write upcasters.
- Replaying long histories becomes slow. Take snapshots.
- Using events as a generic CRUD log ("FieldChanged") instead of meaningful business events.
- Queries against the event store itself. Build projections for reading.
Alternatives
Audit table
Choose it when you just need a record of who changed what; a history table next to normal state is far simpler.
CQRS
Choose it when you need separate read and write models but not the full event history as source of truth.
Transactional Outbox
Choose it when you need reliable events for other services while keeping normal state storage.
Memento
Choose it when you want undo inside one application, not a durable history.