CQRS and Event Sourcing
Separating write and read models, storing state as a sequence of events, projections and snapshots, and why the two patterns are often combined but should be adopted carefully.
CQRS: intent
Command Query Responsibility Segregation separates the model that changes state (commands) from models that read state (queries). Writes go to a model optimized for invariants; reads go to one or more denormalized read models optimized for queries.
Event sourcing: intent
Instead of storing current state, store the sequence of events that led to it. Current state is derived by replaying events (with snapshots for speed). The event store is the system of record.
flowchart LR C[Command: Withdraw 100] --> AGG[Account aggregate<br/>load events, validate] AGG -->|append MoneyWithdrawn| ES[(Event store)] ES --> PR1[Projection: balances table] ES --> PR2[Projection: statement search index] ES --> PR3[Projection: analytics] Q[Query: balance] --> PR1
Java example (framework-free sketch)
sealed interface AccountEvent permits AccountOpened, MoneyDeposited, MoneyWithdrawn {}
record AccountOpened(String id, String owner) implements AccountEvent {}
record MoneyDeposited(String id, long amount) implements AccountEvent {}
record MoneyWithdrawn(String id, long amount) implements AccountEvent {}
final class Account {
private long balance;
private int version;
static Account replay(List<AccountEvent> history) {
Account a = new Account();
history.forEach(a::apply);
return a;
}
List<AccountEvent> withdraw(String id, long amount) {
if (amount > balance) throw new InsufficientFunds();
return List.of(new MoneyWithdrawn(id, amount));
}
void apply(AccountEvent e) {
switch (e) {
case AccountOpened o -> balance = 0;
case MoneyDeposited d -> balance += d.amount();
case MoneyWithdrawn w -> balance -= w.amount();
}
version++;
}
}
// Append with optimistic concurrency: eventStore.append(streamId, expectedVersion, newEvents)Real-world use
Ledgers and payments (audit trail), order management, collaborative systems, regulated workflows. Frameworks: Axon Framework (Java), EventStoreDB/Kurrent, Marten (.NET), or Kafka + a relational event table.
Pros and cons
| Pros | Cons |
|---|---|
| Full audit history and time travel | Schema evolution of events is hard (upcasting) |
| Read models tailored to queries | Eventual consistency between write and read models |
| Easy to add new projections by replay | Replays and snapshots add operational work |
| Natural integration with event-driven systems | Steep learning curve; many teams over-apply it |
When not to use
Simple CRUD domains, teams new to DDD, or when audit needs can be met with an audit table and CDC.
Interview questions
Can you use CQRS without event sourcing?
Yes. Most CQRS implementations update read models from database CDC or domain events while the write model stores current state normally.
How do you handle a bug in a projection?
Fix the projection code and rebuild the read model by replaying the event stream into a new table/index, then switch reads over. That is a major advantage of event sourcing.
How do you evolve event schemas?
Version events, keep old versions readable, use upcasters to transform old events into the new shape on read, and never change the meaning of existing fields.