Quick revision
The short answer
CQRS, Command Query Responsibility Segregation, means handling commands that change state separately from queries that read it, often with a different model and even a different store for each. It is worth it when reads and writes have very different shapes or scale, such as a search page built from many aggregates, but read models updated from events are eventually consistent and add code, infrastructure and rebuild work, so for plain CRUD services it costs more than it gives.
What to remember
In this answer, we saw that CQRS separates commands, which change state through rules on the write model, from queries, which read from models shaped for screens. It pays off when reads and writes differ strongly in shape or scale, such as search pages built from several aggregates, but read models updated from events are eventually consistent and bring more code, more infrastructure and the work of rebuilding projections. For simple CRUD services, one model is cheaper and better.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
CQRS, Command Query Responsibility Segregation, means handling commands that change state separately from queries that read it, often with a different model and even a different store for each. It is worth it when reads and writes have very different shapes or scale, such as a search page built from many aggregates, but read models updated from events are eventually consistent and add code, infrastructure and rebuild work, so for plain CRUD services it costs more than it gives.
Why this question is asked
Most Spring services use one JPA model for everything: the same entity is loaded to change an order and to show a list of orders. That works until the screens we need look nothing like the entities, or reads outnumber writes a thousand to one. CQRS is the name for splitting the two sides, and interviewers ask about it to see whether we know both what it solves and what it costs. In this answer, we will recap the idea, show a small Spring example with separate command and query sides, look at when the split pays off, and go through the problems it introduces.
1A Quick Recap of CQRS
A command is a request to change state: PlaceOrder, CancelOrder, ChangeAddress. It goes through business rules and either succeeds or is rejected. A query is a request to read state: show my orders, search products by name. It changes nothing.
CQRS says these two kinds of requests can use different models. The write model is shaped around rules and consistency, usually aggregates, clusters of objects that are changed together in one transaction, such as an Order with its lines. The read model is shaped around what screens need, often flat, denormalised rows or documents that can be returned without joins.
The split can be light, two sets of classes over one database, or heavy, a write database plus one or more read stores such as Elasticsearch or a separate PostgreSQL schema, updated from events.
2What It Looks Like in a Spring Service
Here is the heavier form. The command side saves the aggregate and publishes an event through an outbox. A projection, a consumer that turns events into a read model, updates a summary table that the query side reads directly.
- REST controllersPOST goes to commands, GET goes to queries
- Command handlersload aggregate, apply rules, save
- Write store + outboxnormalised tables, one transaction
- Event stream
OrderPlaced,OrderShippedon Kafka - Projectionsconsume events, update read tables
- Read storedenormalised rows or a search index
6 layers, from the top down. Step through them one at a time.
Commands flow down the left of this stack and queries read only the bottom layer.
Notice that the query side never loads an Order entity. It reads exactly the columns the screen shows. And notice that the projection is idempotent, so a redelivered event does no harm.
3When the Split Pays Off
CQRS earns its cost when reads and writes differ sharply. The first case is shape: a product search page that shows name, price, stock, rating and seller in one row needs data from four aggregates, and building it with joins on every request is slow and complex. A read model that already holds that row is simple and fast.
The second case is scale. If a catalogue gets fifty thousand reads per second and a hundred writes, we can scale the read store and its replicas independently and keep the write database small and strict. The third case is several different views of the same data, a search index, a reporting table and a customer timeline, each optimised for its own queries and all fed by the same events.
4The Problems It Introduces
The biggest one is eventual consistency. The read model is updated after the command commits, a few milliseconds or seconds later. A user who places an order and immediately opens 'my orders' may not see it yet. We have to design for that in the UI and decide which views may lag.
The second is more code and infrastructure: events, projections, a message broker, a second store, and monitoring of how far each projection lags. The third is rebuilding. When we change a read model, add a column, fix a projection bug, we must replay events or reload from the write side to rebuild it, which needs the events to be kept and a tested rebuild procedure. And debugging now spans two models: a wrong number on the screen might be a bug in the command, the event or the projection.
| Concern | One model (CRUD) | CQRS with read models |
|---|---|---|
| Read after write | always fresh | may lag by seconds |
| Complex read screens | joins at request time | precomputed rows |
| Scaling reads | replicas of the same schema | separate store per view |
| Moving parts | service + database | broker, projections, extra stores |
| Changing a view | change a query | change projection and rebuild |
5When Not to Use It
For a simple CRUD service, an admin screen for reference data, a settings service, a small internal tool, the read and write shapes are the same, and CQRS adds a broker, projections and lag without solving any problem. Even when a few queries are heavy, we can often fix them with a database view, a well-chosen index, or a query that returns a DTO instead of entities, which is CQRS in its lightest form without any events.
A good rule is to start with one model and introduce a separate read model for the specific view that hurts, not for the whole service. CQRS is a pattern for parts of a system, not a style to apply everywhere.
6Follow-up questions
Is CQRS the same as event sourcing?
No. Event sourcing stores state as a sequence of events; CQRS separates reads from writes. They are often used together, because an event-sourced write side has no tables to query, so it needs read models. But we can use CQRS with an ordinary relational write model and an outbox, as in our example, and that is the more common combination.
Can CQRS use a single database?
Yes. The lightest form is separate command and query classes over one database, where queries return DTOs or read from views. There is no lag at all, and we still get read code that is shaped for screens. We move to a separate store only when shape or scale requires it.
How do you rebuild a read model?
We create the new table or index alongside the old one, replay all events from the start (or reload from the write store), let it catch up with live events, and then switch queries over. That needs the event history to be retained, for example a compacted or long-retention topic or an event store, and it should be rehearsed before it is needed.
7Conclusion
In this answer, we saw that CQRS separates commands, which change state through rules on the write model, from queries, which read from models shaped for screens. It pays off when reads and writes differ strongly in shape or scale, such as search pages built from several aggregates, but read models updated from events are eventually consistent and bring more code, more infrastructure and the work of rebuilding projections. For simple CRUD services, one model is cheaper and better.
Open with
CQRS means we separate the handling of commands, which change state, from queries, which only read it. On the command side I keep an aggregate model with business rules and transactions.
Then, if they want more
On the query side I keep read models shaped for the screens, often flat tables or a search index, so a query is one simple read without joins. In the heavier form the write side publishes events through an outbox and projections update the read models.
I would use it when reads and writes really differ, for example a search page combining products, prices, stock and ratings from several aggregates, or a catalogue where reads outnumber writes by orders of magnitude and need to scale separately. The cost is real.
Read models are eventually consistent, so a user may not see their own change for a moment and the UI has to handle that. There is more code and infrastructure: a broker, projections, extra stores, lag monitoring. And whenever a read model changes we must rebuild it by replaying events.
For a simple CRUD service I would not use it; at most I would have query methods that return DTOs, and I would introduce a separate read model only for the specific view that needs it.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation, Spring for Apache Kafka reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is CQRS, and when is it worth separating the read model from the write model in a...": https://scrivio-interview-library.netlify.app/spring-boot/event-driven-patterns/a085082d4a/