Subscribe to CQRS.NET

* indicates required

What is CQRS?

CQRS(Command and Query Responsibility Segregation) is a pattern that is used to separate Read and Write operations. Commands are used to write data and Queries are used to read data.

The basic idea is that you can divide a system's operations into two sharply separated categories:

  1. Queries. These queries return a result and do not change the state of the system, and they are free of side effects.
  2. Commands. These commands change the state of a system.

Why do we want to separate read and write operations?

  1. In the real world there would be many read operations than write operations. So we would want to create a separate read model that executes faster.
  2. Reads and writes have different concerns. When we write to a database we write entities and when we read data we read aggregated data. We can’t optimize for reads and writes . When we want to speed up the reads we add index over index and so on which increases the performance then our write operations are going to become slow and get complaints about delete and update commands. There wouldn’t be any balance between read and write operations and we will have to compromise on the performance.
  3. When we are reading the data from a database(generally this has indexes on tables) which is different from the one we are writing(non indexed tables).

When to use CQRS?

Use this pattern when the business logic is complex, when many reads happen than writes, when different developers are working on the read model and write model.

When NOT to use CQRS?

Don’t use CQRS when the domain is simple and the application can be developed using simple CRUD operations and scaling isn’t required.