Search code examples
cqrscommand-query-separationncqrs

Difference between CQRS and CQS


I am learning what is CQRS pattern and came to know there is also CQS pattern.

When I tried to search I found lots of diagrams and info on CQRS but didn't found much about CQS.

Key point in CQRS pattern

In CQRS there is one model to write (command model) and one model to read (query model), which are completely separate.

CQRS Diagram

How is CQS different from CQRS?


Solution

  • CQS (Command Query Separation) and CQRS (Command Query Responsibility Segregation) are very much related. You can think of CQS as being at the class or component level, while CQRS is more at the bounded context level.

    I tend to think of CQS as being at the micro level, and CQRS at the macro level.

    CQS prescribes separate methods for querying from or writing to a model: the query doesn't mutate state, while the command mutates state but does not have a return value. It was devised by Bertrand Meyer as part of his pioneering work on the Eiffel programming language.

    CQRS prescribes a similar approach, except it's more of a path through your system. A query request takes a separate path from a command. The query returns data without altering the underlying system; the command alters the system but does not return data.

    Greg Young put together a pretty thorough write-up of what CQRS is some years back, and goes into how CQRS is an evolution of CQS. This document introduced me to CQRS some years ago, and I find it a still very useful reference document.