Chapter 24: Event Sourcing and CQRS

Event Sourcing

Instead of storing current state, store the sequence of events that led to current state.

// Traditional: store current state
account: {id: 1, balance: 850}

// Event sourcing: store all events
AccountCreated {id: 1, balance: 1000}
MoneyWithdrawn {id: 1, amount: 100}
MoneyDeposited {id: 1, amount: 50}
MoneyWithdrawn {id: 1, amount: 100}
// Current state = replay all events: 1000 - 100 + 50 - 100 = 850

Benefits

Drawbacks

CQRS (Command Query Responsibility Segregation)

Traditional: same model for reads and writes App ──read/write──► Single Database CQRS: separate models optimized for each Commands (writes) ──► Write Model (normalized, event store) │ events ▼ Event Bus (Kafka) │ ▼ Queries (reads) ◄── Read Model (denormalized, optimized for queries)

Use CQRS when: read and write patterns are very different (e.g., complex writes but simple reads, or vice versa). Don't use for simple CRUD apps.

Key Takeaways