Patterns
35 items
35 items
Storing state as a sequence of events
Event Sourcing stores application state as a sequence of immutable events rather than as current state in a database. Instead of updating records in place, every state change is appended as an event to an event store. The current state is derived by replaying events from the beginning. This provides a complete audit trail, enables time travel, and supports event-driven architectures naturally.
In event sourcing, the database stores every event that ever happened. Current state is derived by replaying these events. This means you have a complete history of how the system reached its current state.
Events are immutable facts about the past—once written, they never change. The event store is append-only. Instead of fixing data by updating records, you append compensating events.
Since all historical events are preserved, you can reconstruct any past state by replaying events up to a point in time. This enables debugging production issues by replaying past events.
Unlike database migrations, you cannot change past events. This requires careful versioning: include a version field in every event, write code that handles multiple versions.
Replaying thousands of events to reconstruct state is too slow. Snapshots periodically capture state, allowing replay to start from the snapshot rather than the beginning.
Event sourcing and CQRS are often used together but are distinct patterns. Event sourcing is about how you store data. CQRS is about separating read and write models.
Traditional CRUD systems store only current state. When you update a record, the previous value is lost forever. This makes it impossible to answer questions about past states or maintain audit trails.
| Aspect | Advantage | Disadvantage |
|---|---|---|