Loading...
Loading...
Comprehensive guidance for CQRS Architecture — an independent CQRS skill covering L1/L2/L3 implementation levels, Event Sourcing, idempotency design, the domain event lifecycle, and architecture-specific CQRS integration patterns for Layered/Onion/Hexagonal/Clean/COLA. Use when the user asks about CQRS, read-write separation, Event Sourcing, event sourcing, Command Bus, Query Model, or domain events.
npx skill4agent add full-stack-skills/ddd-skills ddd-cqrs-architectureStep 1: 判断是否需要 CQRS → 确认读写模式是否显著分化
Step 2: 选择 CQRS 级别 → L1/L2/L3
Step 3: 设计命令模型 → Command → Handler → 领域事件
Step 4: 设计查询模型 → Query → Handler → DTO
Step 5: 实现事件同步(L2/L3)→ Outbox → 发布器 → 投影
Step 6: 按架构集成 → 根据 Layered/Onion/Hexagonal/Clean/COLACRUD 够用(读=写) → 单模型,无需 CQRS
↓
L1 模型分离 → CommandService / QueryService 分离,共享 DB
↓
L2 数据库分离 → Command DB + Query DB,事件同步
↓
L3 Event Sourcing → EventStore + Projection| Scenario | Alternative |
|---|---|
| Simple CRUD where reads and writes are identical | Use a single model without CQRS |
| Prototype or one-off project | Skip CQRS |
| The team is unfamiliar with event-driven architecture | First use |
| Extremely high strong-consistency requirements | Evaluate the cost of distributed transactions |
| Dimension | Command Side (Write) | Query Side (Read) |
|---|---|---|
| Responsibility | Handle state changes and enforce business rules | Return data views without business logic |
| Model | Command Model (command object + aggregate root) | Query Model (DTO + materialized view) |
| Storage | Write DB (3NF, ACID) | Read DB (denormalized, optimized for queries) |
| Consistency | Strong consistency (within an aggregate) | Eventual consistency (across aggregates or services) |
| Output | Domain events | DTO / View Model |
examples/06-order-l1-model-separation.mdexamples/07-order-l2-db-separation.mdexamples/08-order-l3-event-sourcing.md领域行为 → 构建 DomainEvent → 持久化 → EventBus 发布
→ 本地处理器 (同步)
→ MQ 外发 (异步, 跨服务)references/02-cqrs-events.md| Strategy | Overhead | Reliability | Scenario |
|---|---|---|---|
| Event deduplication table | Medium | ★★★ | Mission-critical financial events |
| State-machine guard | Low | ★★★ | State-driven events |
| Redis + TTL | Low | ★★☆ | Non-critical notifications |
| Business idempotency | Low | ★★★ | Simple operations |
references/07-event-governance.md| Architecture | Integration Point | Directory Structure |
|---|---|---|
| Layered | Separate Command/Query Services in the Application layer | |
| Onion | Command/Query UseCase interfaces in the Core layer | |
| Hexagonal | Command Port + Query Port in the Port layer | |
| Clean | Command Interactor + Query Interactor in the UseCase layer | |
| COLA | Command + Query submodules in the App layer | |
examples/04-multi-architecture-integration.md| Question | Answer |
|---|---|
| Does CQRS always introduce eventual consistency? | No. L1 does not require it, while L2/L3 do |
| Is Event Sourcing coupled to CQRS? | No. They can be used independently |
| When is the Outbox Pattern needed? | When reliable event publishing is required, to avoid the dual-write problem |
| What is the relationship between CQRS and microservices? | They are orthogonal and can be deployed within a monolith or across microservices |
| How complex can the query model become? | It can aggregate data across aggregates or services into denormalized materialized views |
| Rule | Description |
|---|---|
| The Command side must validate authorization | Authorization must be validated before executing a command |
| Event payloads must not contain sensitive data | Passwords, PII, and similar data should not appear in events |
| The Query side must not modify state | Query Handler/Service must not perform write operations |
| Write business data and the Outbox in the same transaction | Avoids the dual-write problem |
| Strong consistency within an aggregate; eventual consistency across aggregates | A single transaction should modify the state of at most one aggregate |
ddd-event-stormingCQRSread-write separationEvent Sourcingevent sourcingCommand BusQuery ModelCommand Modeldomain eventDomain Eventeventual consistencyEventual ConsistencyOutbox PatternTransactional OutboxMaterialized ViewProjectionEvent StoreIdempotencyidempotencyL1/L2/L3Clean Architecture CQRSHexagonal CQRSCOLA CQRSreferences/architecture/01-clean-ddd-hexagonal-cqrs.mdreferences/02-cqrs-events.mdreferences/03-cqrs-mindmap.mdreferences/04-ddd4j-cqrs-mindmap.mdreferences/07-event-governance.mdreferences/domain-events/01-domain-events-deep.mdreferences/06-domain-vs-integration-events.mdreferences/08-partme-06-domain-events.mdexamples/06-order-l1-model-separation.mdexamples/07-order-l2-db-separation.mdexamples/08-order-l3-event-sourcing.mdexamples/04-multi-architecture-integration.mdexamples/03-inventory-cqrs.md