Microservice CQRS Pattern Explained

Anish Antony
7 min readDec 11, 2021
Photo by XPS on Unsplash

CQRS stands for Command and Query Responsibility Segregation. Basically, this pattern separates read and update operations for a database. Normally, in monolithic applications, most of the time we have 1 database and this database should respond to both query execution and update operations. That means a database is both working for complex join queries, and also perform CRUD operations. But if the application goes more complex this query and crud operations will be also is going to be unmanageable situations.

In the example of a reading database, if your application required some query that needs to join more than 10 tables, this will lock the database due to latency of query computation. When performing crud operations we would need to make complex validations and process long business logic, so this will cause to lock database operations. So reading and writing databases have different approaches that we can define the different strategies to handle the operations. In order to that CQRS offers to use “separation of concernsprinciples and separate reading from database and the writing to the database with the help of two databases. In this way, we can use different databases for reading and writing database types like using no-SQL for reading and using relational databases for crud operations.

Another consideration is we should understand our application use case behaviours, if our application is mostly reading use cases and not writing so much, we can say our application is a read-incentive application. So you should design the architecture in database read friendly.

The CQRS pattern separates reads and writes into different databases. The Commands performs updating the data and Queries performs reading the data.

Commands are the actions with task-based operations like “add item into shopping cart” or “checkout order”. The commands can handle with the help of message broker systems that process commands in async way.

Queries never modify the database. Queries always return the JSON data with DTO objects. In this way, we can isolate the Commands and Queries.

Anish Antony

Fullstack Developer | Blogger | Experience on Java, Python, React, Angular, Golang | http://www.behindjava.com


See more recommendations