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 inefficient joins. CQRS stands for Command and Query Responsibility Segregation. Basically this pattern separates read and update operations for a database.
2. Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Join the session 5 minutes prior to the session start time. We start on
time and conclude on time!
Feedback
Make sure to submit a constructive feedback for all sessions as it is very
helpful for the presenter.
Silent Mode
Keep your mobile devices in silent mode, feel free to move out of session
in case you need to attend an urgent call.
Avoid Disturbance
Avoid unwanted chit chat during the session.
3. 1. What is CQRS Design Pattern
2. When to use CQRS Design Pattern
3. Standard Structure
4. CQRS Architecture
5. CQRS Command API Flow
6. Benefits & Challenges of CQRS
7. Demo
4. What is CQRS Design Pattern
CQRS stands for Common Querry Responsibility Segregation
It has emerged as a powerful architectural pattern for building complex and scalable software systems
CQRS separates the responsibility of handling commands and queries into different components.
CQRS architectural pattern mainly focuses on separating the way of reading and writing the data. It separates
the read and update operations on a datastore into two separate models:
Query
Command
5. When to use CQRS
CQRS Design Pattern is useful where the data reading / accessing performance has become
very slow due to a large number of read and write operations going on simultaneously, which can
be due to a lot of load on the database, and/or the network on which the database resides.
CQRS Design Pattern is useful when the read and write workloads have different requirements
for latency, scaling, and consistency.
CQRS pattern is useful when an eventually consistent system is acceptable for the read/access
queries.
CQRS pattern is useful in the situation when reading data performance needs to be optimized
separately from writing data performance, especially if the reading data queries are much more in
number than the writing data queries.
CQRS pattern is useful when the application is complex and the business rule of the application
change regularly.
CQRS Design pattern is useful when the system can evolve from time to time and the system
might contain multiple versions of the model.
Note: Should avoid when there is only simple crud operations
7. Basic CQRS Architecture
Command: Commands are instructions that indicate a desired change in the state of an entity.
these commands execute operations such as Insert, Update, and Delete. they do not return data,
but instead, change the application server’s state. each command is an object containing the
name of the operation along with the necessary data to perform that operation.
"A command (procedure) does something but does not return a result. "
Query: Queries are used to retrieve information from a database. Queries objects just return data
and make no modifications to it. Queries will solely comprise data retrieval methods. They are
used to read data from the database and return it to the client for display in the user interface.
The Query Handlers interpret the queries and return query values.
"A query (function or attribute) returns a result but does not change the state."
Command Handler :Is responsible for receiving, interpreting, and executing commands that
intend to change the state of the system, ensuring data consistency and enforcing business rules.
Event Handler: A component responsible for reacting to domain events that are generated as a
result of changes to the system's state, typically by updating the read models or triggering
additional processes based on the events.
Query Handler: Is responsible for receiving and processing read requests, typically queries for
data retrieval, and returning the requested data from the appropriate read model or data source,
ensuring efficient and optimized read operations in the system
10. Benefits and Challenges of CQRS
Benefits:
Improved Scalability: Each model can be optimized for a particular use case by splitting the duties of
commands and queries
Improved performance: The system can outperform a conventional strategy that employs a single model for
both reads and writes by tailoring each model to its particular use case
Maintainability: CQRS can also improve maintainability by making the system easier to understand and
modify
Challenges:
Complexity: Implementing CQRS can introduce additional complexity to your system, especially if you’re not
familiar with the pattern.
Consistency: Maintaining consistency between the read and write models can be challenging, especially in
distributed systems where data updates may not be immediately propagated
Data Synchronization: Keeping the read and write models in sync can be a non-trivial task, especially when
dealing with large volumes of data or complex data transformations
Performance Overhead: Implementing CQRS can introduce performance overhead, especially if not done
carefully.
Operational Complexity: Managing multiple databases or data stores (one for read and one for write) can
increase operational complexity.