Bird
Raised Fist0
HLDsystem_design~15 mins

Event sourcing in HLD - Deep Dive

Choose your learning style9 modes available
Overview - Event sourcing
What is it?
Event sourcing is a way to store data by saving every change as a sequence of events instead of just the current state. Each event records what happened, like a log of actions. This lets systems rebuild the current state by replaying all events in order. It is different from traditional databases that only save the latest snapshot of data.
Why it matters
Without event sourcing, systems lose the history of changes and can only see the current state. This makes it hard to audit, debug, or understand how data evolved. Event sourcing solves this by keeping a full history, enabling better tracking, recovery, and flexibility. It is especially useful in complex systems where understanding past actions is critical.
Where it fits
Learners should know basic database concepts and how data is usually stored and updated. After event sourcing, they can explore related topics like Command Query Responsibility Segregation (CQRS), distributed systems, and eventual consistency. Event sourcing fits into the broader journey of designing scalable and maintainable data architectures.
Mental Model
Core Idea
Event sourcing stores every change as an event, so the system’s state is rebuilt by replaying these events in order.
Think of it like...
Imagine writing a diary where you record every action you take each day instead of just writing how you feel at the end. To know your current mood, you read all diary entries from the start. This way, you never lose any detail about what happened.
┌───────────────┐   ┌───────────────┐   ┌───────────────┐
│ Event 1: Add │ → │ Event 2: Update│ → │ Event 3: Delete│
└───────────────┘   └───────────────┘   └───────────────┘
         ↓                 ↓                   ↓
    ┌─────────────────────────────────────────────┐
    │ Replaying events in order rebuilds the state │
    └─────────────────────────────────────────────┘
Build-Up - 7 Steps
1
FoundationUnderstanding traditional state storage
🤔
Concept: Learn how systems usually store only the current state of data.
Most systems save data by overwriting the previous value with the new one. For example, a bank account balance is updated directly when money is deposited or withdrawn. The system only knows the latest balance, not how it got there.
Result
You understand that traditional storage loses the history of changes.
Knowing this helps you see why keeping only the current state limits auditing and debugging.
2
FoundationWhat is an event in computing
🤔
Concept: Introduce the idea of an event as a record of a change or action.
An event is a message that describes something that happened, like 'User logged in' or 'Order placed'. Events are immutable, meaning once created, they never change. They capture facts about the system's activity.
Result
You grasp that events are detailed records of actions, not just data snapshots.
Understanding events as facts allows you to build systems that track history precisely.
3
IntermediateHow event sourcing stores data
🤔Before reading on: do you think event sourcing stores full snapshots or just changes? Commit to your answer.
Concept: Event sourcing saves every change as an event instead of overwriting data.
Instead of updating a value, event sourcing appends a new event describing the change. For example, instead of changing a balance from 100 to 150, it records 'Deposited 50'. The current state is rebuilt by applying all events in order.
Result
You see that event sourcing keeps a complete, ordered history of changes.
Knowing that state is derived from events helps you understand how to recover or audit data.
4
IntermediateRebuilding state by replaying events
🤔Before reading on: do you think replaying events is fast or slow? Commit to your answer.
Concept: The system recreates current data by applying all stored events sequentially.
To get the current state, the system starts from an empty state and applies each event in order. For example, starting with zero balance, it adds deposits and subtracts withdrawals from the event list. This process is called replaying events.
Result
You understand how the system can always reconstruct the latest state from history.
Understanding replaying events reveals how event sourcing supports auditing and debugging.
5
IntermediateBenefits of event sourcing
🤔
Concept: Explore advantages like auditability, flexibility, and recovery.
Event sourcing keeps a full history, so you can see every change and why it happened. It allows rebuilding state at any point in time, making debugging easier. It also supports features like undo, replay, and integration with other systems.
Result
You appreciate why event sourcing is valuable in complex or regulated systems.
Knowing these benefits helps you decide when event sourcing is the right choice.
6
AdvancedHandling large event streams with snapshots
🤔Before reading on: do you think replaying all events is always practical? Commit to your answer.
Concept: Introduce snapshots to speed up state rebuilding by saving intermediate states.
Replaying thousands or millions of events can be slow. To fix this, systems save snapshots—complete states at certain points. When rebuilding, the system starts from the latest snapshot and replays only newer events. This balances performance and history.
Result
You learn how snapshots optimize event sourcing for real-world scale.
Understanding snapshots prevents performance bottlenecks in event-sourced systems.
7
ExpertEvent sourcing challenges and solutions
🤔Before reading on: do you think event sourcing makes data consistency easier or harder? Commit to your answer.
Concept: Discuss complexities like eventual consistency, event versioning, and data duplication.
Event sourcing can introduce challenges: events must be immutable and backward compatible, which requires versioning strategies. Systems often use eventual consistency, meaning data updates appear with delay. Also, integrating with other systems may require duplicating data or using CQRS to separate reads and writes.
Result
You understand the trade-offs and advanced techniques needed for production event sourcing.
Knowing these challenges prepares you to design robust, maintainable event-sourced systems.
Under the Hood
Internally, event sourcing uses an append-only log where each event is stored sequentially and never changed. The system maintains an event store, a specialized database optimized for fast writes and sequential reads. When the current state is needed, the system loads events from the store and applies them in order to reconstruct the state. Snapshots may be stored to reduce replay time. Events are immutable, ensuring a reliable audit trail.
Why designed this way?
Event sourcing was designed to solve problems of lost history and complex state changes in traditional databases. By storing immutable events, systems gain full traceability and easier debugging. Alternatives like storing full snapshots or partial logs were less flexible or efficient. The append-only log model also aligns well with distributed systems and scalability needs.
┌───────────────┐
│ Event Store   │
│ (append-only) │
└──────┬────────┘
       │
       ▼
