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

CQRS + Event Sourcing implementation using akka-persistence

I want to use akka-persistence event sourcing feature in order to implement CRQS + Event Sourcing idea in my new project. The problem is that besides the documentation (http://doc.akka.io/docs/akka/snapshot/scala/persistence.html) I couldn't find…
Mequrel
  • 727
  • 6
  • 12
12
votes
1 answer

Aggregates in CQRS

What are aggregates and how are they used in CQRS (Command-Query-Responsibility-Segregation) and ES (Event-Sourcing)? I'm new to this kind of architecture, and I'd be really happy if someone could please explain this to me. Thanks!
shmuli
  • 5,086
  • 4
  • 32
  • 64
12
votes
1 answer

CQRS/Event Sourcing, how to get consistent data to apply business rules?

at times I'm developing a small project using CQRS pattern and Event Sourcing. I have a structural issue and I'm not aware of which solution to take to resolve it. Imagine the following example: A command is sent with information that a client of…
JPP
  • 360
  • 6
  • 22
12
votes
1 answer

New entity ID in domain event

I'm building an application with a domain model using CQRS and domain events concepts (but no event sourcing, just plain old SQL). There was no problem with events of SomethingChanged kind. Then I got stuck in implementing SomethingCreated…
Pein
  • 1,216
  • 9
  • 16
11
votes
2 answers

Saving domain entities changes

here's real example that will lead to my question: I have an AddCommentToArticleCommand, which has an ArticleId, comment text and email address. This command: uses the article repository to get the article (which is the domain entity) if article…
L-Four
  • 13,345
  • 9
  • 65
  • 109
11
votes
2 answers

CQRS Naming Conventions

I'm implementing a new webservice and while I'm not yet using CQRS I would like to create my service so it could easily be moved to CQRS in the future. So, I'm wondering about naming convention for my DTO classes and also my methods. I've read this…
Antony Scott
  • 21,690
  • 12
  • 62
  • 94
11
votes
3 answers

CQRS and primary key: guid or not?

For my project, which is a potentially big web site, I have chosen to separate the command interface from the query interface. As a result, submitting commands are one-way operations that don't return a result. This means that the client has to…
L-Four
  • 13,345
  • 9
  • 65
  • 109
11
votes
1 answer

Should an API Gateway Communicate via a Queue or directly to other μServices?

I was wondering which of my two methods is more appropriate, or is there event another one? (1) Direct Direct communication between GATEWAY and μSERVICE A UI sends HTTP request to GATEWAY GATEWAY sends HTTP request to μSERVICE A μSERVICE A returns…
Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137
11
votes
2 answers

Is CQRS compatible with Entity Framework Self Tracking Enities / WCF RIA Services?

CQRS makes alot of sense. However it seems mutually exclusive to the approach of using an ORM that provides change tracking. If I have one "channel" for queries to get my objects as RO, then theres no changes to track. If I have another channel for…
Josh Reuben
  • 577
  • 3
  • 20
11
votes
4 answers

Domain driven design concepts and relation with CQRS

I recently started to become familiar with DDD concepts and CQRS and I realized that one of the most important concepts in CQRS is DDD beside load balancing, NServiceBus and etc but I am curious if we can use DDD concept alone without using it in…
sajadre
  • 1,141
  • 2
  • 15
  • 30
11
votes
2 answers

Version number in event sourcing aggregate?

I am building Microservices. One of my MicroService is using CQRS and Event sourcing. Integration events are raised in the system and i am saving my aggregates in event store also updating my read model. My questions is why we need version in…
Imran Arshad
  • 3,794
  • 2
  • 22
  • 27
11
votes
3 answers

Do we really need Event Sourcing and CQRS in microservices?

In my understanding when database transactions span across microservices ,we can solve this problem with using message-broker(kafka,RabbitMQ etc) by publishing events so that Subscriber Microservices can update their database by listening to these…
Riding Cave
  • 1,029
  • 1
  • 15
  • 32
11
votes
1 answer

App client authentication (login) and CQRS

I'm interested in practical scenarios of authentication/login in web application when CQRS pattern is used to build the system. Say we using HTTP services for commands/queries. And authentication with JWT (or any other authentication token) We send…
WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
11
votes
3 answers

Multi-Tenant CQRS Architecture

My team is beginning implementation of a greenfield application, with a requirement for multi-tenancy. I have been doing a large amount of research on patterns for simple scalability, especially on distributed cloud-based infrastructure, and CQRS…
Mafuba
  • 603
  • 6
  • 19
11
votes
1 answer

How to handle situations, when the read model became out of sync with the event logs?

When snapshots of aggregates became out of sync with event log i can simply replay my events from early snapshots (which supposed to be in sync). The same i can do when i add/remove new fields or when i modify logic of existing handlers. In case i…
Dmitry Schetnikovich
  • 1,752
  • 17
  • 26