Skip to content

Commit faac96b

Browse files
authored
docs: Explanation for CQRS (iluwatar#2896)
* refactor * fix run app * remove apptest
1 parent 8f777cd commit faac96b

File tree

2 files changed

+120
-58
lines changed

2 files changed

+120
-58
lines changed

cqrs/README.md

+71-7
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,89 @@ title: CQRS
33
category: Architectural
44
language: en
55
tag:
6-
- Performance
7-
- Cloud distributed
6+
- Event-driven
7+
- Performance
8+
- Scalability
89
---
910

1011
## Intent
11-
CQRS Command Query Responsibility Segregation - Separate the query side from the command side.
12+
13+
CQRS aims to segregate the operations that modify the state of an application (commands) from the operations that read the state (queries). This separation allows for more flexible and optimized designs, especially in complex systems.
14+
15+
## Explanation
16+
17+
Real world example
18+
19+
> Imagine a modern library where the tasks of borrowing and returning books (commands) are handled at the front desk, while the task of searching for books and reading them (queries) happens in the reading area. The front desk optimizes for transaction efficiency and record-keeping, ensuring books are properly checked in and out. Meanwhile, the reading area is optimized for comfort and accessibility, making it easy for readers to find and engage with the books. This separation improves the library's overall efficiency and user experience, much like the CQRS pattern enhances a software system's performance and maintainability.
20+
21+
In plain words
22+
23+
> The CQRS design pattern separates the actions of modifying data (commands) from the actions of retrieving data (queries) to enhance performance, scalability, and maintainability in software systems.
24+
25+
**Programmatic Example**
26+
27+
One way to implement the Command Query Responsibility Segregation (CQRS) pattern is to separate the read and write operations into different services.
28+
29+
1. Command Service: The `CommandServiceImpl` class is used for write operations. It provides methods to create authors and books, and to add books to authors. Here's a snippet of how it's used:
30+
31+
```java
32+
var commands=new CommandServiceImpl();
33+
commands.authorCreated(AppConstants.E_EVANS,"Eric Evans","[email protected]");
34+
commands.bookAddedToAuthor("Domain-Driven Design",60.08,AppConstants.E_EVANS);
35+
```
36+
37+
2. Query Service: The `QueryServiceImpl` class is used for read operations. It provides methods to get author and book details. Here's a snippet of how it's used:
38+
39+
```java
40+
var queries=new QueryServiceImpl();
41+
var evans=queries.getAuthorByUsername(AppConstants.E_EVANS);
42+
var dddBook=queries.getBook("Domain-Driven Design");
43+
```
44+
45+
This separation of concerns allows for flexibility in how the application handles data access and manipulation, and is a key aspect of the CQRS pattern.
1246

1347
## Class diagram
48+
1449
![alt text](./etc/cqrs.png "CQRS")
1550

1651
## Applicability
17-
Use the CQRS pattern when
1852

19-
* You want to scale the queries and commands independently.
20-
* You want to use different data models for queries and commands. Useful when dealing with complex domains.
21-
* You want to use architectures like event sourcing or task based UI.
53+
* Systems requiring distinct models for read and write operations for scalability and maintainability.
54+
* Complex domain models where the task of updating objects differs significantly from the task of reading object data.
55+
* Scenarios where performance optimization for read operations is crucial, and the system can benefit from different data models or databases for reads and writes.
56+
57+
## Known Uses
58+
59+
* Distributed Systems and Microservices Architecture, where different services manage read and write responsibilities.
60+
* Event-Sourced Systems, where changes to the application state are stored as a sequence of events.
61+
* High-Performance Web Applications, segregating read and write databases to optimize load handling.
62+
63+
## Consequences
64+
65+
Benefits:
66+
67+
* Scalability: By separating read and write models, each can be scaled independently according to their specific demands.
68+
* Optimization: Allows for the optimization of read models for query efficiency and write models for transactional integrity.
69+
* Maintainability: Reduces complexity by separating the concerns, leading to cleaner, more maintainable code.
70+
* Flexibility: Offers the flexibility to choose different technologies for the read and write sides according to their requirements.
71+
72+
Trade-Offs:
73+
74+
* Complexity: Introduces complexity due to synchronization between read and write models, especially in consistency maintenance.
75+
* Overhead: Might be an overkill for simple systems where the benefits do not outweigh the additional complexity.
76+
* Learning Curve: Requires a deeper understanding and careful design to implement effectively, increasing the initial learning curve.
77+
78+
## Related Patterns
79+
80+
* [Event Sourcing](https://java-design-patterns.com/patterns/event-sourcing/): Often used in conjunction with CQRS, where changes to the application state are stored as a sequence of events.
81+
* Domain-Driven Design (DDD): CQRS fits well within the DDD context, providing clear boundaries and separation of concerns.
82+
* [Repository](https://java-design-patterns.com/patterns/repository/): Can be used to abstract the data layer, providing a more seamless integration between the command and query sides.
2283

2384
## Credits
2485

86+
* [Patterns, Principles, and Practices of Domain-Driven Design](https://amzn.to/3vNV4Hm)
87+
* [Implementing Domain-Driven Design](https://amzn.to/3TJN2HH)
88+
* [Microsoft .NET: Architecting Applications for the Enterprise](https://amzn.to/4aktRes)
2589
* [Greg Young - CQRS, Task Based UIs, Event Sourcing agh!](http://codebetter.com/gregyoung/2010/02/16/cqrs-task-based-uis-event-sourcing-agh/)
2690
* [Martin Fowler - CQRS](https://martinfowler.com/bliki/CQRS.html)
2791
* [Oliver Wolf - CQRS for Great Good](https://www.youtube.com/watch?v=Ge53swja9Dw)

cqrs/pom.xml

+49-51
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,53 @@
2626
2727
-->
2828
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29-
<modelVersion>4.0.0</modelVersion>
30-
<parent>
31-
<groupId>com.iluwatar</groupId>
32-
<artifactId>java-design-patterns</artifactId>
33-
<version>1.26.0-SNAPSHOT</version>
34-
</parent>
35-
<artifactId>cqrs</artifactId>
36-
<dependencies>
37-
<dependency>
38-
<groupId>org.junit.jupiter</groupId>
39-
<artifactId>junit-jupiter-engine</artifactId>
40-
<scope>test</scope>
41-
</dependency>
42-
<dependency>
43-
<groupId>com.h2database</groupId>
44-
<artifactId>h2</artifactId>
45-
</dependency>
46-
<dependency>
47-
<groupId>org.hibernate</groupId>
48-
<artifactId>hibernate-core</artifactId>
49-
</dependency>
50-
<dependency>
51-
<groupId>org.glassfish.jaxb</groupId>
52-
<artifactId>jaxb-runtime</artifactId>
53-
<scope>test</scope>
54-
</dependency>
55-
<dependency>
56-
<groupId>javax.xml.bind</groupId>
57-
<artifactId>jaxb-api</artifactId>
58-
<scope>test</scope>
59-
</dependency>
60-
</dependencies>
61-
<build>
62-
<plugins>
63-
<plugin>
64-
<groupId>org.apache.maven.plugins</groupId>
65-
<artifactId>maven-assembly-plugin</artifactId>
66-
<executions>
67-
<execution>
68-
<configuration>
69-
<archive>
70-
<manifest>
71-
<mainClass>com.iluwatar.cqrs.app.App</mainClass>
72-
</manifest>
73-
</archive>
74-
</configuration>
75-
</execution>
76-
</executions>
77-
</plugin>
78-
</plugins>
79-
</build>
29+
<modelVersion>4.0.0</modelVersion>
30+
<parent>
31+
<groupId>com.iluwatar</groupId>
32+
<artifactId>java-design-patterns</artifactId>
33+
<version>1.26.0-SNAPSHOT</version>
34+
</parent>
35+
<artifactId>cqrs</artifactId>
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-engine</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.h2database</groupId>
44+
<artifactId>h2</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.hibernate</groupId>
48+
<artifactId>hibernate-core</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.glassfish.jaxb</groupId>
52+
<artifactId>jaxb-runtime</artifactId>
53+
</dependency>
54+
<dependency>
55+
<groupId>javax.xml.bind</groupId>
56+
<artifactId>jaxb-api</artifactId>
57+
</dependency>
58+
</dependencies>
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-assembly-plugin</artifactId>
64+
<executions>
65+
<execution>
66+
<configuration>
67+
<archive>
68+
<manifest>
69+
<mainClass>com.iluwatar.cqrs.app.App</mainClass>
70+
</manifest>
71+
</archive>
72+
</configuration>
73+
</execution>
74+
</executions>
75+
</plugin>
76+
</plugins>
77+
</build>
8078
</project>

0 commit comments

Comments
 (0)