Sitemap
Sathishkumar

Sathishkumar

Technical Lead, We will do design and develop a project from scratch. We do Python, Java, PHP development.

CQRS Pattern

3 min readSep 27, 2021

--

What is CQRS pattern?

CQRS stands for Command and Query Responsibility Segregation, a pattern that separates read and update operations for a data store. Implementing CQRS in your application can maximize its performance, scalability, and security. The flexibility created by migrating to CQRS allows a system to better evolve over time and prevents update commands from causing merge conflicts at the domain level.

Problem and Context

In traditional architectures, the same data model is used to query and update a database. That’s simple and works well for basic CRUD operations. In more complex applications, however, this approach can become unwieldy. For example, on the read side, the application may perform many different queries, returning data transfer objects (DTOs) with different shapes. Object mapping can become complicated. On the write side, the model may implement complex validation and business logic. As a result, you can end up with an overly complex model that does too much.

Traditional architecture

Solution

CQRS separates reads and writes into different models, using commands to update data, and queries to read data.

  • Commands should be task-based, rather than data centric.
  • Commands may be placed on a queue for asynchronous processing, rather than being processed synchronously.
  • Queries never modify the database. A query returns a DTO that does not encapsulate any domain knowledge.

The models can then be isolated, as shown in the following diagram, although that’s not an absolute requirement.

CQRS Pattern with same schema

Another solution is separates a write and read in two different schema.

Press enter or click to view image in full size
CQRS with different schema’s

If we use two different schema’s we have to sync data from write schema to read schema. For sync data between 2 schema we can use Event Sourcing. All the update command will create a message and publish to message queue. Asynchronously message are saved in to read schema. One main advantage when we use Event Sourcing is same event can be used to notify other components.

Benefits of CQRS

  • Independent scaling. CQRS allows the read and write workloads to scale independently, and may result in fewer lock contentions.
  • Optimised data schemas. The read side can use a schema that is optimised for queries, while the write side uses a schema that is optimised for updates.
  • Security. It’s easier to ensure that only the right domain entities are performing writes on the data.
  • Separation of concerns. Segregating the read and write sides can result in models that are more maintainable and flexible. Most of the complex business logic goes into the write model. The read model can be relatively simple.
  • Simpler queries. By storing a materialised view in the read database, the application can avoid complex joins when querying.

Challenges

  • Complexity. The basic idea of CQRS is simple. But it can lead to a more complex application design, especially if they include the Event Sourcing pattern.
  • Messaging. Although CQRS does not require messaging, it’s common to use messaging to process commands and publish update events. In that case, the application must handle message failures or duplicate messages. See the guidance on Priority Queues for dealing with commands having different priorities.
  • Eventual consistency. If you separate the read and write databases, the read data may be stale. The read model store must be updated to reflect changes to the write model store, and it can be difficult to detect when a user has issued a request based on stale read data.

Conclusion

This pattern recommended only Scenarios where performance of data reads must be fine-tuned separately from performance of data writes, especially when the number of reads is much greater than the number of writes. In this scenario, you can scale out the read model, but run the write model on just a few instances. A small number of write model instances also helps to minimize the occurrence of merge conflicts.

Get Sathishkumar’s stories in your inbox

Join Medium for free to get updates from this writer.

If you enjoy reading my article, bye me a coffee.

https://ko-fi.com/sathishkumarpalanisamy

Keep Learning,

--

--

Sathishkumar
Sathishkumar

Written by Sathishkumar

Technical Lead, We will do design and develop a project from scratch. We do Python, Java, PHP development.