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
19
votes
3 answers

Event Sourcing - How to delete data in an eventstore?

How to work around the issue of deleting data in an eventstore? I need to permanently and completely delete some data in order to comply to privacy laws. I have found these alternatives: Encrypt the data that you need deleted, and store the…
Jan-Terje Sørensen
  • 14,468
  • 8
  • 37
  • 37
18
votes
3 answers

Handling out of order events in CQRS read side

I've read this nice post from Jonathan Oliver about handling out of order events. http://blog.jonathanoliver.com/cqrs-out-of-sequence-messages-and-read-models/ The solution that we use is to dequeue a message and to place it in a “holding table”…
Dasith Wijes
  • 1,328
  • 12
  • 22
18
votes
2 answers

How to support Command in REST while doing REST, CQRS and EventSourcing together?

Consider the following coarse grained REST apis for a Contact resource POST /api/contacts GET /api/contacts GET /api/contacts/:id PUT …
PainPoints
  • 461
  • 8
  • 20
18
votes
4 answers

How to model bank transfer in CQRS

I'm reading Accounting Pattern and quite curious about implementing it in CQRS. I think AccountingTransaction is an aggregate root as it protects the invariant: No money leaks, it should be transfer from one account to another. public class…
Yugang Zhou
  • 7,123
  • 6
  • 32
  • 60
18
votes
4 answers

ICommandHandler/IQueryHandler with async/await

EDITH says (tl;dr) I went with a variant of the suggested solution; keeping all ICommandHandlers and IQueryHandlers potentially aynchronous and returning a resolved task in synchronous cases. Still, I don't want to use Task.FromResult(...) all over…
mfeineis
  • 2,607
  • 19
  • 22
18
votes
2 answers

How To Implement The Query Side Of CQRS in DDD?

I have implemented the command side of DDD using the domain model and repositories, but how do I implement the query side? Do I create an entirely new domain model for the UI, and where is this kept in the project structure...in the domain layer,…
Laz
  • 3,474
  • 9
  • 33
  • 46
18
votes
3 answers

CQRS events do not contain details needed for updating read model

There is one thing about CQRS I do not get: How to update the read model when the raised event does not contain the details needed for updating the read model. Unfortunately, this is a quite common scenario. Example: I add a user to a group, so I…
Golo Roden
  • 140,679
  • 96
  • 298
  • 425
17
votes
2 answers

Event Sourcing: proper way of rolling back aggregate state

I'm looking for an advice related to the proper way of implementing a rollback feature in a CQRS/event-sourcing application. This application allows to a group of editors to edit and update some editorial content, an editorial news for instance. We…
Enrico Massone
  • 6,464
  • 1
  • 28
  • 56
17
votes
3 answers

CQRS Data store approach Nosql Or Sql Server

I am going through the research stage for a new project and I am currently having a debate with a work colleague about the architecture of the project. We have agreed that we will create a distributed messaging system using CQRS and Event sourcing…
user3177251
  • 201
  • 2
  • 8
17
votes
3 answers

User Auth in EventSourcing applications

I'm looking into crafting an app with DDD+CQRS+EventSourcing, and I have some trouble figuring out how to do user auth. Users are intrinsically part of my domain, as they are responsible for clients. I'm using ASP.NET MVC 4, and I was looking to…
tuxbear
  • 551
  • 4
  • 13
17
votes
2 answers

How to model entities that exists in all bounded contexts and that are a central part of the app?

I'm making an application using DDD principles. After thinking everything through as much as I can I'm down to start making my Bounded Contexts. I haven't set the final structure yet, but as of now my application will consist of the following…
cfs
  • 1,304
  • 12
  • 30
17
votes
5 answers

How is Command Query Separation (CQS) implemented when using an ORM?

The principle behind the CQS architectural pattern is that you separate your queries and commands into distinct paths. Ideally, your persistence store can be read/write partitioned, but in my case, there is a single, normalized database. If you are…
pfries
  • 1,720
  • 12
  • 14
17
votes
3 answers

Framework suggestion for CQRS and EventSourcing

Are there any other Java frameworks for CQRS and Event Sourcing other than Axon Framework? I am specifically looking for Java based framework. Must support event sourcing.
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
17
votes
2 answers

CQRS - When to send confirmation message?

Example: Business rules states that the customer should get a confirmation message (email or similar) when an order has been placed. Lets say that a NewOrderRegisteredEvent is dispatched from the domain and is picked up by an event listener that…
Kimble
  • 7,295
  • 4
  • 54
  • 77
16
votes
5 answers

How to "join" two Aggregate Roots when preparing View Model?

Assume that Book and Author are Aggregate Roots in my model. In read model i have table AuthorsAndBooks which is a list of Authors and Books joined by Book.AuthorId When BookAdded event is fired i want to receive Author data to create a new…
Dmitry Schetnikovich
  • 1,752
  • 17
  • 26