Bird
Raised Fist0
Microservicessystem_design~15 mins

Event sourcing pattern in Microservices - Deep Dive

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Overview - Event sourcing pattern
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 represents a fact that happened in the system. To know the current state, you replay all these events in order. This pattern helps keep a full history of changes and makes systems easier to audit and debug.
Why it matters
Without event sourcing, systems only store the latest data, losing all history of how that data changed. This makes it hard to understand past actions, fix bugs, or recover from errors. Event sourcing solves this by keeping a complete record of every change, which helps in building reliable, scalable, and transparent systems, especially in complex microservices environments.
Where it fits
Before learning event sourcing, you should understand basic data storage and CRUD operations. After event sourcing, you can explore related patterns like Command Query Responsibility Segregation (CQRS) and distributed messaging. Event sourcing fits into the journey of designing resilient and scalable microservices architectures.
Mental Model
Core Idea
Store every change as an event, and rebuild current state by replaying these events in order.
Think of it like...
It's like keeping a diary where you write down every action you take each day instead of just writing your current mood. To know how you feel now, you read through all your diary entries from the start.
┌───────────────┐
│  Event Store  │
├───────────────┤
│ Event 1       │
│ Event 2       │
│ Event 3       │
│ ...           │
└─────┬─────────┘
      │ Replay events
      ▼
┌───────────────┐
│ Current State │
└───────────────┘
Build-Up - 7 Steps
1
FoundationUnderstanding traditional data storage
🤔
Concept: Learn how systems usually store only the latest data state.
Most applications save data by updating records directly. For example, a bank account balance is stored as a single number that changes with deposits or withdrawals. This means the system forgets how it got to that number.
Result
You see that traditional storage only keeps the current snapshot, losing history.
Understanding this limitation shows why keeping only the current state can hide important information about past changes.
2
FoundationWhat is an event in software systems
🤔
Concept: Introduce the idea of an event as a record of something that happened.
An event is a fact like 'User logged in' or 'Order placed'. It is immutable, meaning once recorded, it never changes. Events describe what happened, not just the result.
Result
You grasp that events capture actions or changes as discrete records.
Knowing that events are immutable facts helps understand why they are reliable building blocks for reconstructing state.
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 balance directly, event sourcing records 'Deposited $50' or 'Withdrew $20' as events. The current balance is calculated by applying all these events in order.
Result
You see that the system keeps a full timeline of changes, not just the end result.
Understanding that state is rebuilt from events explains how event sourcing preserves history and supports auditing.
4
IntermediateReplaying events to get current state
🤔Before reading on: do you think replaying events is fast or slow? Commit to your answer.
Concept: To find the current state, the system replays all stored events sequentially.
Imagine starting from an empty state and applying each event one by one. For example, starting with zero balance, add deposits and subtract withdrawals as events come in. This process rebuilds the current state exactly.
Result
You understand that the current state is not stored directly but computed from events.
Knowing replay is how state is derived clarifies why event sourcing can recover from errors by replaying events differently.
5
IntermediateEvent store and its role
🤔
Concept: Learn about the special database that holds all events in order.
An event store is a database designed to append events only, never update or delete them. It guarantees order and immutability. This store is the single source of truth for the system's state.
Result
You see that the event store is central to event sourcing and must be reliable and ordered.
Understanding the event store's role highlights why it must be designed for high availability and consistency.
6
AdvancedSnapshots to optimize event replay
🤔Before reading on: do you think replaying all events is always practical? Commit to your answer.
Concept: Snapshots save the state at a point in time to speed up rebuilding current state.
If there are thousands of events, replaying all can be slow. Snapshots store the state after a certain number of events. When rebuilding, the system starts from the latest snapshot and replays only newer events.
Result
You learn how snapshots improve performance without losing event sourcing benefits.
Knowing snapshots balance performance and history retention is key for scalable event sourcing systems.
7
ExpertHandling eventual consistency and concurrency
🤔Before reading on: do you think event sourcing guarantees immediate consistency? Commit to your answer.
Concept: Event sourcing often works with eventual consistency and requires careful concurrency control.
Because events are appended asynchronously, different parts of the system may see updates at different times. Also, multiple users might try to change the same data simultaneously, causing conflicts. Techniques like optimistic concurrency control and versioning help manage this.
Result
You understand the challenges and solutions for consistency and concurrency in event sourcing.
Recognizing these challenges prepares you to design robust systems that handle real-world distributed scenarios.
Under the Hood
Event sourcing stores each change as an immutable event in an append-only log called the event store. When the system needs the current state, it reads all events from the store and applies them in order to an empty state object. This process is called replay. To optimize, snapshots of the state at certain points are saved, so replay starts from the latest snapshot. The event store ensures strict ordering and immutability, often using sequence numbers or timestamps. Concurrency is managed by checking event versions before appending new events.
Why designed this way?
Event sourcing was designed to solve problems of lost history and complex state changes in distributed systems. Traditional databases overwrite data, losing change history. By storing events, systems gain auditability, easier debugging, and the ability to rebuild state after failures. The append-only log model simplifies replication and scaling. Alternatives like storing full snapshots only were rejected because they lose detailed change context and make debugging harder.
┌───────────────┐
│ Client/Service│
└──────┬────────┘
       │ Send command
       ▼
