Questions tagged [cqrs]

Command-Query Responsibility Segregation (CQRS) is an architectural pattern which separates commands (that change the data) from queries (that read the data). See 'about cqrs tag' for more details and references to learning materials. Not to be confused with Command-Query Segregation ([CQS]), a principle of object method design which CQRS incorporates.

The Command-Query Responsibility Segregation (CQRS) principle, in its core form, merely introduces separation of reads from writes. This simple approach brings the following benefits:

  • denormalized query persistence is optimized for the reads (which usually make up the most of the persistence I/O) resulting in better performance and user experience;
  • we can optimize our read side for the needs of the UI (for example, fetching the dashboard for the user in a single query) which will result in better development experience and less risk of breaking something on the write side.
  • read side can be put on some cloud storage, which is inherently optimized for the reads, could be partitioned, replicated and even distributed via a CDN;
  • by offloading data reads to synchronous queries we automatically increase the performance of the write side - now it has lower stress and lower probability of hitting a deadlock (which you should still account for).

Deeper introduction and more learning materials are available for study in CQRS Starting Point.

What about things that you hear in any CQRS talk: commands, events, DDD, eventual consistency and almost-infinite scalability? These are distinct architectural patterns with their own benefits and peculiarities. These patterns play so nicely with the CQRS principle (separation of reads from the writes), that they are often perceived as one thing.

So when we say "CQRS" this often means: "CQRS Principle" + "Message-driven architecture with commands, events and queries" + "Domain-Driven Design Methodology". This combination is one of the most frequent variations of "CQRS Architecture" (sometimes Event Sourcing is included there by default as well). Success of this variation is the reason why there is so much buzz and hype around the original CQRS term.

So here's what we have here:

  • CQRS - buzzword that could mean a lot of things.
  • CQRS Principle - principle that dictates separation of reads from writes in your system.
  • CQRS Architectures - specific architectural designs based upon the CQRS Principle and a few other time-proven methodologies and approaches. They usually come with a clear evolution path enabling migration of live application to more elaborate design, if needed.
  • DDDD (Distributed Domain-Driven Design) - one of the CQRS Architectures, as presented by Greg Young. It is based upon "CQRS Principle" + "DDD" + "Message-based architecture" + "Event Sourcing".

Not to be confused with Command-Query Segregation (), a principle of object method design which CQRS incorporates.

References

1943 questions
23
votes
4 answers

How to manage ViewModel changes in a CQRS + Event Sourcing Architecture

We are currently evaluating CQRS and Event Sourcing architectures. I am trying to understand what the maintenance implications of using this kind of design are. Two questions I am struggling to find answers to are this: 1) What happens if, after…
James
  • 7,877
  • 7
  • 42
  • 57
23
votes
2 answers

Snapshot taking and restore strategies

I've been reading about CQRS+EventSoucing patterns (which I wish to apply in a near future) and one point common to all decks and presentations I found is to take snapshots of your model state in order to restore it, but none of these share…
Mikhas
  • 851
  • 1
  • 12
  • 31
22
votes
2 answers

Sending an e-mail or SMS using CQRS and domain-driven-design

At this moment we are building a new architecture that is based on the principles of CQRS and domain-driven-design. We are now having some discussions about how we should deal with external communication. To make the question more concrete I use the…
llMll
  • 495
  • 4
  • 9
22
votes
4 answers

CQRS Event Sourcing check username is unique or not from EventStore while sending command

EventSourcing works perfectly when we have particular unique EntityID but when I am trying to get information from eventStore other than particular EntityId i am having tough time. I am using CQRS with EventSourcing. As part of event-sourcing we…
Roshan
  • 873
  • 12
  • 33
22
votes
3 answers

What should be returned from the API for CQRS commands?

As far as I understand, in a CQRS-oriented API exposed through a RESTful HTTP API the commands and queries are expressed through the HTTP verbs, the commands being asynchronous and usually returning 202 Accepted, while the queries get the…
ale64bit
  • 6,232
  • 3
  • 24
  • 44
22
votes
3 answers

Can I refactor to Model View Query Handler?

In our MVC application all of our read actions as a paramter take a query which implements: public interface IQuery { } Within the action the query is passed to a bus which locates a handler and returns a view model. So controllers…
GraemeMiller
  • 11,973
  • 8
  • 57
  • 111
21
votes
3 answers

Uniqueness validation when using CQRS and Event sourcing

I'm trying to implement my own CQRS infrastructure with Event Sourcing to learn it better. As a sample project I'm implementing a blog engine, I know it might not be a perfect fit but I just want to work on something real. The problem I've come to…
Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
21
votes
3 answers

CQRS and CRUD screens

One of the basic tenets of CQRS, as I understand it, is that commands should be behaviour-centric, and have a value in the business or the UL, and not data-centric, ie., CRUD. Instead of focusing on updating a customer, we have commands like…
blockhead
  • 9,655
  • 3
  • 43
  • 69
21
votes
1 answer

Read side implementation approaches using CQRS

I've moved to the project which is actively using CQRS + event sourcing. From the first glance it's implemented in accordance with all those books and blogs, but finally I realized what exactly is peevish in the implementation. Here is CQRS…
Max
  • 601
  • 8
  • 21
21
votes
1 answer

Refactoring "procedural" WCF service

I'm tryng to refactor a monstrous WCF service into something more manageable. At the time of writing, the service takes about 9 dependencies via constructor, which makes unit testing it very difficult. The service is handling local state via state…
Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
21
votes
3 answers

CQRS sagas - did I understand them right?

I'm trying to understand sagas, and meanwhile I have a specific way of thinking of them - but I am not sure whether I got the idea right. Hence I'd like to elaborate and have others tell me whether it's right or wrong. In my understanding, sagas are…
Golo Roden
  • 140,679
  • 96
  • 298
  • 425
20
votes
7 answers

GUI recommendations for eventual consistency?

When using distributed and scalable architecture, eventual consistency is often a requirement. Graphically, how to deal with this eventual consistency? Users are used to click save, and see the result instantaneously... with eventual consistency…
Steve B
  • 36,818
  • 21
  • 101
  • 174
20
votes
3 answers

Command Validation in DDD with CQRS

I am learning DDD and making use of the CQRS pattern. I don't understand how to validate business rules in a command handler without reading from the data store. For example, Chris wants to give Ashley a gift. The command might be…
chris
  • 6,653
  • 6
  • 41
  • 54
19
votes
2 answers

Event Sourcing Commands vs Events

I understand the difference between commands and events but in a lot of cases you end up with redundancy and mapping between 2 classes that are essentially the same (ThingNameUpdateCommand, ThingNameUpdatedEvent). For these simple cases can you / do…
user842807
  • 191
  • 1
  • 4
19
votes
1 answer

CQRS event store aggregate vs projection

In a CQRS event store, does an "aggregate" contain a summarized view of the events or simply a reference to the boundary of those events? (group id) A projection is a view or representation of events so in the case of an aggregate representing a…
webish
  • 701
  • 2
  • 9
  • 17