Event Sourcing Pattern
Event Sourcing: start from the problem
See the responsibilities
A stock record shows only its current quantity. A team also needs a precise history of accepted stock changes and a way to rebuild selected views. Storing those accepted events as authority changes the model and its obligations.
This is a data and messaging pattern lesson. Follow the responsibilities, inspect the small JavaScript example and compare the design choices. The example is local teaching code; its boundary is explained before you apply the idea to a real service.
What you’ll learn
- Explain the problem Event Sourcing addresses and the roles involved.
- Follow the example and distinguish this pattern from nearby alternatives.
- Choose when the benefit justifies the extra responsibilities and testing.
How the responsibilities work
Event Sourcing stores an ordered authoritative history of accepted events and derives state from that history. Commands validate against the appropriate current state and append under a supported concurrency rule. Projections can rebuild useful views from retained events.
The example folds a small ordered stock history into a quantity. It does not implement a durable event store, concurrent append or an input validator. Replaying state calculations must be kept separate from resending external business effects.
Follow a small example
Read the JavaScript from top to bottom and compare the expected output. The values are chosen to make this pattern’s responsibility visible. This example runs in one local execution context; it does not create remote services, durable storage or production access controls.
JavaScript · local teaching example
const events = [{type: 'received', count: 10}, {type: 'reserved', count: 3}];
function quantity(history) {
return history.reduce((total, event) =>
event.type === 'received' ? total + event.count : total - event.count, 0);
}
console.log(quantity(events));Expected output
7Choose a design and inspect the consequence
Compare the design choices
Compare the visible consequences. With JavaScript enabled, choose an option in each scenario to assemble your decision record.
This is an exploratory design exercise. It does not run infrastructure, measure capacity or replace the lesson’s quiz.
Apply the pattern to two situations. Select one option in each situation to build a decision record. Every option and its consequence stays visible, and you can change your choices.
When it helps—and what it costs
Use it when authoritative history and rebuildable views justify the additional event-model responsibility. It is not required merely because the service publishes notifications.
Events need stable meaning, version handling and a privacy/retention policy. Concurrent appends must preserve invariants. Snapshots can accelerate replay but are derived state with their own validation. Rebuilding a view must not charge customers again.
Distinguish the neighbouring patterns
Pub/Sub distributes messages. Outbox bridges a local write and later publication. Event Sourcing makes the history itself authoritative; CQRS can separate its commands and queries.
Follow events already covered in the architecture lesson, separate command and read responsibilities, publication intent rather than source history to compare the specific responsibility before combining patterns.
For further reading, use Microsoft’s Event Sourcing guidance. Compare it with this original example and the guarantee required by your application.
Check your pattern reasoning
Check your pattern reasoning
Compare the pattern boundaries
Name the responsibility before choosing the pattern
For Event Sourcing, describe the problem, the participating roles and the observable contract. Use the example as a starting point and test the boundaries identified in the trade-offs. A pattern name alone does not establish correctness or performance.
Return to the design-pattern guide to compare families, or apply the idea alongside publication intent rather than source history.
Reaching the recap and completing the quizzes are separate milestones.