cqrs-implementation
它能做什么
这份指南讲解如何把系统中的数据写入和读取任务分开处理。这有助于让系统运行得更快、结构更清晰,尤其是在大量信息被访问或修改的时候。
适用场景
- ✓当你需要让数据写入和读取以不同的方式运行时。
- ✓想让系统的读取部分运行更快,又不影响写入时。
- ✓构建会把所有变更记录为事件的系统时。
技能链接
https://github.com/wshobson/agents/blob/main/plugins/backend-development/skills/cqrs-implementation/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我怎样用 CQRS 来优化复杂报表的速度?”
每周 5 个新技能,1 分钟读完。
工作原理
它的做法是把操作分成两部分:命令(修改数据)和查询(读取数据)。两部分可以分别优化,使用不同的数据模型,并专注于性能。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
CQRS Implementation
Comprehensive guide to implementing CQRS (Command Query Responsibility Segregation) patterns.
When to Use This Skill
- Separating read and write concerns
- Scaling reads independently from writes
- Building event-sourced systems
- Optimizing complex query scenarios
- Different read/write data models needed
- High-performance reporting requirements
Core Concepts
1. CQRS Architecture
┌─────────────┐
│ Client │
└──────┬──────┘
│
┌────────────┴────────────┐
│ │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ Commands │ │ Queries │
│ API │ │ API │
└──────┬──────┘ └──────┬──────┘
│ │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ Command │ │ Query │
│ Handlers │ │ Handlers │
└──────┬──────┘ └──────┬──────┘
│ │
▼ ▼
┌─────────────┐ ┌─────────────┐
│ Write │─────────►│ Read │
│ Model │ Events │ Model │
└─────────────┘ └─────────────┘
2. Key Components
| Component | Responsibility |
|---|---|
| Command | Intent to change state |
| Command Handler | Validates and executes commands |
| Event | Record of state change |
| Query | Request for data |
| Query Handler | Retrieves data from read model |
| Projector | Updates read model from events |
Templates and detailed worked examples
Full template library and detailed worked examples live in references/details.md. Read that file when you need the concrete templates.
Best Practices
Do's
- Separate command and query models - Different needs
- Use eventual consistency - Accept propagation delay
- Validate in command handlers - Before state change
- Denormalize read models - Optimize for queries
- Version your events - For schema evolution
Don'ts
- Don't query in commands - Use only for writes
- Don't couple read/write schemas - Independent evolution
- Don't over-engineer - Start simple
- Don't ignore consistency SLAs - Define acceptable lag
常见问题
什么是 'eventual consistency'?
也就是最终一致性,意思是数据在系统各个部分之间更新可能需要一点时间,但最终会变得一致、正确。
为什么要把读模型和写模型分开?
因为它们的需求不同。写模型需要保证数据的完整性,而读模型需要快速响应查询。
我可以先从简单的实现开始吗?
可以。建议先从简单做起,只有在确实有必要时才增加复杂度。
详情
作者的原始描述
Implement Command Query Responsibility Segregation for scalable architectures. Use when separating read and write models, optimizing query performance, or building event-sourced systems.
repo: wshobson/agents
热度
- GitHub 星标
- 41k
- 排名
- #6637
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …