CQRS Introduction

CQRS ( Command Query Responsibility Seggregation)

Ankit Verma
Latest posts by Ankit Verma (see all)

CQRS as the name suggests it is about separating the responsibility of writing and reading of data. CQRS is the code pattern, not the architectural pattern.

With the growth of the Internet, we cannot think of creating applications for few users; most of the new applications have premises of scalability, performance and availability. It’s complex to create a model that meet the need to work well with both tens and thousands of users simultaneously. The reason for this lies in DATABASES.

The reason is database can create deadlocks, timeout and slowness which is the result of application in high demand.

See also  C# Collections: When to use what?

More instances of an application is not a guarantee that the application will always be available. In this scenario, the application is totally dependent on the availability of the database.

Database scaling can be much more complex and expensive than scaling application servers. Complex Queries can be performed to obtain database data.

Understanding CQRS

CQRS teaches us the division of responsibility for writing and reading data, both conceptual and using different physical storage. This means that there will be separate means for recording and retrieving data from databases. Queries are done synchronously in separate denormalized database, and writes asynchronously to a normalized database.

CQRS always bring in extra complexity and so it is necessary to evaluate the scenario in which it is really required to work with this pattern. The COMMAND will be responsible for modifying the state of the data in the application, and the QUERY that is the operation responsible for retrieving information from the database.

Advantages

  1. The commands are asynchronous and processed in the queue so as to reduce the waiting time.
  2. Writing and reading data do not compete for the same resources.
  3. Queries on query stack are made separately and independently and do not depend on command stack processing.
  4. It is possible to scale the command stack and query stack processes separately.
See also  Working with Azure Container Instances (ACI)

Disadvantages:

  1. It brings the extra complexity in the system.
  2. Further attention is required when using eventual consistency model. This concept is not mandatory but requires more attention.

It’s important to note that CQRS is not an architectural pattern and can be understood as a form of componentization part of your application.

REFRENCES:

axoniq.io/resources/cqrs

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