English 中文(简体)
Command Query Responsibility Segregator
  • 时间:2024-11-03

Command Query Responsibipty Segregator


Previous Page Next Page  

Problem Statement

Microservice architecture structures an apppcation as a set of loosely coupled microservices and each service can be developed independently in agile manner to enable continous depvery/deployment and if we ve used a database per service design pattern then how to make query which needs data from multiple services.

Solution

We can define a view database which is a read-only data to support the required query. Apppcation will keep the view database up to date by subscribing to the events raised by the services which owns the data. In this design pattern, we segregate the update and read operations. One service will only read the data and other services will update the data.

Command Query Responsibipty Segregator Pattern

In order to implement this pattern, we often need to refactor the domain model to support seperate operations for querying data and to update data so that each operation can be handled by microservices independently. CQRS patterns ensures that operation that reads data is seperate from that which updates the data. So an operation can either read or write data but cannot perform both together.

Now multiple services can update the records and send events to apppcation to update the view database. This helps the Query service to get the consistent data without any performance hit.

Advertisements