Use one model to write data and a different one to read it.
CQRS (Command Query Responsibility Segregation) means using one model to change data and a different model to read data.
Most apps use one model for everything. The same Order object and the same tables serve the checkout screen (writes with strict rules) and the order history screen (reads that join many tables). These two jobs pull in different directions.
CQRS grows from Bertrand Meyer's old rule CQS (Command Query Separation: a method either changes state or returns data, not both). Greg Young applied it at the level of the whole model and called it CQRS.
| Level | What is separate | Consistency | Effort |
|---|---|---|---|
| 1. Code only | Command handlers and query handlers, same database | Immediate | Low |
| 2. Different models, same database | Write tables and read views or materialized views in one database | Immediate or near immediate | Medium |
| 3. Separate read store | Read model in another database, synced by events or change data capture | Eventual | High |
| 4. With Event Sourcing | Write side stores events. Read side is built by projections | Eventual | Highest |
| Command | Query | |
|---|---|---|
| Purpose | Change state | Read state |
| Name style | Verb in the imperative: PlaceOrder, CancelBooking | Question: GetOrderHistory, ListOpenTickets |
| Returns | Success, failure, maybe an id | Data shaped for the screen |
| Side effects | Yes, rules and events | None. Safe to repeat and cache |
| Can fail because of | Rule broken, conflict | Not found, no permission |
Simplified model: a write costs 5 units, a read 1 (x3 with joins in a single model), and one database handles 1000 units per second. Real numbers differ.
| Term | Meaning in easy words |
|---|---|
| Command | A request to change something, with a clear business meaning |
| Query | A request to read data. It changes nothing |
| Write model | The model with rules that decides what changes are allowed |
| Read model | A model or view shaped for fast display and search |
| Projection | Code that builds or updates a read model from changes |
| Eventual consistency | The read side catches up after a short delay |
| CQS | Command Query Separation: a method either changes or returns, not both |
| Command handler | Code that runs one command and applies the rules |
| Change data capture (CDC) | Reading the database log to find out what changed |
| Idempotent projection | Applying the same event twice gives the same result |
| Rebuild | Create the read model again by replaying history |
| Choice | We gain | We lose |
|---|---|---|
| Separate read database | Fast, scalable reads in the right shape | Delay, extra storage and a sync pipeline |
| Same database, separate code | Simple and still consistent | Reads and writes still share hardware |
| Synchronous projection | Read is up to date immediately | Slower writes, tighter coupling |
| Asynchronous projection | Fast writes, loose coupling | Reads can be stale for a moment |
| Many specialised views | Each screen is very fast | More projections to build and keep right |
| Few shared views | Less to maintain | Some screens still need extra work |