Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 261
Mr punch Avatar asked Dec 13 '15 19:12

Mr punch


People also ask

What is CQRS in Microservices?

CQRS is one of the important pattern when querying between microservices. We can use CQRS design pattern in order to avoid complex queries to get rid of inefficient joins. CQRS stands for Command and Query Responsibility Segregation. Basically this pattern separates read and update operations for a database.

What is CQS programming?

Command-query separation (CQS) is a principle of imperative computer programming. It was devised by Bertrand Meyer as part of his pioneering work on the Eiffel programming language. It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both.

Is CQRS a design pattern?

Command query responsibility segregation (CQRS) is a programming design pattern that treats retrieving data and changing data differently. CQRS uses command handlers to simplify the query process and hide complex, multi-system changes.

Is CQRS complicated?

It's often referenced alongside other patterns that can make it seem difficult and complicated. Is CQRS complicated? No. CQRS is simple.


3 Answers

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.

like image 109
David Hoerster Avatar answered Sep 22 '22 19:09

David Hoerster


This is an old question but I am going to take a shot at answering it. I do not often answer questions on StackOverflow, so please forgive me if I do something outside the bounds of community in terms of linking to things, writing a long answer, etc.

There are many differences between CQRS and CQS however CQRS uses CQS inside of its definition! Let's start with defining the two and then we can discuss differences.

CQS defines two types of messages depending on their return value: no return value (void) specifies this is a Command; a return value (non-void) specifies this method is a Query.

  • Commands change information
  • Queries return information

Commands change state. Queries do not.

Now for CQRS, which uses the same definition as CQS for Commands and Queries. What CQRS says is that we do not want one object with Command and Query methods. Instead we want two objects: one with all the Commands and one with all the Queries.

The idea overall is very simple; it's everything after doing this where things become interesting. There are numerous talks online, of me discussing some of the attributes associated (sorry way too much to type here!).

like image 27
Greg Young Avatar answered Sep 18 '22 19:09

Greg Young


  • CQS is about Command and Queries. It doesn't care about the model. You have somehow separated services for reading data, and others for writing data.
  • CQRS is about separate models for writes and reads. Of course, usage of write model often requires reading something to fulfill business logic, but you can only do reads on read model. Separate Databases are state of the art. But imagine single DB with separate models for read and writes modeled in ORM. It's very often good enough.

I have found that people often say they practice CQRS when they have CQS.

like image 29
Tomasz Rzepecki Avatar answered Sep 19 '22 19:09

Tomasz Rzepecki