Bird
Raised Fist0
Microservicessystem_design~7 mins

Event store concept in Microservices - System Design Guide

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
Problem Statement
When a system only stores the current state of data, it loses the history of how that state was reached. This makes debugging, auditing, and recovering past states difficult or impossible, especially in complex microservices where state changes happen frequently and asynchronously.
Solution
An event store records every change to the system as a sequence of events, preserving the full history. Instead of just saving the latest state, it saves all state transitions, allowing the system to reconstruct past states by replaying events. This approach supports audit trails, debugging, and temporal queries.
Architecture
Command/API
(Client)
Event Store
Read Models /
Read Models /

This diagram shows how commands from clients generate events stored in an append-only event store. Events are then published to processors that update read models or trigger side effects.

Trade-offs
✓ Pros
Preserves full history of all changes, enabling audit and debugging.
Supports rebuilding system state at any point in time by replaying events.
Decouples write and read models, improving scalability and flexibility.
Facilitates event-driven architectures and asynchronous processing.
✗ Cons
Increases storage requirements since all events are kept indefinitely.
Requires careful design for event versioning and schema evolution.
Adds complexity in rebuilding current state and handling eventual consistency.
Use when system requires audit trails, temporal queries, or complex state transitions in microservices with asynchronous workflows and high scalability needs.
Avoid when system is simple CRUD with low change volume or when event replay and history are not needed, as complexity and storage overhead outweigh benefits.
Real World Examples
Uber
Uber uses event sourcing to track ride state changes and payments, enabling reliable audit and recovery of ride histories.
LinkedIn
LinkedIn employs event stores to capture user activity streams, allowing reconstruction of user timelines and analytics.
Amazon
Amazon uses event sourcing in parts of its order processing system to maintain a complete history of order state changes for auditing and troubleshooting.
Code Example
The before code updates the account balance directly, losing history. The after code appends each change as an event to the event store and applies it, preserving full change history and enabling state reconstruction.
Microservices
### Before: naive state update without event store
class Account:
    def __init__(self):
        self.balance = 0

    def deposit(self, amount):
        self.balance += amount

    def withdraw(self, amount):
        self.balance -= amount


### After: event store records all changes as events
class EventStore:
    def __init__(self):
        self.events = []

    def append(self, event):
        self.events.append(event)

    def get_events(self):
        return self.events

class Account:
    def __init__(self, event_store):
        self.balance = 0
        self.event_store = event_store
        self.replay_events()

    def replay_events(self):
        for event in self.event_store.get_events():
            self.apply(event)

    def apply(self, event):
        if event['type'] == 'deposit':
            self.balance += event['amount']
        elif event['type'] == 'withdraw':
            self.balance -= event['amount']

    def deposit(self, amount):
        event = {'type': 'deposit', 'amount': amount}
        self.event_store.append(event)
        self.apply(event)

    def withdraw(self, amount):
        event = {'type': 'withdraw', 'amount': amount}
        self.event_store.append(event)
        self.apply(event)
OutputSuccess
Alternatives
State-based persistence
Stores only the current state snapshot instead of all changes as events.
Use when: Choose when system complexity is low and full history or temporal queries are not required.
Command Query Responsibility Segregation (CQRS)
Separates read and write models but does not necessarily store all events; can be combined with event store.
Use when: Choose when you want to optimize read and write workloads separately, possibly alongside event sourcing.
Summary
Event store records every change as an immutable event, preserving full history.
It enables rebuilding system state by replaying events and supports audit and debugging.
Event sourcing adds complexity and storage needs but is valuable for complex, scalable microservices.

Practice

(1/5)
1. What is the primary purpose of an event store in a microservices architecture?
easy
A. To save every change as an immutable event in order
B. To store user credentials securely
C. To cache frequently accessed data for faster reads
D. To manage service discovery and load balancing

