What is CQRS Pattern?

Description of your first forum.
Post Reply
shewlykhatun708
Posts: 14
Joined: Wed Dec 04, 2024 4:53 am

What is CQRS Pattern?

Post by shewlykhatun708 »

Command Query Responsibility Segregation In short, CQRS is an architectural design model based on the separation of write and read operations. The basis of CQRS is Command Query Separation ( CQS ). CQS was first developed by Bertrand Mayer in a project he was working on with the Eiffel programming language. According to CQS , each method must either contain a command that performs some task or use a query to retrieve the required data , never both.

Although CQS and CQRS are very similar at first glance , they cannot be equated. CQS is used more at the method or class level , while CQRS is applied at the application level based on CQS . CQRS commands and queries allow different datastores and models (data or ORM ) and data is connected between datastores via a service bus . CQS also follows the same logic, but shares the same datastores and the same models . This allows CQRS to be used in more complex and large projects.

Using CQRS in large projects helps uk b2b leads to achieve some principles such as performance , scalability and security at the top level. As we mentioned in this approach, the methods are divided into 2 bases:

Image

Command – used to change any data. ( Insert, Update, Delete )
Query – used only to retrieve data, not change it. ( Select )
One of the most common ways to implement CQRS is the command pattern . At Runtime , the base class receives the command , the appropriate object handler is created, and the method to execute the command is called.

cqrs-pattern
Query Dispatcher and Command Dispatcher are used to select the appropriate handler for the given command/query and execute it. Query handler retrieves data from the repository and associates the result with the corresponding DTO (Data Transfer Object) object. The Dispatcher is responsible for returning data to the UI (User Interface) . The command handler attempts to validate data before making any changes to the system. You can see this link for a code example.

CQRS can be used when:

It can be used in projects that are complex or where requirements change frequently.

It can be used in systems with high data traffic.

It is not recommended to be used in systems where small and simple CRUD (Create, Read, Write, Delete) operations are performed in order not to unnecessarily complicate the code readability.

Advantages of CQRS :

Separation of Read and Write operations helps to improve performance , scalability and security .

You can use different databases for your Read and Write operations (For example, you can use MySQL for write operations and Couchbase for read operations).

Since the Read and Write operations are separated from each other, there is no need to wait for the write operation in any read operation .
Post Reply