Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Event Sourcing: Principles and Distinctions from State-Oriented Persistence

Hello! Welcome to the first lesson in our module on State Management.

Over the past few modules, we've explored the foundational challenges and communication patterns in distributed systems. Now, we're diving into one of the most powerful—and conceptually distinct—patterns for managing data and state: Event Sourcing.

Today's lesson addresses the learning outcome: "Explain the core principles of event sourcing and how it differs from traditional state-oriented persistence." We will deconstruct this pattern, contrasting it with the familiar CRUD (Create, Read, Update, Delete) model. Given your background in building financial systems like clearing and FX trading platforms, the core ideas of immutability and auditability should resonate strongly.

1. The Fundamental Shift: From State to History

In most applications, we persist the current state of our data. If a user changes their address, we execute an UPDATE statement, and the old address is gone, overwritten by the new one. The database acts as a snapshot of the world right now.

Event Sourcing proposes a radical shift. Instead of storing the current state, we store the complete, ordered sequence of events that have affected the data over time. The source of truth is no longer the current state but the immutable log of everything that has happened.

The most direct analogy comes from your domain: an accounting ledger. An accountant doesn't erase a previous balance; they append new transactions (debits and credits). The current balance is derived by summing all transactions. The ledger itself—the history of events—is the source of truth.

This image contrasts a traditional database update with the Event Sourcing approach. The traditional model on the left modifies a single balance record. The Event Sourcing model on the right appends immutable `DepositEvent` and `WithdrawalEvent` records, from which the balance can be calculated.

To begin, let's watch a short introduction by Martin Fowler, who formalized the pattern. He explains this fundamental distinction clearly.

Event Sourcing • Martin Fowler • YOW! 2016

In this clip from his talk 'Event Sourcing', Martin Fowler introduces the core concept by contrasting it with traditional state persistence and drawing the analogy to version control systems.

Please watch the first two sections (timestamps 00:13 - 02:49 and 03:15 - 04:21). Focus on: How an event-sourced system handles a change request differently. The two parts of the world view: application state and the event log. The critical role of the event log as the 'fundamental source of truth'. The powerful analogy he makes to version control systems like Git.

As Fowler points out, if you've used Git, you've used a system that works this way. Your working directory is the "application state," but the true source of truth is the history of commits in the .git directory. You can always rebuild your current state (or any past state) from that history.

2. Core Mechanics and Components

To make this abstract idea concrete, let's break down the machinery of an event-sourced system. The following reading provides a clear, structured overview of the key components.

Event Sourcing Explained: The Pros, Cons & Strategic Use ...

This article, 'Event Sourcing Explained', provides a detailed look at the components that make up an event-sourced system.

Please read the section titled 'Core Mechanics and Components: The Machinery of Event Sourcing'. It begins after the introduction and ends just before the section on benefits and challenges. Focus on understanding the role of each of these four components: The Event Store: The append-only database for events. Events as First-Class Citizens: How events are designed to capture business intent. Aggregates and State Reconstruction (Rehydration): The process of loading an entity's state by replaying its events. Commands vs. Events: The crucial distinction between a request to do something and the record of something that has happened.

To summarize the key workflow from that reading:

  1. A Command is issued to the system (e.g., SettlePaymentCommand).
  2. The system loads the relevant entity (often called an Aggregate, like a Payment object) by rehydrating it from its stream of past events in the Event Store.
  3. The loaded Aggregate validates the command against its current state and business rules.
  4. If the command is valid, the Aggregate produces one or more new Events (e.g., PaymentSettled).
  5. These new events are atomically appended to the event stream in the Event Store. The Aggregate then updates its own in-memory state by applying the new event to itself.

The distinction between a command and an event is critical from an epistemological standpoint, which might appeal to your interests. A command is an intent and can be rejected. An event is an immutable fact—it represents something that has already successfully occurred.

3. Event Sourcing vs. CRUD: A Direct Comparison

The difference between Event Sourcing and the traditional CRUD model is not just an implementation detail; it's a fundamental dichotomy in the philosophy of data management.

This image provides a simple analogy: CRUD is like having a single, modifiable page in a binder, where history is overwritten. Event Sourcing is like an append-only ledger, where the full history is preserved.

The CRUD model is concerned with storing the present. Event Sourcing is concerned with preserving the past.

