cqrs-event-sourcing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

CQRS and Event Sourcing Patterns

CQRS与事件溯源模式

Expert guidance for implementing Command Query Responsibility Segregation (CQRS) and Event Sourcing patterns to build scalable, auditable systems with complete historical tracking and optimized read/write models.
本指南为实现命令查询职责分离(CQRS)与事件溯源模式提供专业指导,助力构建具备完整历史追踪能力、读写模型优化的可扩展、可审计系统。

When to Use This Skill

适用场景

  • Building systems requiring complete audit trails and compliance
  • Implementing temporal queries ("show me the state at time T")
  • Designing high-scale applications with complex domain logic
  • Creating systems with significantly different read and write patterns
  • Building event-driven architectures with historical replay capability
  • Implementing systems requiring multiple read model projections
  • Designing applications where understanding "what happened" is critical
  • Building collaborative systems with conflict resolution needs
  • 构建需要完整审计追踪与合规性的系统
  • 实现时态查询(如“展示时间点T的系统状态”)
  • 设计具有复杂领域逻辑的高扩展性应用
  • 创建读写模式差异显著的系统
  • 构建具备历史事件重放能力的事件驱动架构
  • 实现需要多读取模型投影的系统
  • 设计需明确追踪“发生过什么”的应用
  • 构建具备冲突解决需求的协作系统

Core Principles

核心原则

1. Command Query Separation

1. 命令与查询分离

Separate operations that change state (commands) from operations that read state (queries).
Commands (Write)Queries (Read)
Express intent (CreateOrder, UpdatePrice)Return data, never change state
Can be rejected (validation failures)Can be cached and optimized
Return success/failure, not dataMultiple models for different needs
Change system stateEventually consistent with writes
将修改状态的操作(命令)与读取状态的操作(查询)完全分离。
命令(写入操作)查询(读取操作)
表达意图(如CreateOrder、UpdatePrice)返回数据,绝不修改状态
可能被拒绝(如验证失败)可被缓存与优化
返回成功/失败结果,不返回数据可针对不同需求设计多套模型
修改系统状态与写入操作最终一致

2. Events as Source of Truth

2. 事件作为唯一数据源

Store state changes as immutable events rather than current state snapshots.
Traditional: Store what IS →
UPDATE users SET email = 'new@email.com'
Event Sourcing: Store what HAPPENED →
APPEND UserEmailChanged event
Result: Complete history, temporal queries, audit trail
以不可变事件的形式存储状态变更,而非当前状态快照。
传统方式:存储当前状态 →
UPDATE users SET email = 'new@email.com'
事件溯源:存储发生的事件 →
APPEND UserEmailChanged event
优势:完整历史记录、支持时态查询、可生成审计追踪

3. Eventual Consistency

3. 最终一致性

Accept temporary inconsistency between write and read models for scalability.
为提升扩展性,接受读写模型间的临时不一致。

4. Domain-Driven Design Integration

4. 与领域驱动设计(DDD)集成

  • Aggregates enforce business invariants
  • Events represent domain facts
  • Commands express domain operations
  • Bounded contexts define consistency boundaries
  • 聚合根(Aggregates)强制执行业务不变量
  • 事件代表领域事实
  • 命令表达领域操作
  • 限界上下文(Bounded contexts)定义一致性边界

Quick Reference

快速参考

TaskLoad reference
CQRS implementation patterns
skills/cqrs-event-sourcing/references/cqrs-patterns.md
Event sourcing & snapshots
skills/cqrs-event-sourcing/references/event-sourcing.md
EventStoreDB & Axon Framework
skills/cqrs-event-sourcing/references/event-store-tech.md
Consistency patterns
skills/cqrs-event-sourcing/references/consistency-patterns.md
Best practices checklist
skills/cqrs-event-sourcing/references/best-practices.md
任务参考路径
CQRS实现模式
skills/cqrs-event-sourcing/references/cqrs-patterns.md
事件溯源与快照
skills/cqrs-event-sourcing/references/event-sourcing.md
EventStoreDB与Axon Framework
skills/cqrs-event-sourcing/references/event-store-tech.md
一致性模式
skills/cqrs-event-sourcing/references/consistency-patterns.md
最佳实践清单
skills/cqrs-event-sourcing/references/best-practices.md

Workflow

实施流程

  1. Identify if CQRS/ES is appropriate (high audit, temporal, or scale needs)
  2. Design commands expressing user intent
  3. Define events as immutable facts (past tense naming)
  4. Implement aggregates as consistency boundaries
  5. Create projections optimized for specific query needs
  6. Handle eventual consistency across bounded contexts
  1. 评估:判断CQRS/事件溯源是否适用(如高审计需求、时态查询、高扩展性场景)
  2. 设计:表达用户意图的命令
  3. 定义:以不可变事实形式存在的事件(采用过去式命名)
  4. 实现:将聚合根作为一致性边界
  5. 创建:针对特定查询需求优化的投影模型
  6. 处理:限界上下文间的最终一致性

Common Mistakes

常见误区

  • Using CQRS for simple CRUD applications (overkill)
  • Large aggregates that span multiple consistency boundaries
  • Modifying or deleting events after publication
  • Skipping command validation before aggregate processing
  • Missing idempotency in event handlers
  • No versioning strategy for event schema evolution
  • Tight coupling between aggregates (use ID references only)
  • 为简单CRUD应用使用CQRS(过度设计)
  • 聚合根跨越多一致性边界
  • 事件发布后修改或删除事件
  • 聚合根处理前跳过命令验证
  • 事件处理器未实现幂等性
  • 未制定事件 schema 演进的版本策略
  • 聚合根间紧耦合(仅使用ID引用)

Resources

参考资源

  • Books: "Implementing Domain-Driven Design" (Vernon), "Event Sourcing & CQRS" (Betts et al)
  • Sites: cqrs.wordpress.com, eventstore.com/blog, axoniq.io/resources
  • Tools: EventStoreDB, Axon Framework, Marten, Eventuous
  • Patterns: Event Sourcing, CQRS, Process Manager, Saga, Snapshot
  • 书籍:《Implementing Domain-Driven Design》(Vernon 著)、《Event Sourcing & CQRS》(Betts 等著)
  • 网站:cqrs.wordpress.com、eventstore.com/blog、axoniq.io/resources
  • 工具:EventStoreDB、Axon Framework、Marten、Eventuous
  • 模式:事件溯源、CQRS、流程管理器、Saga、快照