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
16
votes
1 answer

Is it OK to have one handler call another when using MediatR?

Or is that considered bad practice or something? I have one notification triggers 4-5 handlers, which in turn call database to retrieve data. Each those calls can also be called separately, so they are request/handler themselves. Thanks.
Whoever
  • 1,295
  • 1
  • 14
  • 21
16
votes
5 answers

Applying CQRS - Is unit testing the thin read layer necessary?

Given that some of the advice for implementing CQRS advocates fairly close-to-the-metal query implementation, such as ADO.NET queries directly against the database (or perhaps a LINQ-based ORM), is it a mistake to try and unit test them? I wonder if…
Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
16
votes
2 answers

How granular should a domain event be?

I am wondering how granular should a domain event be? For example I have something simple, like changing the firstName, the secondName and the email address on a profile page. Should I have 3 different domain events or just a single one? By coarse…
inf3rno
  • 24,976
  • 11
  • 115
  • 197
16
votes
2 answers

Where to verify authorization for a Command?

The question's title resumes pretty much: where do I verify authorization for a Command? For example, setting a customer as preferred involves: MarkAsPreferred controller action (could be Winforms or…
Luiz Damim
  • 3,803
  • 2
  • 27
  • 31
16
votes
3 answers

Why should the event store be on the write side?

Event sourcing is pitched as a bonus for a number of things, e.g. event history / audit trail, complete and consistent view regeneration, etc. Sounds great. I am a fan. But those are read-side implementation details, and you could accomplish the…
Jeremy Rosenberg
  • 792
  • 6
  • 18
15
votes
1 answer

Scala CQRS Framework

I'm looking for CQRS framework for Scala programming language. The idea of using Scala is to avoid annotation hell that appears when Java frameworks such as AxonFramework are used. Can you recommend some?
Mairbek Khadikov
  • 7,939
  • 3
  • 35
  • 51
15
votes
2 answers

CQRS commands and GraphQL mutations

I've just learned about CQRS, and I would like to combine it in a project with a GraphQL based API. However, in order to do that, a question has come to my mind: according to CQRS, commands have to not return anything after its execution. However,…
pitazzo
  • 1,125
  • 1
  • 13
  • 28
15
votes
2 answers

Unable to resolve service for type 'MediatR.IMediator'

I try to make .NET Core API with CQRS, but i cannot build it because of MediatR error: System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType:…
ervin
  • 329
  • 1
  • 2
  • 12
15
votes
1 answer

what is the difference between event driven and domain driven design Microservices?

What is event driven design and Domain driven design? What are the specific benefits using of Domain driven design, event driven design in MicroServices.
G SriHAri
  • 706
  • 2
  • 9
  • 27
15
votes
4 answers

Is Event sourcing using Database CDC considered good architecture?

When we talk about sourcing events, we have a simple dual write architecture where we can write to database and then write the events to a queue like Kafka. Other downstream systems can read those events and act on/use them accordingly. But the…
RBanerjee
  • 957
  • 1
  • 9
  • 18
15
votes
3 answers

EventStore Subscribing to a stream for a category

I have started creating a test application in .Net, that uses EventStore by Greg Young as the backing store for CQRS/ES. In order to make it easy to load up a full aggregate, I save to a stream with a name of "agg-123". For example for a product…
eyeballpaul
  • 1,725
  • 2
  • 25
  • 39
15
votes
3 answers

In CQRS pattern, should work go in domain services or command handlers

Should domain services inject other domain services and do work between each other and have the commandhandler be dumb. OR, should the domain services be dumb (only be used to interface the repository barrier) and the majority of work be done in…
user3689167
  • 863
  • 1
  • 14
  • 28
15
votes
3 answers

CQRS: Synchronizing the Write and Read databases

Can anyone please give me some direction in regards to various ways to synchronize the Write and Read databases? What are different technologies out there, and how do you evaluate each, in terms of realiability, performance, cost to implement, etc.
Mosh
  • 5,944
  • 4
  • 39
  • 44
15
votes
5 answers

Event sourcing infrastructure implementation

I implement Event Sourcing and CQRS pattern in my application. I inspired by CQRS journey where I downloaded sample code. There I found whole infrastructure for Event sourcing (CommandHandlers, EventHandlers, Events, Envelopes ... etc.), but it is…
y0j0
  • 3,369
  • 5
  • 31
  • 52
15
votes
3 answers

Practical examples of how correlation id is used in messaging?

Can anyone give me examples of how in production a correlation id can be used? I have read it is used in request/response type messages but I don't understand where I would use it? One example (which maybe wrong) I can think off is in a publish…
JD.
  • 15,171
  • 21
  • 86
  • 159