Feature Traditional State-Oriented (CRUD) Event Sourcing
Source of Truth The current state of data in tables/documents. The chronological, immutable log of events.
Write Operation Destructive (UPDATE, DELETE modify/erase data). Append-only (Events are added, never changed).
Data Fidelity Lossy. Historical context is overwritten and lost. Lossless. The complete history is preserved.
Audit Trail Must be explicitly built as a separate feature. Intrinsic. The event log is a perfect audit trail.
Querying State Direct and simple (e.g., SELECT * FROM users). Indirect. Requires replaying events or querying a projection.
Temporal Queries Difficult without custom versioning schemes. Natural. Reconstruct state at any point in time.

This table, adapted from the "Event Sourcing Explained" article, provides a concise summary of the trade-offs.

For a quick video walkthrough of this comparison with practical examples, the following clip is helpful.

Event Sourcing VS CRUD | System Design

This video from System Design School offers a direct comparison between Event Sourcing and CRUD, using banking and inventory management as examples.

Watch the sections covering the introductions to both patterns and their corresponding examples (timestamps 00:00 - 01:58 and 02:56 - 04:23). Notice how the banking example naturally fits Event Sourcing, while the simple inventory example illustrates the directness of CRUD.

4. The Architectural Trade-Off: Why (and Why Not) Choose Event Sourcing?

As an architect, the most important question is not "what is it?" but "when should I use it?". Event Sourcing offers profound benefits but comes at the cost of significant complexity. It is not a silver bullet.

Key Benefits

Your experience at Revolut and the hedge fund likely involved systems where these benefits are not just nice-to-haves, but critical requirements.

  • Complete Audit Trail: The event log provides a perfect, tamper-evident history of every meaningful change. This is invaluable for compliance, security, and financial reconciliation.
  • Powerful Debugging and Analytics: You can replay events to reproduce bugs, analyze historical user behavior, or answer complex business questions like, "How many times did this user's FX trade order get repriced before execution?"
  • Temporal Queries: The ability to reconstruct the state of any entity at any point in time is built-in.
  • Enabler for Advanced Patterns: Event Sourcing is a natural foundation for patterns like Command Query Responsibility Segregation (CQRS) and for building resilient, decoupled distributed systems, which we will explore in depth later.

Key Challenges

  • Conceptual Complexity: It's an unfamiliar model for many developers. The learning curve involves not just ES, but often Domain-Driven Design (DDD) and eventual consistency.
  • Querying for State: While writing is simple (append-only), reading the current state requires rehydration, which can be slow for entities with long histories. This often necessitates building and maintaining separate "read models" (a core concept in CQRS).
  • Event Schema Evolution: Events are immutable, but business requirements change. What happens when you need to add a field to an OrderPlaced event? You can't change the old events, so you must handle multiple versions of the same event in your code, a process called "upcasting."
  • External System Integration: If your logic depends on an external API call (e.g., fetching a live FX rate), you cannot simply "replay" that. The result would be different. This requires careful design to ensure that all necessary information from external systems is captured within your events.

Martin Fowler and the author of "The Promised Land of Event Sourcing" both provide excellent, balanced discussions on these trade-offs.

The Promised Land of Event Sourcing

This article, 'The Promised Land of Event Sourcing', gives a pragmatic overview of the pros and cons from a developer's perspective.

Please read the sections 'Why Would One Use Event Sourcing?' and 'Event Sourcing Is Not All Fun And Games'. This will reinforce the benefits and provide a clear-eyed view of the associated complexities.

Conclusion

In this lesson, we've established the core principles of Event Sourcing as a data persistence pattern that prioritizes history over the present moment.

Key Takeaways:

  • Core Principle: The source of truth is an immutable, append-only log of domain events.
  • State Management: The current state of an entity is not stored directly but is reconstructed on-demand by replaying its event history.
  • CRUD vs. ES: Traditional CRUD models use destructive writes (UPDATE, DELETE) and store a snapshot of the current state, losing historical context. Event Sourcing uses constructive, append-only writes, preserving the full narrative.
  • The Trade-Off: Event Sourcing provides powerful benefits like complete auditability, temporal queries, and enhanced debugging capabilities. This power comes at the cost of increased conceptual and operational complexity, particularly around querying, event versioning, and integration.

Your background in finance gives you a head start in appreciating why this trade-off is often worth it in high-stakes domains. The need for a perfect, auditable transaction log is the very problem Event Sourcing is designed to solve.

Preview of the Next Lesson:
Now that we have the theory, our next step is to apply it. In the next lesson, we will begin designing the components for an event-sourced system, starting with the most important part: the events themselves. The learning outcome will be: "Design aggregate roots and domain events for a financial scenario from your experience (e.g., FX trade lifecycle)." We will take a familiar workflow and model it as a stream of immutable facts.

Can't find a good explanation? Sign up and we'll make it for you

Sign up