From "Designing Data-Intensive Applications"
🎧 Listen to Summary
Free 10-min PreviewEvent Sourcing and the Nature of State
Key Insight
Event sourcing moves beyond low-level database change capture by building application logic directly upon immutable events, which are written to an append-only log. These events represent application-level occurrences, such as 'student cancelled their course enrollment,' capturing the user's intent more meaningfully than raw database mutations. This design choice discourages or prohibits direct updates and deletes, making it easier to evolve applications, debug issues by understanding the full history, and guard against application bugs by preserving every action as an immutable fact.
While the event log stores the history of actions, users typically expect to see the current state of a system (e.g., shopping cart contents). Therefore, event-sourced applications must transform the log of events into read-optimized application state. This transformation process must be deterministic, allowing the current state to be reliably reconstructed by replaying the entire event log. Unlike Change Data Capture (CDC), where log compaction can discard older versions of records because events often contain the full new record state, event sourcing often requires the full history of high-level events to reconstruct the final state, meaning log compaction as a complete state snapshot is not always directly applicable in the same way, though performance optimizations like snapshots are used.
A critical distinction in event sourcing is between 'commands' and 'events.' A user request is initially a 'command' that might fail validation (e.g., username taken). If validated and accepted, it becomes an 'event,' which is a durable, immutable fact. This validation must happen synchronously before an event is generated, often within a serializable transaction. Once an event is part of the log, consumers are not allowed to reject it, reinforcing its immutability. This philosophy underpins the idea that mutable application state is merely a 'cache' or an 'integration' of the immutable event stream over time, making the changelog the ultimate 'truth' and enhancing auditability, data retention for analytics, and the ability to derive multiple read-optimized views from the same source of truth, aligning with Command Query Responsibility Segregation (CQRS).
📚 Continue Your Learning Journey — No Payment Required
Access the complete Designing Data-Intensive Applications summary with audio narration, key takeaways, and actionable insights from Martin Kleppmann.