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
15
votes
5 answers

Naming Convention for Commands & Events

I'm just getting into event-driven architectures and would like to know what the convention is for naming commands and events. I know this much: Commands should be in the form DoSomething while events should be in the form SomethingHappened. What I…
Tolu
  • 1,081
  • 1
  • 8
  • 23
15
votes
4 answers

Multiple Aggregates / Repositories in one Transaction

I have a payment system as shown below. The payment can be made through multiple gift coupons. The gift coupons are issued along with a purchase. The customer can make use of this gift coupon for future purchase. When a Payment is made through gift…
LCJ
  • 22,196
  • 67
  • 260
  • 418
15
votes
4 answers

What triggers UI refresh in CQRS client app?

I am attempting to learn and apply the CQRS design approach (pattern and architecture) to a new project but seem to be missing a key piece. My client application executes a query and retrieves a list of light-weight, read-only DTOs from the read…
SonOfPirate
  • 5,642
  • 3
  • 41
  • 97
15
votes
4 answers

Is Reactive Extensions a good fit for a bus?

I have been using Rx for a little bit now to create an event bus (think CQRS/ES) within a single application, and it seems to work great. However, after surveying a bunch of different Event Sourcing frameworks, I have not seen Rx used once. It seems…
Erick T
  • 7,009
  • 9
  • 50
  • 85
14
votes
3 answers

Event Sourcing: Events that trigger others & rebuilding state

I'm struggling to get my head around what should happen when rebuilding the model by replaying events from the EventStore, in particular when events may trigger other events to happen. For example, a user who has made 10 purchases should be…
Kirschstein
  • 14,570
  • 14
  • 61
  • 79
14
votes
3 answers

Is MediatR library overused in CQRS examples on the web?

I'm struggling to understand why so many examples on the web are using MediatR when explaining CQRS patterns, when dealing with commands and queries. Almost everywhere I see examples where Commands and Queries are handled by MediatR but I do not see…
Tomasz Sikora
  • 1,649
  • 3
  • 19
  • 30
14
votes
4 answers

Clean Architecture CQRS with GraphQL API

I'm working on an API server (ASP.NET Core). To prevent spaghetti code and other nastiness in the future, I design the system following Clean Architecture/CQRS (using MediatR). I'm considering to use GraphQL for the API instead of REST (Hot…
Arthur Kater
  • 826
  • 10
  • 17
14
votes
1 answer

Is it possible to scale Axon Framework without Axon Server Enterprise

Is it possible to scale Axon Framework without Axon Server Enterprise? I'm interested in creating a prototype CQRS app with Axon, but the final, deployable system has to be be free from licensing fees. If Axon Framework can't be scaled to half a…
ahoffer
  • 6,347
  • 4
  • 39
  • 68
14
votes
5 answers

Event sourcing without CQRS

I know that CQRS can be implemented with or without event sourcing, but does it work the other side? Does event sourcing without CQRS make sense? If so, how it should be implemented?
k13i
  • 4,011
  • 3
  • 35
  • 63
14
votes
1 answer

How do I register and use a MediatR pipeline handler, for ASP.NET Core?

I'm using ASP.NET Core, the latest MediatR, and MediatR extension for Core's DI. I'm trying to set up a pipeline with validation, using the official blog post. The example is here. I don't understand how to register/use that pipeline class. Another…
grokky
  • 8,537
  • 20
  • 62
  • 96
14
votes
2 answers

Cache invalidation in CQRS application

We practice CQRS architecture in our application, i.e. we have a number of classes implementing ICommand and there are handlers for each command: ICommandHandler. Same way goes for data retrieval - we have IQUery with…
trailmax
  • 34,305
  • 22
  • 140
  • 234
14
votes
4 answers

Occasionally connected CQRS system

Problem: Two employees (A & B) go off-line at the same time while editing customer #123, say version #20, and while off-line continue making changes... Scenarios: 1 - The two employees edit customer #123 and make changes to one or more identical…
14
votes
2 answers

Handling duplication of domain logic using DDD and CQRS

I'm experimenting with DDD + CQRS and I can not understand how to handle this domain logic duplication problems: First, about duplication across domains: Scenario 1: Let's say I have some application which handles office employees. I have 3 bounded…
bezmax
  • 25,562
  • 10
  • 53
  • 84
13
votes
4 answers

Is a document/NoSQL database a good candidate for storing a balance sheet?

If I were to create a basic personal accounting system (because I'm like that - it's a hobby project about a domain I'm familiar enough with to avoid getting bogged-down in requirements), would a NoSQL/document database like RavenDB be a good…
Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
13
votes
1 answer

Architecture: simple CQS

I'm thinking about applying CQS for my ASP.NET MVC web site, but in a very simple matter. I don't mean CQRS, because I want to use the same data source for query and command parts, and so I don't need event sourcing and other more complex…
L-Four
  • 13,345
  • 9
  • 65
  • 109