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

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

Architecture

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

完整 FlowZap 代码

Gateway { # API Gateway
n1: circle label:"Client Request"
n2: rectangle label:"Route to Service"
n3: rectangle label:"Aggregate Responses"
n4: circle label:"Return to Client"
n1.handle(right) -> n2.handle(left)
n2.handle(bottom) -> UserService.n5.handle(top) [label="User Data"]
n2.handle(bottom) -> OrderService.n9.handle(top) [label="Order Data"]
n3.handle(right) -> n4.handle(left)
}
UserService { # User Service
n5: rectangle label:"Handle User Request"
n6: rectangle label:"Query User DB"
n7: rectangle label:"User PostgreSQL"
n8: rectangle label:"Return User Data"
n5.handle(right) -> n6.handle(left)
n6.handle(right) -> n7.handle(left) [label="SELECT"]
n7.handle(right) -> n8.handle(left) [label="Result"]
n8.handle(top) -> Gateway.n3.handle(bottom) [label="User JSON"]
}
OrderService { # Order Service
n9: rectangle label:"Handle Order Request"
n10: rectangle label:"Query Order DB"
n11: rectangle label:"Order MongoDB"
n12: rectangle label:"Return Order Data"
n9.handle(right) -> n10.handle(left)
n10.handle(right) -> n11.handle(left) [label="find()"]
n11.handle(right) -> n12.handle(left) [label="Documents"]
n12.handle(top) -> Gateway.n3.handle(bottom) [label="Order JSON"]
}
EventBus { # Event Bus (Kafka)
n13: rectangle label:"Publish Domain Event"
n14: rectangle label:"Event Topic"
n15: rectangle label:"Consume Event"
n16: rectangle label:"Sync Read Model"
n13.handle(right) -> n14.handle(left) [label="UserUpdated"]
n14.handle(right) -> n15.handle(left) [label="Subscribe"]
n15.handle(right) -> n16.handle(left)
n16.handle(top) -> OrderService.n11.handle(bottom) [label="Update Denormalized"]
n13.handle(top) -> UserService.n8.handle(bottom) [label="After Write"]
}

为什么需要这个工作流?

Sharing a single database across microservices creates tight coupling, schema change conflicts, and performance bottlenecks that defeat the purpose of microservices. The database-per-service pattern ensures each service owns its data, enabling independent deployment, technology-appropriate storage choices, and fault isolation.

工作原理

  1. Step 1: Each microservice owns a dedicated database optimized for its access patterns.
  2. Step 2: The User Service uses PostgreSQL for relational data; the Order Service uses MongoDB for document storage.
  3. Step 3: Cross-service data needs are fulfilled through event-driven synchronization via Kafka.
  4. Step 4: When a user updates their profile, a UserUpdated event is published to the event bus.
  5. Step 5: The Order Service consumes the event and updates its denormalized user data.
  6. Step 6: The API Gateway aggregates responses from multiple services for client queries.

替代方案

Shared databases are simpler but create deployment coupling and single points of failure. Database-per-service with synchronous API calls creates latency chains. This template shows the event-driven approach that balances autonomy with data consistency.

Key Facts

Template Name微服务每服务独立数据库架构
CategoryArchitecture
Steps6 workflow steps
FormatFlowZap Code (.fz file)

相关模板

事件驱动发布-订阅架构

Architecture

事件驱动发布-订阅架构图,展示 Kafka 或 RabbitMQ 消息代理、事件序列化、主题分区、向多个消费者的扇出交付以及死信队列错误处理。该模板模拟了生产者和消费者通过消息代理完全解耦的基础异步消息传递模式。对于构建松耦合、可扩展的事件驱动系统的架构师至关重要。

事件驱动编排架构

Architecture

事件驱动编排架构图,微服务通过事件协调而无需中央编排器,订单、支付、库存和物流服务相互响应对方的领域事件。该模板模拟去中心化协调模式,每个服务只了解自己的职责并发布事件供其他服务消费。适合偏好服务自治而非集中式工作流控制的团队。

微服务 API 网关架构

Architecture

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

微服务 Backend-for-Frontend (BFF) 架构

Architecture

Backend-for-Frontend 架构图,为 Web 和移动客户端提供独立的 BFF 层,每层针对其特定平台优化 API 响应,同时共享通用后端微服务。该模板展示如何通过按客户端类型定制数据聚合和负载优化来避免一刀切的 API。推荐给从共享微服务后端服务多个前端平台的团队。

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

Architecture

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

事件驱动流处理管道架构

Architecture

实时事件流管道架构图,IoT 传感器、应用日志和用户点击流数据通过 Kafka 流入 Apache Flink 进行窗口聚合、异常检测以及向数据湖和仪表板的多目标输出。该模板可视化从摄取到转换、存储和告警的端到端流处理。对于构建实时分析和监控平台的数据工程师至关重要。

返回所有模板