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

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

Architecture

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

完整 FlowZap 代码

WriteAPI { # Write API (Command)
n1: circle label:"POST/PUT Request"
n2: rectangle label:"Command Handler"
n3: rectangle label:"Validate Business Rules"
n4: rectangle label:"Persist to Write DB"
n5: rectangle label:"Publish Domain Event"
n1.handle(right) -> n2.handle(left)
n2.handle(right) -> n3.handle(left)
n3.handle(right) -> n4.handle(left) [label="Valid"]
n4.handle(right) -> n5.handle(left) [label="Committed"]
n5.handle(bottom) -> EventBus.n6.handle(top) [label="OrderPlaced"]
}
EventBus { # Event Bus (Kafka)
n6: rectangle label:"Receive Domain Event"
n7: rectangle label:"Route to Topic"
n8: rectangle label:"Guarantee Delivery"
n9: rectangle label:"Fan-Out to Consumers"
n6.handle(right) -> n7.handle(left)
n7.handle(right) -> n8.handle(left) [label="At-Least-Once"]
n8.handle(right) -> n9.handle(left)
n9.handle(bottom) -> ReadSide.n10.handle(top) [label="Consume"]
n9.handle(bottom) -> Projector.n14.handle(top) [label="Consume"]
}
ReadSide { # Read API (Query)
n10: rectangle label:"Query Handler"
n11: rectangle label:"Read from Read DB"
n12: rectangle label:"Transform to DTO"
n13: circle label:"GET Response"
n10.handle(right) -> n11.handle(left)
n11.handle(right) -> n12.handle(left) [label="Denormalized"]
n12.handle(right) -> n13.handle(left)
}
Projector { # Read Model Projector
n14: rectangle label:"Event Handler"
n15: rectangle label:"Update Read Model"
n16: rectangle label:"Rebuild Materialized View"
n17: diamond label:"Consistency Check"
n18: rectangle label:"Mark Projection Current"
n14.handle(right) -> n15.handle(left) [label="Transform"]
n15.handle(right) -> n16.handle(left) [label="Upsert"]
n16.handle(right) -> n17.handle(left)
n17.handle(right) -> n18.handle(left) [label="Consistent"]
n18.handle(top) -> ReadSide.n11.handle(bottom) [label="Updated"]
}

为什么需要这个工作流?

Systems with high read-to-write ratios suffer when the same data model serves both commands and queries. CQRS with an event store separates the write path (optimized for consistency) from the read path (optimized for speed), allowing each to scale independently while domain events keep them synchronized.

工作原理

  1. Step 1: Write requests go through a command handler that validates business rules.
  2. Step 2: Valid commands are persisted to the write database and a domain event is published.
  3. Step 3: The event bus guarantees at-least-once delivery to all subscribed projectors.
  4. Step 4: Projectors transform domain events into denormalized read models optimized for specific queries.
  5. Step 5: The read API serves queries directly from the optimized read database.
  6. Step 6: Consistency checks monitor lag between write and read models.

替代方案

A single database with read replicas is simpler but limits query optimization. GraphQL can reduce over-fetching without full CQRS. This template shows the complete CQRS+ES architecture for teams that need independent read/write scaling.

Key Facts

Template Name事件驱动 CQRS 与事件存储架构
CategoryArchitecture
Steps6 workflow steps
FormatFlowZap Code (.fz file)

相关模板

CQRS 物化视图架构

Architecture

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

CQRS 工作流

patterns

CQRS(命令查询职责分离)模式,包含独立的命令与查询路径、领域事件发布、读模型同步以及 DTO 转换。

微服务每服务独立数据库架构

Architecture

每服务独立数据库架构图,每个微服务拥有其专用数据存储,通过 Kafka 进行事件驱动同步以实现跨服务数据一致性。该模板展示了微服务数据隔离的核心原则,展示 PostgreSQL 和 MongoDB 如何在多语言持久化策略中共存。对于在保持最终一致性的同时强制服务自治的架构师至关重要。

事件驱动事件溯源架构

Architecture

事件溯源架构图,所有状态变更存储为不可变的领域事件序列,读取投影从事件流构建,快照优化用于快速聚合加载。该模板展示事件溯源如何通过保留每次状态转换的完整历史来消除数据丢失。对于需要完整审计跟踪、时间查询和事件重放能力的系统至关重要。

CQRS 读写分离架构

Architecture

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

CQRS 基于任务的 UI 架构

Architecture

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

返回所有模板