Event Sourcing
Store all changes as events. State = replay of events. Enables audit trail, time travel, replay.
Event-Driven Architecture (EDA) is an architectural pattern where systems communicate through events - immutable records of something that happened in the past. Unlike request-response patterns where components directly call each other, event-driven systems use an event bus or message broker to decouple producers and consumers.
Traditional architectures use synchronous request-response patterns: Service A calls Service B and waits for a response. Event-driven architectures flip this: Service A publishes an event (“OrderCreated”) and continues. Service B (and potentially C, D, E) subscribe to that event and react asynchronously.
Key Characteristics:
Event sourcing is a pattern where you store all changes to application state as a sequence of events, rather than storing the current state. The current state is derived by replaying all events from the beginning (or from a snapshot).
Instead of updating a record in a database (overwriting the previous state), you append a new event to an event log. To get the current state, you replay all events. To see the state at any point in time, you replay events up to that point.
Why This Matters:
Traditional databases store the current state - you can see what something is now, but not how it got there. Event sourcing stores the history - you can see every change that ever happened, when it happened, and why it happened (if events include context).
This enables powerful capabilities: audit trails (see who changed what and when), time travel (see state at any historical point), replay (rebuild state from scratch), and debugging (replay events to understand what happened).
Problem: Lost history! Can’t see how state changed.
Benefits:
CQRS (Command Query Responsibility Segregation) is a pattern that separates the read and write models of your application. Instead of using the same data model for both reading and writing, you have separate models optimized for each operation.
In traditional applications, you use the same database schema for both:
CQRS recognizes that these operations have different requirements:
The Separation:
The read model is typically updated asynchronously by consuming events from the write model, ensuring eventual consistency.
Benefits:
Rebuild state by replaying events.
Problem: Replaying millions of events is slow!
Solution: Snapshots!
Strategy:
Event Sourcing
Store all changes as events. State = replay of events. Enables audit trail, time travel, replay.
CQRS
Separate read and write models. Optimize independently. Scale independently. Better performance.
Event Replay
Replay events to rebuild state. Use snapshots for performance. Enables debugging, testing, time travel.
Events are Facts
Events represent something that happened. Immutable. Past tense. Full history of changes.