Greg Young [MVP]

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Command Query Separation?

There has been a lot of talk lately about Command and Query Separation. One thing that has come up with many people in learning it is that they get confused between CQS [Meyer] and CQS [Architecture or DDD]. As such many have called for us to rename the latter to something different as although it is very similar to CQS [Meyer] they find it to be quite different. I however disagree with this let’s look at our definitions.

Meyer:
   Separate command methods that change state from query methods that read state.


Current:
   Separate command messages that change state from query messages that read state.

The reader should notice that these two are nearly identical, the principle has remained the same, only the definitions of what commands and queries are has changed. A really quick reader will also be quick to jump on me for not bringing in all of what CQS has come to be known as.

In CQS [Architecture/DDD/whatever] we also go one step further and we create two end points (or objects) by splitting the responsibilities of the original concept. We give one sub-concept the responsibility of processing all of the commands and another the responsibility of processing all of the queries. We do this because we have come to understand that there are very different non-functional requirements for each and that by separating the two we can better tailor solutions.

One question that has been hitting me is whether or not this is something that may work as a pattern at the object granularity as opposed to the endpoint as many of these types of patterns are relevant to both scopes.

This part is new and is not CQS [Meyer] I am going to throw out a quick pattern name for this and call it Command and Query Responsibility Segregation or CQRS as we are in fact simply taking what would usually be one object and splitting its responsibilities into two objects.

So the first step is that we apply CQS as a principle, and we then come through and apply the CQRS pattern in order to end up with what people are currently calling “CQS”. I would hope that this is a clearer explanation and manages to bring in the original intent of command and query separation while realizing that there is in fact an important difference.

 

Does this help clear things up or does it only make things more confusing?


Posted Thu, Aug 13 2009 1:07 AM by Greg

[Advertisement]

Comments

Jonathan wrote re: Command Query Separation?
on Thu, Aug 13 2009 8:41 AM

Having a unique term that is search friendly is important.  "Command and Query Responsibility Segregation" is a good term and would ultimately be fine, but it's a bit of a mouthful.

What about <b>Command Query Distribution</b>?

Greg wrote re: Command Query Separation?
on Thu, Aug 13 2009 10:02 AM

I am 100% open to other names (feel free to drop them) ... to me the important part is the distinction and the recognition that it is in fact the CQS principle to prevent later confusion amoungst people

on Thu, Aug 13 2009 12:59 PM

Pluralsight On-Demand! I plan to have a full post on Pluralsight On- Demand! very soon. I became a subscriber recently and have found it to be an amazing training tool. Its just ike being in the classroom with the best instructors in the business and

Jason wrote re: Command Query Separation?
on Sun, Aug 16 2009 2:45 PM

Hey Greg,

Could I entice you into giving your thoughts and insights into when a/each context for it's querying would go directly to the database versus through messaging into a reporting context.

Daniel Auger wrote re: Command Query Separation?
on Mon, Aug 17 2009 2:12 PM

I think CQRS describes it as an applied pattern very well, and I do like the idea of having a specific term for this pattern to avoid confusion. I'll admit that I was completely confused a couple months ago when I first started reading about CQS [Architecture or DDD] through your blogs solely because CQS [Meyer] is typically explained with the word "method". My mind latched on to "method" and it took me awhile to see it as a broader concept.

Kevlin Henney wrote re: Command Query Separation?
on Sat, Aug 22 2009 5:02 AM

CQS historically and presently means what Meyer meant it to be mean. The CQS [Young] interpretation describes a good architectural approach that shares the same underlying principles but goes further. So, similar at one level, but ultimately not the same.

Using the same term and abbreviation is the cause of the confusion and, although the explanation is clear, anything that needs to be re-explained every time it is used (or qualified with suffixes such as [Meyer] or [Young]) is obviously suffering a usability problem.

The naming of patterns is never necessarily an easy task (we devoted more space than anticipated to the topic in POSA5), but sometimes listening to how people might otherwise describe or explain it can provide hints as to alternative wording. The first time I had the confusion (having ignored a whole thread on the topic, because I have known for a couple of decades what CQS means... or so I thought), I realised "Oh, you mean, CQS _Messaging_. Aha!" Based on the wording you have used in the post, CQS Messaging is the explanation (also, Command-Query Separated/Segregated Messaging), the summary and a less confusing alternative handle.

Vaughn Vernon wrote re: Command Query Separation?
on Sat, Aug 22 2009 3:04 PM

How about ACQS for Architectural Command Query Separation? This name embodies the intent to be similar, and the difference.

Simon says... wrote [EN] Is DTO an excuse?
on Wed, Sep 9 2009 3:33 PM

This thought came into my mind when I was returning home from work. It all started with this old dilemma

Alexandre Garino wrote re: Command Query Separation?
on Thu, Sep 10 2009 11:21 AM

I'd like to see an example app (package structure, etc.) which deal with the CQS pattern. Have you got some pointers?

Simon says... wrote DTO vs CQS
on Thu, Sep 17 2009 5:02 AM

Dzisiejsza notka jest wolnym tłumaczeniem jednej z poprzednich, którą opublikowałem in English. Ilość

Simon says... wrote [EN] Execution-Definition Separation
on Wed, Sep 23 2009 5:44 AM

Extrapolating CQS to the requirements of the latest system I've been working on I found useful to define

Ian Cooper wrote The catalogue metaphor and command-query seperation architectures
on Thu, Oct 8 2009 8:47 AM

Command-Query separation is being popularized today with architects like Greg Young and Udi Dahan as

BlogCoward wrote cqrs for dummies
on Thu, Dec 31 2009 9:07 PM

cqrs for dummies

Kevin wrote re: Command Query Separation?
on Fri, Feb 12 2010 2:00 PM

Hello Greg,

I've watched your presentation in InfoQ about DDD and CQS.

In your sample, you told that Domain Model (in CQS) is write-only. But, in this case, can I load data from database to, for example, to execute a command?

So, is legal to load data from database when I running a command?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us