┌───────────────┐
│ Command Handler│
└──────┬────────┘
       │ Create event
       ▼
┌───────────────┐
│  Event Store  │
│ (append-only) │
└──────┬────────┘
       │ Store event
       ▼
┌───────────────┐
│ Event Replay  │
│ (build state) │
└──────┬────────┘
       │ Provide current state
       ▼
┌───────────────┐
│ Application   │
│ State         │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does event sourcing store only the latest state or all changes? Commit to your answer.
Common Belief:Event sourcing just stores the current state like a normal database.
Tap to reveal reality
Reality:Event sourcing stores every change as a separate event, not just the latest state.
Why it matters:Believing this causes confusion about how event sourcing preserves history and can lead to wrong implementations that lose audit trails.
Quick: Is replaying events always fast regardless of event count? Commit to your answer.
Common Belief:Replaying all events to get current state is always fast and simple.
Tap to reveal reality
Reality:Replaying many events can be slow; snapshots are needed to optimize performance.
Why it matters:Ignoring this leads to slow systems that degrade user experience and increase costs.
Quick: Does event sourcing guarantee immediate consistency across services? Commit to your answer.
Common Belief:Event sourcing always provides immediate consistency everywhere.
Tap to reveal reality
Reality:Event sourcing often works with eventual consistency, meaning some parts see updates later.
Why it matters:Assuming immediate consistency can cause design errors and bugs in distributed systems.
Quick: Can events be changed or deleted after being stored? Commit to your answer.
Common Belief:Events can be updated or deleted if mistakes happen.
Tap to reveal reality
Reality:Events are immutable and never changed or deleted to preserve history integrity.
Why it matters:Trying to modify events breaks audit trails and can corrupt system state.
Expert Zone
1
Event versioning is crucial to handle changes in event structure over time without breaking replay.
2
Choosing the right granularity of events affects system complexity and performance; too fine or too coarse can cause issues.
3
Integrating event sourcing with CQRS allows separating read and write models for better scalability and flexibility.
When NOT to use
Event sourcing is not ideal for simple CRUD applications with low complexity or where full history is unnecessary. Alternatives like traditional relational databases or document stores are better when audit trails and replay are not needed.
Production Patterns
In production, event sourcing is combined with CQRS to separate commands and queries. Snapshots and event versioning are used to optimize performance and handle schema evolution. Event buses or message queues distribute events to other microservices for eventual consistency and integration.
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 clarity.
Distributed Ledger Technology (Blockchain)
Both store immutable sequences of events or transactions.
Knowing event sourcing clarifies how blockchains maintain tamper-proof histories of changes.
Historical Research Methodology
Both reconstruct current understanding by analyzing past records sequentially.
Seeing event sourcing like historians studying archives helps appreciate the value of preserving every change.
Common Pitfalls
#1Replaying all events every time causes slow response.
Wrong approach:Always replay entire event log from the beginning for every request.
Correct approach:Use snapshots to start replay from a recent state and only apply newer events.
Root cause:Not realizing that replaying large event logs is inefficient and needs optimization.
#2Modifying events after storing to fix errors.
Wrong approach:Update or delete events in the event store when mistakes are found.
Correct approach:Append compensating events that correct previous mistakes without changing history.
Root cause:Misunderstanding immutability principle of events and audit trail importance.
#3Assuming immediate consistency in distributed event sourcing.
Wrong approach:Design system expecting all services to see events instantly and in sync.
Correct approach:Design for eventual consistency and handle out-of-order or delayed events gracefully.
Root cause:Ignoring distributed system realities and asynchronous event propagation.
Key Takeaways
Event sourcing stores every change as an immutable event, not just the current state.
The current state is rebuilt by replaying events in order, preserving full history.
Snapshots improve performance by reducing the number of events to replay.
Event sourcing often works with eventual consistency and requires concurrency control.
It is best suited for complex systems needing auditability, debugging, and scalability.

