Implementing the CQRS pattern
CQRS stands for Command Query Responsibility Segregation. We can apply CQRS in two ways:
- Dividing requests into commands and queries.
- Applying the CQRS concept to a higher level, leading to a distributed system.
In this chapter, we stick with the first one, but we will tackle the second definition in Chapter 16, Introduction to Microservices Architecture.
Goal
The goal is to divide all requests into two categories: commands and queries.
A command mutates the state of an application. For example, creating, updating, and deleting an entity are commands. Commands do not return a value.
On the other hand, a query reads the state of the application but never changes it. For example, reading an order, reading your order history, and retrieving your user profile are all queries.
By performing this division, we create a clear separation of concerns between mutator and accessor requests.