Skip to content

CQRS

This pattern is used to solve an issue of how to implement a query that retrieves data from multiple services in a microservice architecture?

Solution

The command query responsibility segregation (CQRS) pattern separates the data mutation, or the command part of a system, from the query part. You can use the CQRS pattern to separate updates and queries if they have different requirements for throughput, latency, or consistency.

The CQRS pattern splits the application into two parts—the command side and the query side—as shown in the following diagram. The command side handles create, update, and delete requests. The query side runs the query part by using the read replicas.

CQRS Example

The diagram shows the following process:

  1. The business interacts with the application by sending commands through an API. Commands are actions such as creating, updating or deleting data.
  2. The application processes the incoming command on the command side. This involves validating, authorizing, and running the operation.
  3. The application persists the command’s data in the write (command) database.
  4. After the command is stored in the write database, events are triggered to update the data in the read (query) database.
  5. The read (query) database processes and persists the data. Read databases are designed to be optimized for specific query requirements.
  6. The business interacts with read APIs to send queries to the query side of the application.
  7. The application processes the incoming query on the query side and retrieves the data from the read database.

Example of CQRS in AWS

In the following illustration, a NoSQL data store, such as DynamoDB, is used to optimize the write throughput and provide flexible query capabilities. This achieves high write scalability on workloads that have well-defined access patterns when you add data. A relational database, such as Amazon Aurora, provides complex query functionality. A DynamoDB stream sends data to a Lambda function that updates the Aurora table.

CQRS Example


Was this page helpful?
-->