Day 29: Event Sourcing Fundamentals
Day 29: Event Sourcing Fundamentals
Welcome back, future architects of financial truth! In our last session, we delved into Write-Ahead Logging (WAL) – a powerful technique that ensures data durability and consistency. WAL is foundational, but as systems evolve to handle complex, high-volume financial transactions, we need a paradigm that offers more than just durability. We need history. We need intent. We need to reconstruct "what happened" at any point in time, not just "what is now."
Today, we're unlocking a core pattern in ultra-high-scale, resilient systems: Event Sourcing. This isn't just a database choice; it's a fundamental shift in how you perceive and manage your system's state, especially critical for banking and ledger systems where every single change is a moment of truth.
The Engineering of Financial Truth: Beyond WAL
Think about a traditional database. When you update an account balance from $100 to $150, the old value ($100) is typically overwritten. WAL ensures this update is durable, but the fact that $50 was added is implicitly lost in the "current state." If you want to know why it changed, you need separate audit tables or complex logging.
This approach becomes a bottleneck when you need:
Auditing: A complete, immutable history of every change. Regulators demand it.
Temporal Queries: "What was the account balance last Tuesday at 3 PM?"
Debugging: Replaying sequences of operations to understand anomalies.
Business Intelligence: Analyzing patterns of user behavior over time.
System Evolution: Easily introducing new read models without data migration.
Event Sourcing addresses these challenges head-on by making the sequence of events the primary source of truth.
Core Concepts: The Building Blocks of Immutability
At its heart, Event Sourcing is beautifully simple, yet profoundly powerful.
1. Events: The Immutable Facts of Life
An Event is a record of something that has already happened in your system. It's a statement of fact, expressed in the past tense (e.g., AccountCreditedEvent, FundsTransferredEvent). Events are immutable – once recorded, they can never be changed or deleted. Think of them as entries in a ledger, each one a definitive, unalterable transaction.
Key Insight: Events capture intent. Instead of update account set balance = 150, an event says AccountCreditedEvent { accountId: "123", amount: 50, timestamp: ... }. This semantic richness is invaluable for understanding your system's behavior and for business analytics.
2. Aggregates: The Consistency Guardians
An Aggregate is a cluster of domain objects that are treated as a single unit for data changes. It's the consistency boundary. In our banking context, an Account is a perfect aggregate. All commands (like deposit, withdraw) target an aggregate, and all events originate from it. An aggregate's current state is derived by replaying all its past events.
Why Aggregates? They enforce business rules and invariants. For example, an Account aggregate ensures that you can't withdraw more money than you have (unless it's an overdraft account, itself a business rule).
3. Event Store: The Immutable Ledger
The Event Store is a specialized database that stores events in a sequential, append-only manner. It's the central repository for all your system's historical facts. Unlike traditional databases, you don't update records; you only append new events.
Production Insight: For ultra-high scale, an Event Store is often a distributed, highly available log-structured database (think Kafka, Cassandra, or specialized event stores like EventStoreDB). It's designed for high write throughput and sequential reads.
4. Projections (Read Models): The Views You Need
Keep going with the full lesson
The rest of this lesson — plus its implementation guide, runnable scripts and source code — is included with the course or an All Access plan. Create a free account to start the free lessons in every course.
Already a member? Sign in