┌───────────────┐      ┌───────────────┐
│ Event Replay  │─────▶│ Current State │
│ (apply events)│      │ (reconstructed)│
└───────────────┘      └───────────────┘
       ▲
       │
┌───────────────┐
│ Snapshots     │
│ (optional)    │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does event sourcing store only the latest state? Commit yes or no.
Common Belief:Event sourcing just stores the current state like a normal database.
Tap to reveal reality
Reality:Event sourcing stores every change as an event, not just the latest state.
Why it matters:Believing this causes confusion about how event sourcing supports auditing and recovery.
Quick: Is replaying all events always fast? Commit yes or no.
Common Belief:Replaying events to rebuild state is always quick and simple.
Tap to reveal reality
Reality:Replaying large event streams can be slow, so snapshots are used to optimize performance.
Why it matters:Ignoring this leads to performance problems in real systems.
Quick: Does event sourcing guarantee immediate consistency? Commit yes or no.
Common Belief:Event sourcing systems always have consistent data instantly after changes.
Tap to reveal reality
Reality:Event sourcing often uses eventual consistency, so data updates may appear with delay.
Why it matters:Assuming immediate consistency can cause bugs and wrong expectations in distributed systems.
Quick: Can events be changed after creation? Commit yes or no.
Common Belief:Events can be edited or deleted if mistakes happen.
Tap to reveal reality
Reality:Events are immutable and never changed; corrections are done by new events.
Why it matters:Trying to change events breaks the audit trail and system integrity.
Expert Zone
1
Event versioning is critical: as business rules evolve, events must be backward compatible or transformed to avoid breaking replay.
2
Choosing the right granularity for events affects system complexity and performance; too fine-grained events increase overhead, too coarse lose detail.
3
Integrating event sourcing with CQRS separates read and write models, improving scalability but adding complexity in synchronization.
When NOT to use
Event sourcing is not ideal for simple CRUD applications with low audit needs or where event replay performance is critical without snapshots. Alternatives include traditional relational databases or stateful caches. Also, systems requiring strict immediate consistency may find event sourcing challenging.
Production Patterns
In production, event sourcing is often combined with CQRS to separate commands and queries. Snapshots are regularly taken to optimize replay. Event stores like Apache Kafka or specialized databases are used. Versioning strategies and migration tools manage event schema changes. Monitoring and alerting track event processing health.
Connections
Command Query Responsibility Segregation (CQRS)
Event sourcing often pairs with CQRS to separate write and read operations.
Understanding event sourcing helps grasp why CQRS splits commands and queries for scalability and consistency.
Distributed Ledger Technology (Blockchain)
Both store immutable sequences of events or transactions.
Knowing event sourcing clarifies how blockchains maintain tamper-proof histories of actions.
Historical Research Methodology
Both reconstruct current understanding by analyzing past records sequentially.
Seeing event sourcing like historical research shows how past events build present knowledge.
Common Pitfalls
#1Replaying all events every time causes slow system response.
Wrong approach:Always rebuild state from the first event without snapshots, even with millions of events.
Correct approach:Use snapshots to store intermediate states and replay only recent events.
Root cause:Not realizing that replaying large event streams is costly and needs optimization.
#2Changing or deleting past events to fix errors.
Wrong approach:Edit or remove events in the event store to correct mistakes.
Correct approach:Append new compensating events that reverse or fix previous ones.
Root cause:Misunderstanding that events must be immutable to maintain a reliable history.
#3Expecting immediate consistency after event writes.
Wrong approach:Assuming that once an event is stored, all parts of the system see the updated state instantly.
Correct approach:Design for eventual consistency and handle delays in data propagation.
Root cause:Confusing event sourcing with traditional synchronous database updates.
Key Takeaways
Event sourcing stores every change as an immutable event, not just the current state.
The system’s current state is rebuilt by replaying all events in order, enabling full history and auditability.
Snapshots optimize performance by saving intermediate states to avoid replaying all events every time.
Event sourcing introduces challenges like event versioning and eventual consistency that require careful design.
It pairs well with CQRS and is powerful for complex, scalable, and auditable systems but is not always the best choice.