CQRS Its full name is Command Query Responsibility Segregation. CQRS Not a complete architecture , It's a small pattern . This model is first developed by Greg Young and Udi Dahan Put forward ,Martin Flower There is an article dedicated to this model , Microsoft also has a special tutorial introduction CQRS.
CQRS It's easy to describe , Is the separation of command and query responsibilities .
Traditionally , We will use a unified model for data addition, deletion, modification and query , And provide unified services , This is the most common way we use , Many frameworks also provide auxiliary functions to help us automatically generate CRUD Code for .
however , This also brings some problems ：
- Data is often read much more frequently than written
- While reading the data , We usually get a lot of data , Or a data list . Compared with , Writing to data usually affects only a single aggregation .
- From the user's point of view , Reading data should show higher performance than writing . For users , It's easier to accept some slowness when making data changes .
Vladimir Khorikov Defined Three types of CQRS, And not used CQRS（No CQRS） The architecture of ：
- stay No CQRS Architecture , Use the same domain model to process commands and queries . This approach does not increase code or complexity , But it will make it very difficult to optimize the read operation , It's not even possible .
- In a separate class structure , Use domain classes to handle commands , And transfer objects with data （DTO） Responsible for returning the read data , This approach will lead to some degree of repetition .Khorikov Believe in , At this stage CQRS Applications are enough for most enterprise applications , It achieves a good balance between complexity and performance .
- In a separate model , Data will be read and written in different ways API Corresponding to the model . This approach not only optimizes queries , And can take advantage of cache , It is a good solution for applications with high read load .
- Use a separate storage structure for queries for optimization , Able to cope with larger read operations . Different types of storage structures will be used for data writing and query , For example, use a relational database and a NoSQL Database of type . Synchronization of read storage structures is usually run in the background , Therefore, the reading of data will achieve final consistency . This mode is the most scalable , But it also means the highest complexity .
CQRS Often with Event Sourcing Mentioned together , Although there is no dependency between the two , They just often complement each other .
An object experiences many events from creation to death , In the past, we persisted the latest state of the object to the database after each object participated in a business action , In other words, the data in our database reflects the latest state of the object . The opposite is true of events , Instead of saving the latest state of the object , It's about saving every event that this object goes through , All the events generated by the object will be stored in the database in chronological order . It can be seen that , This way of tracing the source of events is more in line with the concept of facts , Because it completely describes all the events experienced in the whole life cycle of an object .
- CQRS Journey
- Event Sourcing Pattern
- Introduction to CQRS