CQRS(命令查询职责分离)
将读取与写入操作拆分到不同的数据模型。
结构与工作原理
按步骤查看箭头所示的流程。
分离读取与写入职责,不强制使用不同数据库或事件溯源。
预期收益
分别优化读写模型有助于改善性能、可扩展性和安全性。
权衡
数据存储间会出现最终一致性和读取延迟。
不适合写入后必须立即读取同一数据的场景,例如银行余额、库存及安全关键控制。
运维与应用指南
综合说明,与有原文摘录支持的条目分开。实际应用前请先验证。
何时使用
适合读写负载差异大,并有高吞吐、低延迟、复杂领域逻辑或审计合规要求的系统,例如证券交易以及将订单查询与库存查询分开的电商。
应用场景
一家中型网店每秒处理数千次商品查询,订单写入相对较少。要求下单后尽快显示库存与订单状态,但可容忍几秒延迟。写端验证订单和库存命令,保存到写库并发布事件;读端更新独立读取模型,用于商品列表和订单查询。支付后的完成页面使用读己之写优先展示刚保存的内容,并监控读模型延迟。
部署原则
- 写模型与写库作为命令验证和事务的权威来源
- 读模型与读库独立并仅供查询,必要时使用副本或缓存
- 通过消息代理把领域事件从写端传给读端
- 独立部署投影进程或索引器更新读模型
- 在 Kubernetes 中分开部署读写服务、Redis 主节点与副本及 Helm Chart。
本地部署
将容器化命令与查询服务、消息代理和读写存储部署到内部 Kubernetes 或虚拟机。评估网络与存储隔离、安全、隐私、总体拥有成本及备份恢复。监控读取副本延迟与代理积压,并统一部署和升级流程。
云环境
借助托管 Kubernetes、数据库、缓存及消息代理分别扩展读写负载;部署前在本地和预发布环境测试容器与编排。采用自动扩缩、多可用区、读取复制与可观测性,并添加成本标签。
Kubernetes
命令与查询微服务、投影工作进程、读写数据存储和消息代理使用独立的 Deployment 或 StatefulSet 与 Service,并用 Helm 或 Kustomize 管理。Redis 主从、Kafka 或 Pulsar 等通过 StatefulSet 或 Operator 运行;验证 HPA、PDB、网络策略、资源请求与限制、滚动升级及投影重放幂等性。
故障应对
读模型延迟或不一致会让用户看不到刚保存的数据并重复提交;其他典型故障包括消息积压、投影中断以及重新部署或重放时重复应用导致的数据损坏。跟踪读取模型的偏移量、延迟和缺口,用读己之写或缓存缓解。投影应具备幂等性,并把同一日志重复重放测试加入 CI。故障时先消化积压,再重启投影,必要时重建读模型。
14 个维度的证据
反映调研时的证据收录情况,请另行判断来源是否准确、是否仍然有效。
定义与概览有证据
将读取与写入操作拆分到不同的数据模型。
Segregate the read and write operations for a data store into separate data models.解决的问题有证据
解决单一模型难以同时优化读写各自性能与扩展需求的问题。
As applications grow, it can become increasingly difficult to optimize read and write operations on a single data model.关键组件与资源有证据
可由 Web 应用或微服务以及独立的读写数据存储构成。
It has basically three components: * A web app written in NodeJS (which can be thought of a microservice)数据与资产放置原则有证据
将写入的权威存储与读取副本或读取模型分开部署。
A 'write' datastore (implemented using Redis master instance) * A 'read' datastore (implemented using Redis replica instances )代表性使用场景有证据
适合性能或扩展要求高、领域复杂或审计与合规要求严格的系统,例如证券交易平台。
CQRS is great for a stock trading platform where performance, scalability, and regulatory compliance are paramount.应用场景示例有证据
用户提交表单后,由于读取模型延迟而看到旧数据并再次提交。
the command succeeds, the read model has not caught up, the user's next page load shows old data不适用情形与注意事项有证据
不适合写入后必须立即读取同一数据的场景,例如银行余额、库存及安全关键控制。
banking balances, inventory counts at checkout, safety-critical controls- don't tolerate eventual consistency well.本地部署:开发、发布与运维有证据
可将容器化微服务部署到本地 Kubernetes。
You can take a containerized microservices-based application and deploy it into an on-premises Kubernetes cluster.云环境:开发、发布与运维有证据
容器化后,可在 OCI Kubernetes Engine 等托管 Kubernetes 上部署与测试。
After you containerize your microservices, you can deploy them to OCI Kubernetes Engine.Kubernetes:开发、发布与运维有证据
可用 Node.js、Redis、Helm 等在 Kubernetes 中部署及运维 CQRS 组件。
implementation of CQRS (Command Query Responsibility Segregation) architectural pattern on Kubernetes using NodeJS, Redis and Helm.故障应对有证据
读取模型会在写入后更新,因此查询可能短暂无法反映最新变更。
the read models are updated after the write, so for a short interval a query may not reflect a change优势有证据
分别优化读写模型有助于改善性能、可扩展性和安全性。
This approach allows you to optimize each model independently and can improve the performance, scalability, and security of an application.不足与权衡有证据
数据存储间会出现最终一致性和读取延迟。
The CQRS pattern typically results in eventual consistency between the data stores.代表性技术与工具有证据
典型技术有 Kubernetes、Node.js、Redis 和 Helm。
on Kubernetes using NodeJS, Redis and Helm.更多参考资料 (21)
- CQRS Pattern - Azure Architecture Center | Microsoft Learn ↗
- bliki: CQRS ↗
- Untitled result ↗
- An introduction to command query responsibility segregation ↗
- Command Query Responsibility Segregation (CQRS) ↗
- CQRS Pattern Explained: When To Use It And When Not To ↗
- CQRS pitfalls and patterns • Particular Software ↗
- Applying simplified CQRS and DDD patterns in a microservice - .NET | Microsoft Learn ↗
- Follow these 8 best practices for cloud-native app development | TechTarget ↗
- Design a Microservices-Based Application ↗
- Building Kubernetes Native SaaS applications: iterating quickly by deploying in-cluster data planes | Pixie Labs Blog ↗
- Command-Query Responsibility Segregation (CQRS) ↗
- EZF-006. Should You Use Kubernetes?; CQRS. Command Query Responsibility Segregation; gRPC API Architecture; API Design: 51 ✔️DO's and ❌DON'Ts; Functional Requirements (FR) vs Non-functional (NFR) ↗
- code2k13/cqrs-example: An opiniated sample implementation of CQRS architectural pattern on Kubernetes using NodeJS, Redis and Helm ↗
- An illustrated guide to CQRS data patterns ↗
- CQRS Journey | Microsoft Learn ↗
- CQRS: Separating Reads from Writes Without Losing Your Mind - The HLD Handbook ↗
- CQRS and Projection Architecture | System Design Patterns ↗
- chapter_12_cqrs.asciidoc at master · cosmicpython/book ↗
- 2.20 Error handling and resilience patterns — Software Engineering Guide ↗
- System Design, Plainly — Season 3, Part 3: Event Sourcing & CQRS - DEV Community ↗