事件驱动 CQRS 与事件存储架构
Architecture
CQRS 架构图,结合独立的命令和查询 API 与事件总线进行异步读模型同步,包括从领域事件构建反规范化视图的投影器。该模板演示了完整的 CQRS+ES 技术栈,写入通过领域验证,读取从优化的物化视图提供服务。适合读写工作负载具有根本不同扩展需求的高吞吐量系统。
Architecture
CQRS 读写分离架构图,展示专用的命令和查询路径,PostgreSQL 用于写入,Redis 或 Elasticsearch 用于优化读取,以及带有延迟监控的事件驱动同步层。该模板演示了 CQRS 的核心原则——分离读写模型以独立扩展和优化每条路径。适合读写比例不对称且查询性能至关重要的应用。
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.
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.
| Template Name | CQRS 读写分离架构 |
| Category | Architecture |
| Steps | 6 workflow steps |
| Format | FlowZap Code (.fz file) |
Architecture
CQRS 架构图,结合独立的命令和查询 API 与事件总线进行异步读模型同步,包括从领域事件构建反规范化视图的投影器。该模板演示了完整的 CQRS+ES 技术栈,写入通过领域验证,读取从优化的物化视图提供服务。适合读写工作负载具有根本不同扩展需求的高吞吐量系统。
Architecture
CQRS 物化视图架构图,多个投影器构建特定用途的读模型:订单摘要、客户仪表板、分析立方体和搜索索引,全部由单一事件流提供。该模板展示如何从相同的写入事件创建多个优化的查询视图,每个视图针对特定用例定制,读取延迟低于毫秒级。适合需要从单一数据源进行多样化查询模式的系统。
Architecture
CQRS 基于任务的 UI 架构图,前端将用户意图捕获为显式命令,异步提交并进行乐观更新,当读模型同步时通过 WebSocket 接收实时确认。该模板模拟用意图驱动命令替代 CRUD 表单的现代 UI 模式,实现具有最终一致性的响应式用户体验。推荐给在 CQRS 后端上构建响应式前端的团队。
Architecture
微服务 API 网关架构图,展示请求路由、JWT 身份验证、速率限制、服务发现以及跨分布式后端服务的响应聚合。该模板模拟微服务生态系统中所有客户端流量的入口点,在请求到达内部服务之前执行安全策略。适合设计具有集中式横切关注点的可扩展 API 基础设施的平台工程师。