Introduction to Command Query Responsibility Segregation

This pattern is based on the idea of command-query separation (CQS). So, according to CQS, we have to divide our command and query separately to make the system more reactive and robust. This command means the query to write something into the database to change the state of the domain, and the query means ready only query that doesn't change the state of the domain. These queries are based on the ready on access either from another database or somewhere in the cache. Let's look at the following:

  • Commands, changing state of the system
  • Queries, getting some information from the system

The CQRS naturally fits with some other architectural patterns, such as event-based programming models. It's common to see CQRS systems split into separate services communicating with event collaboration. This allows these services to easily take advantage of Event Sourcing.

This architectural pattern improves the performance of a distributed application where the application is required to process complex domain-driven programming. So, it separates this domain-driven module from the other part where we are querying data for representation and reading only. We can use either messaging or event-driven software architecture asynchronously with non-blocking calls to write into the database. Let's see the following application architecture and see how the CQRS pattern is used in the system architecture:

As you can see, the system has divided into two different parts, such as read only query (the components used for reading) and command query (the components used for writing). The commands are responsible for performing an action or changing states of the system. The Autonomous Component is a piece of business logic that can update the Domain model and inform the client about whether this change has been accepted or not. The Autonomous Component also notifies everyone whenever any changes are detected. In the preceding diagram, the first AC component publishes a domain event to update the database and also notifies another AC component to update cache using in the application.

The second part is querying data for representation for the client, it is getting information from the system without changing its state. This part uses only the View model rather than the Domain model. This CQRS pattern is all about the separation of concerns between the Domain and View models, and these models can operate asynchronously to improve the performance of the application. Let's see another pattern that is commonly used with the CQRS pattern—the Event Sourcing pattern.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.173.53