欢迎使用 FlowZap,快速、清晰、掌控的绘图应用。

CQRS 读写分离架构

Architecture

CQRS 读写分离架构图,展示专用的命令和查询路径,PostgreSQL 用于写入,Redis 或 Elasticsearch 用于优化读取,以及带有延迟监控的事件驱动同步层。该模板演示了 CQRS 的核心原则——分离读写模型以独立扩展和优化每条路径。适合读写比例不对称且查询性能至关重要的应用。

完整 FlowZap 代码

Client { # Client Application
n1: circle label:"User Action"
n2: diamond label:"Read or Write?"
n3: rectangle label:"Display Query Result"
n4: rectangle label:"Confirm Write Success"
n5: circle label:"End"
n1.handle(right) -> n2.handle(left)
n2.handle(right) -> ReadPath.n10.handle(left) [label="Query"]
n2.handle(bottom) -> WritePath.n6.handle(top) [label="Command"]
n3.handle(right) -> n5.handle(left)
n4.handle(right) -> n5.handle(left)
}
WritePath { # Write Path (Command)
n6: rectangle label:"Command Validator"
n7: rectangle label:"Domain Aggregate"
n8: rectangle label:"Write Database (PostgreSQL)"
n9: rectangle label:"Publish Change Event"
n6.handle(right) -> n7.handle(left) [label="Validated"]
n7.handle(right) -> n8.handle(left) [label="Persist"]
n8.handle(right) -> n9.handle(left) [label="Committed"]
n9.handle(bottom) -> Sync.n14.handle(top) [label="DomainEvent"]
n8.handle(top) -> Client.n4.handle(bottom) [label="ACK"]
}
ReadPath { # Read Path (Query)
n10: rectangle label:"Query Router"
n11: rectangle label:"Read Database (Redis/Elastic)"
n12: rectangle label:"Transform to DTO"
n13: rectangle label:"Return Response"
n10.handle(right) -> n11.handle(left) [label="Optimized Query"]
n11.handle(right) -> n12.handle(left) [label="Denormalized"]
n12.handle(right) -> n13.handle(left)
n13.handle(top) -> Client.n3.handle(bottom) [label="JSON"]
}
Sync { # Synchronization Layer
n14: rectangle label:"Event Consumer"
n15: rectangle label:"Transform for Read Model"
n16: rectangle label:"Update Read Database"
n17: diamond label:"Lag Acceptable?"
n18: rectangle label:"Alert on Lag"
n14.handle(right) -> n15.handle(left) [label="Consume"]
n15.handle(right) -> n16.handle(left) [label="Upsert"]
n16.handle(right) -> n17.handle(left)
n17.handle(top) -> ReadPath.n11.handle(bottom) [label="Yes"]
n17.handle(bottom) -> n18.handle(top) [label="No"]
}

为什么需要这个工作流?

Applications with 100:1 read-to-write ratios waste resources when the same database handles both workloads. CQRS read-write separation uses optimized stores for each path—a normalized relational database for writes and a denormalized cache or search index for reads—enabling each to scale independently.

工作原理

  1. Step 1: Write requests go through the command validator and domain aggregate.
  2. Step 2: Validated changes are persisted to PostgreSQL (write-optimized, normalized).
  3. Step 3: A change event is published to the synchronization layer.
  4. Step 4: The event consumer transforms data and updates the read database (Redis or Elasticsearch).
  5. Step 5: Read requests are served directly from the optimized read store with sub-millisecond latency.
  6. Step 6: Lag monitoring alerts if the read model falls too far behind the write model.

替代方案

Read replicas provide simpler read scaling but limit query optimization. Materialized views in PostgreSQL work for moderate read loads. This template shows the full CQRS separation with independent read/write stores.

Key Facts

Template NameCQRS 读写分离架构
CategoryArchitecture
Steps6 workflow steps
FormatFlowZap Code (.fz file)

相关模板

事件驱动 CQRS 与事件存储架构

Architecture

CQRS 架构图,结合独立的命令和查询 API 与事件总线进行异步读模型同步,包括从领域事件构建反规范化视图的投影器。该模板演示了完整的 CQRS+ES 技术栈,写入通过领域验证,读取从优化的物化视图提供服务。适合读写工作负载具有根本不同扩展需求的高吞吐量系统。

CQRS 物化视图架构

Architecture

CQRS 物化视图架构图,多个投影器构建特定用途的读模型:订单摘要、客户仪表板、分析立方体和搜索索引,全部由单一事件流提供。该模板展示如何从相同的写入事件创建多个优化的查询视图,每个视图针对特定用例定制,读取延迟低于毫秒级。适合需要从单一数据源进行多样化查询模式的系统。

CQRS 基于任务的 UI 架构

Architecture

CQRS 基于任务的 UI 架构图,前端将用户意图捕获为显式命令,异步提交并进行乐观更新,当读模型同步时通过 WebSocket 接收实时确认。该模板模拟用意图驱动命令替代 CRUD 表单的现代 UI 模式,实现具有最终一致性的响应式用户体验。推荐给在 CQRS 后端上构建响应式前端的团队。

速率限制器架构

Architecture

速率限制器架构图,实现令牌桶算法,使用 Redis 支持的分布式计数器、滑动窗口日志、API 密钥识别、速率限制头和多节点同步以实现一致的执行。该模板展示如何保护 API 免受滥用并确保客户端间的公平使用,包括适当的 HTTP 429 响应和 Retry-After 头。对于构建生产级速率限制基础设施的 API 平台团队至关重要。

微服务 API 网关架构

Architecture

微服务 API 网关架构图,展示请求路由、JWT 身份验证、速率限制、服务发现以及跨分布式后端服务的响应聚合。该模板模拟微服务生态系统中所有客户端流量的入口点,在请求到达内部服务之前执行安全策略。适合设计具有集中式横切关注点的可扩展 API 基础设施的平台工程师。

微服务服务网格架构

Architecture

服务网格架构图,展示 Istio 或 Linkerd 边车代理处理 mTLS 加密、流量策略、熔断器和跨微服务的分布式追踪。该模板可视化服务网格如何将网络关注点从应用代码中抽象出来,实现服务间的零信任通信。对于采用服务网格基础设施以提升可观测性和安全性的团队至关重要。

返回所有模板