Solution

  1. Step 1: Understand event store role

    An event store records all changes as events, preserving order and immutability.
  2. Step 2: Compare options with event store purpose

    Only To save every change as an immutable event in order describes saving changes as immutable events in order, which matches event store's main function.
  3. Final Answer:

    To save every change as an immutable event in order -> Option A
  4. Quick Check:

    Event store = immutable ordered events [OK]
Hint: Event store saves changes as events, not data or cache [OK]
Common Mistakes:
  • Confusing event store with caching layer
  • Thinking event store manages security or load balancing
  • Assuming event store modifies events after saving
2. Which of the following best describes the structure of data in an event store?
easy
A. A mutable key-value store with random access
B. An append-only log of immutable events
C. A relational database with tables and joins
D. A cache with time-to-live expiration

Solution

  1. Step 1: Identify event store data structure

    Event stores keep data as an append-only log where events cannot be changed once stored.
  2. Step 2: Match options to event store structure

    An append-only log of immutable events correctly describes an append-only log of immutable events, unlike mutable stores or caches.
  3. Final Answer:

    An append-only log of immutable events -> Option B
  4. Quick Check:

    Event store = append-only immutable log [OK]
Hint: Event store data is append-only and immutable, not mutable [OK]
Common Mistakes:
  • Thinking event store allows event updates
  • Confusing event store with relational databases
  • Assuming event store is a cache with expiration
3. Given the following sequence of events stored in an event store:
1: UserCreated {userId: 1, name: "Alice"}
2: UserNameUpdated {userId: 1, name: "Alicia"}
3: UserDeleted {userId: 1}

What is the current state of the user with userId=1 after replaying these events?
medium
A. User with name "Alice" and deleted flag true
B. User with name "Alicia" exists
C. User with name "Alice" exists
D. User does not exist

Solution

  1. Step 1: Replay events in order

    First event creates user Alice, second updates name to Alicia, third deletes the user.
  2. Step 2: Determine final user state

    After deletion event, user no longer exists regardless of previous name changes.
  3. Final Answer:

    User does not exist -> Option D
  4. Quick Check:

    Last event is deletion, so user is gone [OK]
Hint: Last event determines existence; deletion means no user [OK]
Common Mistakes:
  • Ignoring the delete event
  • Assuming user name remains after deletion
  • Confusing event replay order
4. You notice that your event store is allowing events to be updated after they are stored. What is the main issue with this behavior?
medium
A. It enables faster event replay by skipping old events
B. It improves performance by reducing storage needs
C. It breaks the immutability principle, causing inconsistent system state
D. It allows easier debugging by fixing event data

Solution

  1. Step 1: Understand immutability in event stores

    Events must be immutable to ensure reliable replay and audit trails.
  2. Step 2: Analyze impact of updating events

    Updating events breaks immutability, leading to inconsistent or incorrect system state.
  3. Final Answer:

    It breaks the immutability principle, causing inconsistent system state -> Option C
  4. Quick Check:

    Event immutability = consistent state [OK]
Hint: Events must never change after storing [OK]
Common Mistakes:
  • Thinking event updates improve debugging
  • Assuming updates improve performance
  • Believing updates speed up replay
5. In a microservices system using an event store, how can you efficiently rebuild the current state of a service that has millions of events without replaying all events every time?
hard
A. Use snapshots to save intermediate states periodically
B. Delete old events after a certain time to reduce replay
C. Store only the latest event per entity to minimize data
D. Replay events in parallel without ordering

Solution

  1. Step 1: Identify replay challenges with many events

    Replaying millions of events is slow and inefficient for rebuilding state.
  2. Step 2: Evaluate solutions to speed up rebuilding

    Snapshots save the state at points in time, allowing replay from snapshot forward, reducing events to process.
  3. Final Answer:

    Use snapshots to save intermediate states periodically -> Option A
  4. Quick Check:

    Snapshots optimize replay by reducing event count [OK]
Hint: Snapshots speed up state rebuild, don't delete events [OK]
Common Mistakes:
  • Deleting old events breaks audit and consistency
  • Storing only latest event loses history
  • Replaying events out of order causes errors