c# – CQRS – When to use and why to use it?

Question:

What are the advantages of using the CQRS (Command/Query Responsibility Segregation) pattern? What would be the disadvantages?

Answer:

In summary, CQRS is useful and recommended in complex domain scenarios or in high performance applications.

To paraphrase Microsoft's documentation, when compared to the conventional CRUD model:

Compared to the single data model used in CRUD-based systems, using separate update and query models for the data in CQRS-based systems simplifies design and implementation. However, one drawback is that, unlike CRUD projects, CQRS code cannot be generated automatically using scaffolding mechanisms.

So one of the biggest advantages is the performance improvement in complex applications and one of the biggest disadvantages is the possible increase of complexity of the code that represents the model. However, CQRS is not recommended in all scenarios.

Recommended scenarios:

  • Collaborative domains with parallel operations on the same data model.
  • Multi-step task-based user interfaces
  • Systems where the amount of data read is much greater than the number of writes
  • Scenarios where the business rule changes frequently
  • Integration between systems, where the failure of one cannot stop the other

Not recommended scenarios:

  • Domains with simple business rules (e.g. CRUD-based applications)

Furthermore, according to Martin Fowler's recommendations, the CQRS standard should be applied to parts of a system and not to the whole system. Attempting to implement this pattern on a system whose complexity is not supported will only significantly decrease development productivity and complexity. Also consider the team's experience with DDD, as a team that is immature in correctly implementing rich domains can be disastrous in trying to implement CQRS.

There is no difference between implementing this pattern with .NET Core or another framework or language, the considerations of using it (or not) are not limited to a technical one.

Sources:

Microsoft Documentation: https://docs.microsoft.com/en-us/azure/architecture/patterns/cqrs

Martin Fowler: https://martinfowler.com/bliki/CQRS.html

Scroll to Top