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
0
votes
0 answers

CQRS + ES Implementation Advice

I'm working on a generic CQRS + ES framework (with nodejs) in the company. Remark: Only RDBMS + Redis (without AOF/RDB persistence) is allowed due to some reasons. I really need some advices on how to implement the CQRS + ES framework.... Ignoring…
0
votes
0 answers

What is an appropriate way to split a MediatR hander?

I am currently using MediatR to handle request/response cycles and wanted some ideas on how best to split an existing request handler which is now performing multiple operations. The handler in question performs the following actions but may include…
chris05
  • 735
  • 4
  • 13
  • 27
0
votes
1 answer

How to handle static data in ES/CQRS?

After reading dozens of articles and watching hours of videos, I don't seem to get an answer to a simple question: Should static data be included in the events of the write/read models? Let's take the oh-so-common "orders" example. In all examples…
user1102018
  • 4,369
  • 6
  • 26
  • 33
0
votes
1 answer

Where should i put a logic that is going to be used in many command/query handler?

So I have a method that receives an array of ids of a given entity in our database, I search in the db for those ids, and the ones that were not found I store them in DB, after that I return an array of those entities containing the ones that were…
Matheus Hatje
  • 987
  • 1
  • 7
  • 18
0
votes
1 answer

CQRS how to create a new record in multiple bounded contexts

How to create a new record (for example product) when the product exists in multiple bounded contexts and the client enters all the data for the product at once?
manfrom
  • 91
  • 7
0
votes
1 answer

Using the command pattern to process messages/ events

We are writing a C# application to process events/ messages from a student application system. The web based portal sends events/ messages to a queue table. We dequeue these and, based on message type, want to process each event. Example events are…
Rich W
  • 85
  • 1
  • 5
0
votes
1 answer

How to read events with Eventflow?

I work on a .NET 6 application which uses Eventflow. I would like to read my events. Events are in the table EventEntity Do you have any idea ? Can we create a ReadModel for this table ? Is there any method to get data from this table ?
0
votes
1 answer

Query vs Transaction

In this picture, we can see saga is the one that implements transactions and cqrs implements queries. As far as I know, a transaction is a set of queries that follow ACID properties. So can I consider CQRS as an advanced version of saga which…
saaho
  • 1
  • 2
0
votes
1 answer

Unit Test For Infrastructure Layer CQRS with Moq and xUnit, How to write tests using In-Memory Db

I am new to unit testing and I have been using the CQRS mediatR pattern. I want to add unit tests for GetAllAsync() and GetByGameNameAsync() methods using Moq and xUnit. The code runs fine as intended but I need unit testing for it. There is no…
LowKeyLo
  • 57
  • 8
0
votes
1 answer

Need of CQRS when SAGA is already there

From what I have understood that transaction is made up of queries which follow ACID properties. SAGA is already there which is suitable for both maintaining data consistency when transaction happens and also for querying. So is the CQRS really used…
saaho
  • 1
  • 2
0
votes
2 answers

how to use automapper in update method cqrs pattern

When I want to update an entity, I face the following error: Message = "The instance of entity type 'Vehicle' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities,…
irancss
  • 41
  • 4
0
votes
1 answer

How to Unit testing in CQRS Pattern C#

I need to know how to Unit testing in CQRS pattern. as a beginer i dont have much idea about that. I develope below Command and queries. I need to Test those Here is my add student command public record AddStudentCommand(Student…
0
votes
1 answer

Resharper 6 live templates for CQRS dev

There's been some chatter on the ddd-cqrs list about people using Resharper live templates to help take care of the boilerplate code that you get when doing CQRS dev. Is this the best way to overcome the boilerplate madness and does anyone have any…
user156888
0
votes
0 answers

Replicate information from Firebase to Device Storage

I have integrated Firebase (Firestore and Storage) inside my app (it is about QRs and chat with images). I have been checking the costs of the data transmission with Firebase and I think bill could skyrocket if my app is really successful. I have…
xavi
  • 147
  • 9
0
votes
0 answers

Sorting self-referencing aggregate roots

I'm using Domain-Driven Design and CQRS on my project and I have a self-referencing aggregate root to model a hierarchical structure: Category CategoryId $id CategoryName $name ?CategoryId $parentId CategoryPosition $position Now I want to…
David
  • 503
  • 3
  • 7
  • 18