Event Sourcing
In the familiar CRUD model a table row holds the current value, and the past is lost on every UPDATE. Event sourcing flips this: the source of truth becomes an append-only event log, and the current state is a derived value obtained by replaying events in order. An interview checks not the term but the consequences: how the event store works, where invariants are checked, what to do about concurrent writes, long streams, and a changing schema.
The central trap is treating event sourcing as "CRUD plus an audit log". An audit log is a side copy that can drift from the data; here the log is the data. Everything else follows: events are immutable past-tense facts, Apply may not fail or call external services, a snapshot is only a cache, and an event's schema evolves by upcasting on read rather than by migrating history. Event sourcing is often combined with CQRS, but neither approach requires the other. Delivering events to subscribers is not re-taught here: the outbox and delivery guarantees live in Messaging, and compensation in the SAGA pattern.
Topic Map
- Event sourcing basics — events as the source of truth, state as a fold over events, and the difference from CRUD with an audit log.
- The event store — append-only streams, optimistic concurrency via an expected version, a Postgres schema, and erasing personal data.
- The aggregate and its loading —
Handlechecks invariants and decides,Applyonly changes state, loading is replaying the stream. - Snapshots — when replay gets expensive, how to load from a snapshot, and why a snapshot is a cache.
- Event versioning — history is never rewritten: additive changes, new event types, and upcasting on read.
- Process manager — a stateful coordinator that reacts to events, sends commands, and handles timeouts.
Common Mistakes and Traps
| Mistake | Consequence |
|---|---|
| Treating the event log as an audit table next to the "real" table | Two sources of truth that drift apart at the first failure |
| Storing commands instead of events | Replay re-runs validation, and history yields a different result |
Fixing a wrong event with UPDATE | Consumers and projections already saw the old version — history is no longer trustworthy |
| Appending to a stream without an expected version | Two concurrent commands bypass an invariant — say, push a balance below zero |
Checking business rules in Apply | Loading fails or silently skips a fact that already happened |
Calling external services from Apply | An email or payment repeats on every load and rebuild |
| Deleting events covered by a snapshot | The snapshot stops being a cache — it can no longer be rebuilt |
| Serializing the aggregate itself into a snapshot without a version | After a struct refactor, old snapshots silently yield the wrong state |
Migrating history with UPDATE when the schema changes | The original facts are lost and built projections fall out of sync |
| Putting personal data into events in plain text | An erasure request cannot be honoured without rewriting the log |
| Keeping process-manager timeouts in in-process timers | A restart loses the deadlines — the process hangs forever |
Interview Relevance
Event sourcing comes up less often than messaging, but almost always together with CQRS and DDD, in roles where money, auditing, or complex business processes matter. The interviewer checks whether you understand the cost of the approach, not just its benefits.
What interviewers usually check:
- How to get the current state and how an event log differs from an audit log.
- Whether CQRS is required for event sourcing and why the two are often used together.
- How the event store handles two concurrent writes to one stream — the expected version and a command retry.
- Where invariants are checked (
Handle) and whyApplycannot fail or call external services. - When snapshots are needed and what to do when the aggregate's struct changes.
- How to change an event's schema when old events are stored forever.
- How to honour a personal-data erasure request in an immutable log.
- How a process manager differs from an event handler and from choreography.
A typical wrong answer: "event sourcing is when we additionally write every change to a log." That immediately reveals the "table plus audit" model and leads to a discussion that in event sourcing the log is the only source of truth, while tables are just derived projections that can be dropped and rebuilt.