Practice

(1/5)
1. What is the main idea behind the Event sourcing pattern in microservices?
easy
A. Store all changes as a sequence of events instead of only the current state
B. Store only the latest snapshot of data for faster access
C. Use events only for communication between services, not for storage
D. Store events only when errors occur in the system

Solution

  1. Step 1: Understand event sourcing concept

    Event sourcing means saving every change as an event, not just the final state.
  2. Step 2: Compare options with definition

    Only Store all changes as a sequence of events instead of only the current state correctly describes storing all changes as events, others focus on snapshots or partial use.
  3. Final Answer:

    Store all changes as a sequence of events instead of only the current state -> Option A
  4. Quick Check:

    Event sourcing = storing all changes as events [OK]
Hint: Event sourcing saves every change, not just the final state [OK]
Common Mistakes:
  • Confusing event sourcing with snapshot storage
  • Thinking events are only for communication
  • Believing events are stored only on errors
2. Which of the following is the correct sequence in event sourcing when a user updates their profile?
easy
A. Update state directly, then create an event for the change
B. Send event to other services before updating state
C. Create an event representing the change, then update the state from the event
D. Store the new state without creating any event

Solution

  1. Step 1: Recall event sourcing flow

    In event sourcing, changes are first recorded as events, then state is updated from those events.
  2. Step 2: Match options to flow

    Create an event representing the change, then update the state from the event correctly shows event creation before state update; others do not follow this order.
  3. Final Answer:

    Create an event representing the change, then update the state from the event -> Option C
  4. Quick Check:

    Event first, then state update = Create an event representing the change, then update the state from the event [OK]
Hint: Events come before state updates in event sourcing [OK]
Common Mistakes:
  • Updating state before creating events
  • Skipping event creation
  • Sending events before state is updated
3. Given this simplified event sourcing code snippet, what will be the final user balance?
events = [
  {"type": "Deposit", "amount": 100},
  {"type": "Withdraw", "amount": 30},
  {"type": "Deposit", "amount": 50}
]

balance = 0
for event in events:
    if event["type"] == "Deposit":
        balance += event["amount"]
    elif event["type"] == "Withdraw":
        balance -= event["amount"]
print(balance)
medium
A. 80
B. 150
C. 100
D. 120

Solution

  1. Step 1: Calculate deposits and withdrawals

    Deposits: 100 + 50 = 150; Withdrawals: 30
  2. Step 2: Compute final balance

    Balance = 0 + 150 - 30 = 120
  3. Final Answer:

    120 -> Option D
  4. Quick Check:

    Deposits minus withdrawals = 120 [OK]
Hint: Add deposits, subtract withdrawals to find balance [OK]
Common Mistakes:
  • Adding withdrawals instead of subtracting
  • Ignoring last deposit event
  • Starting balance not zero
4. In an event sourcing system, which issue is caused by missing an event during state reconstruction?
medium
A. System crashes immediately
B. State becomes inconsistent or incorrect
C. Events are duplicated in the event store
D. Commands fail to generate new events

Solution

  1. Step 1: Understand event replay in event sourcing

    State is rebuilt by replaying all events in order.
  2. Step 2: Effect of missing an event

    If an event is missing, the rebuilt state will not reflect all changes, causing inconsistency.
  3. Final Answer:

    State becomes inconsistent or incorrect -> Option B
  4. Quick Check:

    Missing event = incorrect state [OK]
Hint: Missing events cause wrong state, not crashes [OK]
Common Mistakes:
  • Assuming system crashes on missing event
  • Confusing missing event with duplicate events
  • Thinking commands stop working due to missing event
5. You are designing a microservice using event sourcing. To improve performance, you want to avoid replaying all events every time. Which approach is best to achieve this?
hard
A. Use snapshots to save periodic state and replay only recent events
B. Store only the latest event and discard older ones
C. Send all events to other services for processing
D. Update state directly and ignore events after initial load

Solution

  1. Step 1: Identify performance issue in event sourcing

    Replaying all events from the start can be slow as event count grows.
  2. Step 2: Choose solution to reduce replay time

    Snapshots save the current state at intervals, so only recent events need replaying.
  3. Final Answer:

    Use snapshots to save periodic state and replay only recent events -> Option A
  4. Quick Check:

    Snapshots improve replay speed [OK]
Hint: Snapshots speed up state rebuild by reducing event replay [OK]
Common Mistakes:
  • Discarding old events breaks event sourcing
  • Sending all events elsewhere doesn't reduce replay
  • Ignoring events after initial load loses audit trail