Event Sourcing vs CQRS
Event sourcing and CQRS are often used together, so people mix them up, but they solve two different problems. Event sourcing is about how data is stored. Every change is kept as an event. CQRS is about how code is split. One side changes data, and another side reads it. Either one works fine without the other.
Event Sourcing
Save each change as an event. Build the present state from them.
Event sourcing stores every change to data as an event, not just the latest value. A shopping cart is saved as item added, item added, item removed, not as its final list. The current state is worked out by replaying the events in order. Nothing is ever written over. So you have the full history. The cost is more storage and more complex code.
Command Query Responsibility Segregation (CQRS)
Splitting the code that changes data from the code that reads it.
CQRS splits the code that changes data from the code that reads it. Commands change things, like place order. Queries only read, like show my orders. Each side can have its own model, and often its own database, shaped for its job. Reads can then be fast and simple even when writes have many rules.
In one line
- Event Sourcing
- Save each change as an event. Build the present state from them.
- Command Query Responsibility Segregation (CQRS)
- Splitting the code that changes data from the code that reads it.