CQRS: Separating Responsibilities for Efficient Data Management
Tags: architecture, api, database, eventdriven
Published: 2023-06-26 04:50:46

### Breakdown

1. Overview
2. What is CQRS ?
    - Definition and main components
    - Separation of concerns
    - Difference to the typical CRUD model
    - Scalability benefits
    - Added complexity to the overall development
3. Main components
    - Commands
    - Queries
    - Events
4. Data storage
    - Data separation and projections
    - Data store flexibility and diversity of options
    - Synchronization and eventual consistency
5. Use cases and what problems does it solve
6. Some notes on event-sourcing

### Overview

In the following article, we will delve into the concept of **Command and Query Responsibility Segregation** (CQRS), what need is there for us to consider it, what level of complexity it can bring, and when it is completely justified to adopt this architecture. Additionally, I will touch on its components and different combinations you can typically see it implemented with, like event-driven architectures or even event-sourcing. Although the article will not go into too much detail about side topics.

### What is CQRS ?

Command and Query Responsibility Segregation (CQRS) is a software architecture pattern that separates the responsibilities of read and write operations in a system. Instead of using the same models for both reading and writing data, CQRS introduces a clear distinction between the two operations.

At its core, CQRS recognizes that read and write operations have different requirements and can benefit from different optimizations. By segregating these responsibilities, CQRS enables developers to design systems that are more efficient, scalable, and maintainable.

In a traditional CRUD (Create, Read, Update, Delete) system, the same models and data access layers are often used for both reading and writing data. However, as systems grow in complexity, this approach can lead to performance bottlenecks and limited scalability. CQRS addresses these issues by introducing separate models and data access layers for read and write operations.

Implementing CQRS introduces additional complexity compared to traditional approaches. Developers need to design and maintain separate models, data access layers, and possibly even different storage mechanisms for read and write operations. However, this added complexity can be justified in scenarios where the benefits of scalability, performance, and maintainability outweigh the costs.

### Main components

The key idea behind CQRS is that write operations, also known as commands, focus on changing the system's state. These commands typically involve validation, business logic, and updating the underlying data store. On the other hand, read operations, known as queries, are concerned with retrieving data without modifying the system's state. Queries are performed on materialized views, which are precomputed representations of the data optimized for specific read patterns.

### Commands

Commands represent the intent to perform a specific action or change within the system. They encapsulate the necessary information and parameters to execute a command-oriented operation. Commands are responsible for initiating changes to the data or triggering relevant business logic. This type of operation is responsible for performing only 1 change to the system state, after the command is received and recorded by the command handler, the system can trigger different side effects based on the type and essence of the command. For example, check the chart below.

![Commands chain structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5mlmtpo6m2uhzbs5e4pv.png)

### Queries

Unlike commands, which focus on write operations, queries are concerned with retrieving data based on specific criteria or conditions. They represent the questions we ask the system to obtain relevant information. For example, querying for a list of products that meet certain criteria or retrieving user details based on a specific identifier. Queries are used to fetch information or perform read operations on the data model.

Queries are typically executed by query handlers, which are responsible for retrieving the requested data from appropriate data stores, such as databases or caches. The query handler receives the query, performs the necessary operations, and returns the result set or data object to the caller.

Additionally queries should not be used to perform state changes or trigger any side effects in the system.

Because the purpose of CQRS is to place a “wall” between both sides, we can be even more flexible and design the system in a way that allows us to send the commands to one application, and the queries to another if there is the need for it.


![Query the same data store as writing to](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbtqesc438n3kn1nu1xz.png)


![Query different database](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1afw76tgml0igpixl0it.png)

By separating the read and write responsibilities, CQRS allows each side to be optimized independently. This means that read models can be denormalized, tailored for specific use cases, and optimized for efficient querying. On the write side, models can focus solely on capturing the intent of the operation and enforcing business rules.

