Course contentsShow
← Software Engineering
Lesson 1347 of 1,93340. Event-Driven ArchitectureFree lesson

Event Sourcing Fundamentals

Store state as a sequence of immutable events rather than current values, enabling complete audit trails and time travel.

Event Sourcing Fundamentals

What you'll learn: How to store your application's state as an immutable sequence of events instead of overwriting current values.

The Core Idea

Traditional systems store the current state of entities—like "User balance: 100."Whenthebalancechanges,youoverwritethatvalue.EventSourcingflipsthis:insteadofstoringcurrentstate,youstore∗everyeventthatchangedthestate∗:"AccountCredited100." When the balance changes, you overwrite that value. Event Sourcing flips this: instead of storing current state, you store *every event that changed the state*: "AccountCredited 50," "AccountDebited 20,""AccountCredited20," "AccountCredited 70." The current state is derived by replaying all events from the beginning.

Think of it like a bank statement versus your account balance. The statement lists every transaction (events), while your balance is just the sum. Event Sourcing keeps the full statement and calculates the balance when needed.

Why This Matters in Event-Driven Architecture

Event Sourcing naturally extends event-driven architecture. Instead of just publishing events to notify other systems, you also persist them as your source of truth. This gives you:

Complete audit trail: Every state change is recorded with context—who did what, when, and why.

Time travel: Replay events to see what your system looked like at any point in the past.

Debugging superpowers: Reproduce bugs by replaying the exact sequence of events that caused them.

Flexibility: Project events into different read models optimized for various queries.

Trade-offs to Consider

Event Sourcing isn't free. You must handle event schema evolution carefully, as old events never disappear. Performance requires careful design—replaying thousands of events for every read is expensive (solved with snapshots and CQRS patterns you'll learn later). The mental shift from CRUD to event-thinking can be challenging for teams.

Key Takeaway: Event Sourcing stores state as an append-only log of immutable events rather than mutable current values, enabling complete history tracking and temporal queries at the cost of increased complexity.