CQ

CQRS

Used in 126 java projects (avg ★307)

Java
Architecture
Used by 126 projects
api
webapp

Details

Language / Topic
javaJava
Category
Architecture

Rules

balanced
- Separate commands (mutations) and queries (reads) into distinct service classes or handler methods.
- Use `CommandHandler` and `QueryHandler` interfaces for type-safe dispatching.
- Use separate read and write repositories — query side can use raw SQL or projections for performance, command side uses rich domain models.
- Use Spring's `ApplicationEventPublisher` or a mediator library for command/query dispatching.
- Optimize read paths independently: use JPA projections, `@Query` with native SQL, or read replicas.
- Validate commands with Bean Validation before handling. Commands carry only validated data.
- Use separate DTOs for commands (input) and queries (output).