CQRS stands for the Command and Query Responsibility segregation pattern. CQRS is one of the important pattern when querying between microservices. 

We can use CQRS design pattern in order to avoid complex queries to get rid of the inefficient joins. Basically, this pattern separates read and write operations with separating databases. 

The idea behind is to divide the application operation into two separate categories.

  • Commands - changing the state of data into application. 
  • Queries - handling complex join operations and returning a result, and don't change the state of data into application

 

In large scale microservice architectures, it needs to manage high volume data requirements for this kind of use cases. In Monolithic approaches like single database for services can cause bottlenecks for complex and large scale microservice architectures. We can use both CQRS and event sourcing patterns to improve application performance. 

CQRS offers two separate read and write data that provide to maximize query performance and scalability.

 

 

 

How CQRS better than monolithic approach?

In old monolithic applications, most of the time we have one databases, and this database should respond to both query and update operations. i.e. a single database is both working for complex join operations and also perform Crud operations.

But if the application goes more complex, this query and Crud operations will be also going to be unmanageable situation.

  • While reading database, if your application required some query that needs to join more than one table, this will lock the database due to latency of query computation.
  • While writing database when performing the Crud operations, we would need to make complex validations and process long business logic. So this will cause the lock database operations.
  • So reading and writing database has different approaches that we can define different strategies to handle that operation.
     

in CQRS approach, SQL is offered to use separation of concerns, principles and separate reading database and writing database with two databases. 

  • NoSQL database for reading and using relational database for Crud operations.  Mostly read database uses NoSQL databases with Denormalized data, which generates from the right databases,
  • Another database uses relational databases with fully normalized and supports strong data consistency.

 

When CQRS Required?

This is often seen in a more complex systems, particularly when scaling is a concern.

 

CQRS Challenges 

  • There is a synchronization challenge happen when we are separate databases.
  • While a physical implementation can offer performance advantages and better scaling capabilities, it also introduces complexity in terms of maintaining data consistency and synchronization between the read and write operations.

Related Question