Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use the CQRS design pattern?

My team and I have been discussing using the CQRS (Command Query Responsibility Segregation) design pattern and we are still trying to asses the pros and cons of using it. According to: http://martinfowler.com/bliki/CQRS.html

we haven't seen enough uses of CQRS in the field yet to be confident that we understand its pros and cons

So what do you guys think, when does a problem call for using CQRS?

like image 829
Eric Avatar asked Jan 11 '12 14:01

Eric


People also ask

When should CQRS be used?

When to use CQRS pattern. Consider CQRS for the following scenarios: Collaborative domains where many users access the same data in parallel. CQRS allows you to define commands with enough granularity to minimize merge conflicts at the domain level, and conflicts that do arise can be merged by the command.

Why we use CQRS pattern?

CQRS is a popular architecture pattern because it addresses a common problem to most enterprise applications. Separating write behavior from read behavior, which the essence of the CQRS architectural pattern, provides stability and scalability to enterprise applications while also improving overall performance.

Why you should not use CQRS?

The use of CQRS as part of a system should not belong to its strategic design. It should only be used in the design of some of the components and not become the base of the entire system. If the system is complex enough, using CQRS as the basis for everything may become too complex and some advantages may be lost.

What is the benefit of CQRS in Microservices?

CQRS is one of the important pattern when querying between microservices. We can use CQRS design pattern in order to avoid complex queries to get rid of inefficient joins. CQRS stands for Command and Query Responsibility Segregation. Basically this pattern separates read and update operations for a database.


2 Answers

CQRS is not a pattern that encompasses the whole application.

It is a concept that builds on Domain Driven Design (DDD). And an important strategic concept of DDD is the so-called Bounded Context.

In a typical application there are multiple bounded contexts, any of which can be implemented the way it makes sense. For instance

  • User Management -> CRUD
  • Invoicing -> CRUD
  • Insurance Policy Management (the Core Domain) -> CQRS
  • ...

This probably doesn't answer your question but it might give a little more insight into the topic. To be honest, I don't think it can be answered at all without considering a project's specifics, and even then there is rarely something like a definite best practice.

like image 59
Dennis Traub Avatar answered Oct 16 '22 12:10

Dennis Traub


Well CQRL critics may say that CQRS is complicated and that might be true.

Of course, it's adding overhead developing a simple CRUD application in the CQRS style, so I'd consider using CQRS only in the following cases:

  1. Large team - You can split development tasks between people easily if you have chosen CQRS architecture. Your top people can work on domain logic leaving usual stuff to less skilled developers.
  2. Difficult business logic - CQRS forces you to avoid mixing domain logic and infrastructural operations.
  3. Scalability matters - With CQRS you can achieve great read and write performance, command handling can be scaled out on multiple nodes and as queries are read-only operations they can be optimized to do fast read operations.
like image 39
Mairbek Khadikov Avatar answered Oct 16 '22 14:10

Mairbek Khadikov