CQRS can describe anything from separate command and query handlers in one application to an architecture with different databases and asynchronous message processing.
These implementations have different costs and benefits. To evaluate the pattern, we need to be clear about what we are separating and why.
Throughout this article, we will use a simple example: an online store where a customer places an order and then views its details.
What Does CQRS Solve?
CQRS stands for Command Query Responsibility Segregation. Its central idea is to separate the responsibility for changing information from the responsibility for reading it, using models suited to each purpose.
In our store:
- A command requests a change, such as placing or cancelling an order.
- A query requests information, such as retrieving order details.
Placing an order must enforce business rules. Displaying an order must provide the information a page needs. When one shared model becomes difficult to use for both purposes, separating the models can help. Martin Fowler’s explanation of CQRS
For example, placing an order requires checking the requested quantity, available inventory, and authoritative price. The order details page only needs the product name, quantity, total, and status.
That page does not necessarily need the complete domain model and its behavior.
Separating Commands and Queries in the Application Layer
We can separate read and write paths while keeping a single database. Separate models with a shared data store are a valid CQRS implementation. Microsoft’s CQRS documentation
Our request and response contracts might look like this:
public record PlaceOrderCommand(
Guid ProductId,
int Quantity);
public record GetOrderDetailsQuery(
Guid OrderId);
public record OrderDetailsDto(
Guid Id,
string ProductName,
int Quantity,
decimal TotalPrice,
string Status);
These declarations show the contracts, not a complete implementation.
The command handler must check authorization, validate the quantity, obtain the authoritative price, enforce inventory rules, and persist the changes.
The query handler must check whether the user can view the order and return the information required by the page.
Command names should communicate intent. CancelOrder, for example, gives cancellation rules a clear place to run. A general UpdateOrder request tells us less about the behavior the user wants. Greg Young’s discussion of commands
What Do We Gain?
In this example, changing the order details page can be handled in the query path. Adding a rule for placing orders has a clear home in the command path.
We can also retrieve only the fields required by a query. In EF Core, projecting with Select can avoid fetching unnecessary columns. This optimization does not require a second database. EF Core documentation on efficient querying
What Does It Cost?
We introduce more contracts, handlers, and processing paths to maintain.
For a simple operation that stores and displays a few fields, this structure may require more work than a small application service.
Creating Commands and Queries folders does not, by itself, demonstrate useful separation between models. Separating code also does not automatically make queries faster or increase database capacity.
CQRS is useful in some contexts, but a shared model can be easier to maintain in simpler domains. Fowler’s discussion of CQRS trade-offs
A command may read data to enforce business rules. Its handler may also return an order ID or an operation result. The purpose of the separation is to give reads and state changes clear responsibilities.
What Happens to Consistency with One Database?
Separating code does not create a new synchronization step.
If a command commits changes to the primary database and a query reads the same data, the result depends on the database’s transaction and isolation rules.
For example, PostgreSQL’s Read Committed isolation gives each statement a snapshot taken when that statement begins. Under Repeatable Read, a transaction can continue using an older snapshot. PostgreSQL documentation on transaction isolation
A shared database therefore does not mean that every read always sees the newest state. However, separate handlers do not inherently introduce eventual consistency.
If we maintain a separate read table asynchronously—even inside the same database—that table can lag behind. The update mechanism matters.
Why Use a Separate Read Database?
Suppose the order listing receives substantial traffic. Each request joins several tables and calculates totals.
We could maintain a read representation containing the information needed for the page:
- Order ID
- Customer name
- Total amount
- Order status
This derived representation is a projection. Preparing it ahead of time can reduce repeated work during reads, but it needs an update mechanism. Microsoft’s Materialized View pattern
Putting it in a separate database allows us to choose a storage structure and capacity suited to those queries. It also introduces another component to synchronize, monitor, and operate. Separate data stores in CQRS
A read replica and a dedicated read model are different choices. A replica generally follows the primary database’s structure. A dedicated read model can have a different structure designed for specific queries.
How Does Asynchronous Synchronization Affect Consistency?
An event-driven implementation might follow these steps:
- The customer requests an order.
- The application stores the order in the write database.
- An
OrderPlacedevent is published. - A consumer updates the read model.
- Queries against that model can see the order.
There is a gap between storing the order and updating the read model. During that gap, the order exists, but the order listing may not show it.
This is an example of eventual consistency in an asynchronously updated read model. An asynchronously replicated database can lag behind its primary as well. PostgreSQL documentation on replication
Separate databases alone do not determine the consistency guarantees. The synchronization protocol and the conditions for acknowledging an operation matter too.
Eventual consistency also requires a functioning recovery path. The system must preserve changes and support retries or catch-up when processing fails.
What If Saving Succeeds but Publishing the Event Fails?
Saving an order and publishing a message are two separate operations.
If saving succeeds but publishing fails, the read model may never learn about the order. The Transactional Outbox pattern addresses this problem:
- Store the order and an outbox record in the same database transaction.
- Use a separate process to publish committed outbox records.
- Retry failed publication attempts.
A message may be delivered more than once. The consumer must therefore be idempotent: processing the same message again must not produce an additional effect.
Changes affecting the same order also need appropriate ordering or version handling, so an older event cannot overwrite newer state.
An outbox addresses the database-write-and-message-publication failure gap. It does not make the read model update immediately. AWS documentation on the Transactional Outbox pattern
What Should the Customer See After Placing an Order?
The response must communicate what actually happened.
If the command executes synchronously, the application can return an order ID after the transaction commits. If it only queues the request, it should report acceptance rather than claiming the order has already been placed.
For the delay before the read model catches up, our example has several design options:
- Display the confirmed command result immediately.
- Read from the authoritative store for a sensitive read immediately after the write.
- Wait until the read model reaches the required version, with a timeout.
These are choices to evaluate against the page’s requirements. Displaying the command result does not mean every query in the system is already up to date.
The interaction between user actions, commands, and displayed information is part of the architecture. Udi Dahan’s Clarified CQRS
Can We Trust the Inventory Shown on the Page?
Suppose the page shows one item in stock, but another customer has just bought it.
The order-placement path must check authoritative inventory and update it with appropriate concurrency control. Reading inventory and then saving changes is not enough if concurrent requests can interfere.
Optimistic concurrency, for example, can detect conflicting changes. The application must handle the conflict and, when appropriate, reload the data and reevaluate the business rules. EF Core documentation on concurrency conflicts
The displayed inventory can be stale while the final purchasing decision remains valid. Read-model lag is not permission to violate write-side business rules.
Does CQRS Require Event Sourcing?
CQRS separates read and write responsibilities. Event Sourcing stores the history of events as the basis for reconstructing state.
They can be combined, but CQRS does not require Event Sourcing. Storing the current order state in a relational database while maintaining an independent query path can still be a CQRS design. Greg Young’s discussion of CQRS and Event Sourcing
If the read model combines data owned by several services, the cost of microservice data coupling also matters. If a write starts a sequence of dependent API calls, orchestration, outbox, and sagas address a different part of the problem: tracking progress and recovering from partial failure.
How Much Separation Do We Need?
For our store, we can make the decision by asking concrete questions:
- Is the shared model making business rules or queries difficult to implement?
- Are queries still problematic after improving indexes and data retrieval?
- How much delay is acceptable before a new order appears?
- How will we detect failed event processing and recover the read model?
- Does a second database provide enough value to justify its operational cost?
The answers may lead us to separate application paths with one database. We may introduce a dedicated read model only for expensive reports. Other parts of the application may work well with straightforward CRUD.
Each additional separation should solve a specific problem and provide enough value to justify its cost.