Architecturecommand query responsibility segregationread modelwrite modelcqs

CQRS

Definition

Command query responsibility segregation splits an application into a write side that validates commands and enforces rules, and one or more read models shaped for specific queries, typically updated asynchronously from the write side's events.

In practice

Reads and writes often differ in shape and volume: writes need rules and normalised data, while a dashboard wants a denormalised view read thousands of times per second. Separate models let each side be optimised, and read models can live in a different store, such as a search index.

Read models lag behind writes, and that lag fails silently with older answers rather than errors. Return a version from each command so a user can read their own writes, and monitor projection lag. Start with separate command and query code over one database; separate stores add real operational cost.

Example

PlaceOrder writes to the normalised orders tables; a projector consumes OrderPlaced and updates a customer_order_summary table that the "My orders" page reads with one query.