One of the main advantages of CQRS is its scalability options. Since read and write operations are decoupled, it becomes easier to scale each side independently based on the specific demands of the system. For example, read models can be replicated or distributed across multiple nodes to handle high read loads, while write models can be optimized for write-intensive operations.

CQRS is not a one-size-fits-all solution and should be considered in cases where a clear distinction between read and write responsibilities is necessary. It is particularly beneficial in scenarios where the read and write patterns differ significantly, such as in business intelligence or reporting systems. CQRS also shines in complex domains where the separation of concerns improves code readability and maintainability.

### Events

When adopting CQRS, it's common to leverage event-driven messaging to handle communication between different components. Events are used to propagate changes made by write operations and can be processed asynchronously by various consumers. This allows for loose coupling, scalability, and the potential for eventual consistency across the system.


![Event-driven side effects](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7zslsiyjv3i148x3f8e.png)

### Data storage

You have several options when it comes to modeling your data management. Of course, you can implement a single data store, so that the commands write to the same store the queries read from. Which has a lot of added value, by ensuring data consistency and perhaps some cost optimization. 

There is also the option to break down the way data is written and read. We can have the commands write the data to a table in one schema for example, and with projection views we can reconstruct this data in other tables elsewhere. Of course, we can push past these limits and start reading from a completely different data store than the one we are writing to. Image is a use case where we write all commands and their result to a relational database, and with the addition of some synchronization, we can read the projection of this data from a NoSql database.


![Different data stores](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/of6dsyyuuq1tig6i29iy.png)


![Use same data store, different table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svq7jl50vf7oor9gye77.png)

### Eventual consistency

We define eventual consistency by referring to a state where all copies of data will be consistent and hold the same information, but not at first or immediately.

However, it's important to note that eventual consistency can introduce challenges in terms of data synchronization between the read and write sources. Strategies like event sourcing, where events are stored as the system's source of truth, can be used to address these challenges. Event sourcing complements CQRS by providing a reliable log of events that can be used to rebuild materialized views or synchronize read models.

### Use cases and what problems can CQRS solve

Let’s start this part with one important thought, do not try to solve problems you don’t have currently or will not have in the near future. Look at what’s in front of you. If CQRS would bring too many changes and requirements for development, no need to focus on it when you’re a startup trying to enter a market.

Here are a few use cases for this architecture that I think are quite relevant:

- You want to focus on scaling read from write capacity separately. Perhaps you need to increase and optimize your read throughput or maybe your write capacity and speed is holding you back
- Loose coupling - You want to decouple your application, and keep the domain or business logic cleaner.
- You want further flexibility in your storage solutions, where you can choose from different technology stacks for each of side of the application
- You are not concerned about eventual consistency (or rather delayed consistency)
- It is great at reducing complexity in entangled code bases, where the domain logic has risen tremendously. Separate the different concerns in the operations, which achieves good readability and understanding of the overall processes.
- You need to perform business intelligence or reporting/analytics where the read patterns can really differ. Read queries can contain a lot of different data and combinations of data. In cases where you need to think about BI and combining, and querying data you will need to consider something like a warehousing solution. You can give a separate projection for BI purposes, not interfering with the regular database for OLTP -  online transaction processing

### Some notes on event-sourcing

When combined with event-sourcing, the system stores all changes as an unchangeable sequence of events, enabling reliable auditing, temporal querying, and the ability to rebuild state at any point in time. This combination provides a solid foundation for building highly scalable, maintainable, and event-driven applications. CQRS with event-sourcing revolutionizes data management by introducing a new level of flexibility, auditability, and temporal querying capabilities.


![Event-sourcing schema example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yevdqbn13ygl8q7dhhwq.png)

In conclusion, CQRS is a powerful pattern that offers advantages in terms of scalability, performance, and maintainability. By separating the responsibilities of read and write operations, developers can optimize each side independently and design systems that better align with specific requirements. While implementing CQRS introduces complexity, it can be a valuable approach in scenarios where the benefits outweigh the costs, such as in complex domains or systems with distinct read and write patterns.