$npx -y skills add wshobson/agents --skill cqrs-implementationImplement Command Query Responsibility Segregation for scalable architectures. Use when separating read and write models, optimizing query performance, or building event-sourced systems.
| 1 | # CQRS Implementation |
| 2 | |
| 3 | Comprehensive guide to implementing CQRS (Command Query Responsibility Segregation) patterns. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Separating read and write concerns |
| 8 | - Scaling reads independently from writes |
| 9 | - Building event-sourced systems |
| 10 | - Optimizing complex query scenarios |
| 11 | - Different read/write data models needed |
| 12 | - High-performance reporting requirements |
| 13 | |
| 14 | ## Core Concepts |
| 15 | |
| 16 | ### 1. CQRS Architecture |
| 17 | |
| 18 | ``` |
| 19 | ┌─────────────┐ |
| 20 | │ Client │ |
| 21 | └──────┬──────┘ |
| 22 | │ |
| 23 | ┌────────────┴────────────┐ |
| 24 | │ │ |
| 25 | ▼ ▼ |
| 26 | ┌─────────────┐ ┌─────────────┐ |
| 27 | │ Commands │ │ Queries │ |
| 28 | │ API │ │ API │ |
| 29 | └──────┬──────┘ └──────┬──────┘ |
| 30 | │ │ |
| 31 | ▼ ▼ |
| 32 | ┌─────────────┐ ┌─────────────┐ |
| 33 | │ Command │ │ Query │ |
| 34 | │ Handlers │ │ Handlers │ |
| 35 | └──────┬──────┘ └──────┬──────┘ |
| 36 | │ │ |
| 37 | ▼ ▼ |
| 38 | ┌─────────────┐ ┌─────────────┐ |
| 39 | │ Write │─────────►│ Read │ |
| 40 | │ Model │ Events │ Model │ |
| 41 | └─────────────┘ └─────────────┘ |
| 42 | ``` |
| 43 | |
| 44 | ### 2. Key Components |
| 45 | |
| 46 | | Component | Responsibility | |
| 47 | | ------------------- | ------------------------------- | |
| 48 | | **Command** | Intent to change state | |
| 49 | | **Command Handler** | Validates and executes commands | |
| 50 | | **Event** | Record of state change | |
| 51 | | **Query** | Request for data | |
| 52 | | **Query Handler** | Retrieves data from read model | |
| 53 | | **Projector** | Updates read model from events | |
| 54 | |
| 55 | ## Templates and detailed worked examples |
| 56 | |
| 57 | Full template library and detailed worked examples live in `references/details.md`. Read that file when you need the concrete templates. |
| 58 | |
| 59 | ## Best Practices |
| 60 | |
| 61 | ### Do's |
| 62 | |
| 63 | - **Separate command and query models** - Different needs |
| 64 | - **Use eventual consistency** - Accept propagation delay |
| 65 | - **Validate in command handlers** - Before state change |
| 66 | - **Denormalize read models** - Optimize for queries |
| 67 | - **Version your events** - For schema evolution |
| 68 | |
| 69 | ### Don'ts |
| 70 | |
| 71 | - **Don't query in commands** - Use only for writes |
| 72 | - **Don't couple read/write schemas** - Independent evolution |
| 73 | - **Don't over-engineer** - Start simple |
| 74 | - **Don't ignore consistency SLAs** - Define acceptable lag |