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
47
votes
4 answers

IRequestHandler return void

Please see the code below: public class CreatePersonHandler : IRequestHandler { public async Task Handle(CreatePersonCommand message, CancellationToken cancellationToken) { return true; } } It…
w0051977
  • 15,099
  • 32
  • 152
  • 329
47
votes
3 answers

CQRS Commands and Queries - Do they belong in the domain?

In CQRS, do they Commands and Queries belong in the Domain? Do the Events also belong in the Domain? If that is the case are the Command/Query Handlers just implementations in the infrastructure? Right now I have it layed out like…
Sam
  • 15,336
  • 25
  • 85
  • 148
47
votes
2 answers

How do Repositories fit with CQRS?

According to Fowler (here), a repository "mediates between the domain and data mapping layers, acting like an in-memory domain object collection." So, for example, in my Courier Service application, when a new run is submitted, my application…
SonOfPirate
  • 5,642
  • 3
  • 41
  • 97
42
votes
5 answers

Value Objects in CQRS - where to use

Let's say we have CQRS-inspired architecture, with components such as Commands, Domain Model, Domain Events, Read Model DTOs. Of course, we can use Value Objects in our Domain Model. My question is, should they also be used…
driushkin
  • 3,531
  • 1
  • 24
  • 25
42
votes
4 answers

CQRS and Event Sourcing Difference

What is the difference between CQRS (Command Query Responsibility Segregation) and Event Sourcing? I believe Event Sourcing is a type of CQRS. What distinguishes each, and what makes Event Sourcing different from other types of CQRS? Thanks,
user8280126
42
votes
3 answers

Relation between command handlers, aggregates, the repository and the event store in CQRS

I'd like to understand some details of the relations between command handlers, aggregates, the repository and the event store in CQRS-based systems. What I've understood so far: Command handlers receive commands from the bus. They are responsible…
Golo Roden
  • 140,679
  • 96
  • 298
  • 425
41
votes
1 answer

Alternatives to many-to-many relationships with CQRS

How do we model classic many-to-many relationships with CQRS/DDD? I know that both DDD and CQRS implementations and solutions tend to be domain-specific, so it may be difficult to come up with a general answer to this question. However, let's assume…
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
38
votes
6 answers

Communicating between two Bounded Contexts in DDD

I have few different Bounded Contexts in the domain. The validation of a CRUD operation is built in each Bounded Context. For example, I can create an entity called GAME only if the person creating it is a Group Leader. I have two Bounded Contexts…
wonderful world
  • 10,969
  • 20
  • 97
  • 194
37
votes
3 answers

Add validation to a MediatR behavior pipeline?

I'm using ASP.NET Core, the built-in container, and MediatR 3 which supports "behavior" pipelines: public class MyRequest : IRequest { // ... } public class MyRequestHandler : IRequestHandler { public string…
grokky
  • 8,537
  • 20
  • 62
  • 96
36
votes
4 answers

In CQRS, should my read side return DTOs or ViewModels?

I'm having a debate with my coworkers in the design of the read side of a CQRS application. Option 1: The application read side of my CQRS application returns DTOs, e.g: public interface IOrderReadService { public OrderDto Load(int…
Josh Kodroff
  • 27,301
  • 27
  • 95
  • 148
36
votes
1 answer

Business rule validators and command handler in CQRS

I am new to CQRS and I am tying to make sense of business rule validation within the write side (domain). I know that client side validation should be done in terms of valid date (required field, string length, valid email, etc) and business…
kind_robot
  • 2,383
  • 4
  • 31
  • 45
36
votes
6 answers

best event sourcing db strategy

I want to setup a small event sourcing lib. I read a few tutorials online, everything understood so far. The only problem is, in these different tutorials, there are two different database strategies, but without any comments why they use the one…
SharpNoiZy
  • 1,099
  • 2
  • 11
  • 22
35
votes
3 answers

Event Sourcing and Read Model generation

Assuming Stack Overflow domain problem and the following definition of events: UserRegistered(UserId, Name, Email) UserNameChanged(UserId, Name) QuestionAsked(UserId, QuestionId, Title, Question) Assuming the following state of event store (in the…
Dmitry Schetnikovich
  • 1,752
  • 17
  • 26
35
votes
4 answers

How to handle set based consistency validation in CQRS?

I have a fairly simple domain model involving a list of Facility aggregate roots. Given that I'm using CQRS and an event-bus to handle events raised from the domain, how could you handle validation on sets? For example, say I have the following…
34
votes
3 answers

Do I use Azure Table Storage or SQL Azure for our CQRS Read System?

We are about to implement the Read portion of our CQRS system in-house with the goal being to vastly improve our read performance. Currently our reads are conducted through a web service which runs a Linq-to-SQL query against normalised data,…