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

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

Architecture

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

完整 FlowZap 代码

WebApp { # Web Application
n1: circle label:"Browser Request"
n2: rectangle label:"Web BFF"
n3: rectangle label:"Render HTML Response"
n4: circle label:"Display Page"
n1.handle(right) -> n2.handle(left)
n2.handle(bottom) -> Services.n9.handle(top) [label="Fetch Data"]
n3.handle(right) -> n4.handle(left)
}
MobileApp { # Mobile Application
n5: circle label:"Mobile API Call"
n6: rectangle label:"Mobile BFF"
n7: rectangle label:"Optimized Payload"
n8: circle label:"Render Native UI"
n5.handle(right) -> n6.handle(left)
n6.handle(bottom) -> Services.n9.handle(top) [label="Fetch Data"]
n7.handle(right) -> n8.handle(left)
}
Services { # Shared Microservices
n9: rectangle label:"User Profile Service"
n10: rectangle label:"Content Service"
n11: rectangle label:"Notification Service"
n12: rectangle label:"Analytics Service"
n9.handle(right) -> n10.handle(left) [label="User Context"]
n10.handle(top) -> WebApp.n3.handle(bottom) [label="Full Payload"]
n10.handle(top) -> MobileApp.n7.handle(bottom) [label="Compact Payload"]
n11.handle(top) -> MobileApp.n7.handle(bottom) [label="Push Token"]
n12.handle(top) -> WebApp.n2.handle(bottom) [label="Track"]
n12.handle(top) -> MobileApp.n6.handle(bottom) [label="Track"]
}

为什么需要这个工作流?

A single API serving both web and mobile clients forces compromises: web clients receive unnecessary mobile-specific fields, and mobile clients download bloated payloads. The BFF pattern creates dedicated API layers for each client type, optimizing response payloads, aggregation logic, and caching strategies per platform.

工作原理

  1. Step 1: The Web BFF aggregates data from shared microservices and renders full HTML payloads.
  2. Step 2: The Mobile BFF returns compact JSON optimized for bandwidth-constrained mobile networks.
  3. Step 3: Both BFFs call the same shared backend microservices for data.
  4. Step 4: Each BFF handles platform-specific concerns like push notifications or SSR.
  5. Step 5: Analytics tracking is integrated at the BFF level for platform-specific metrics.
  6. Step 6: Shared microservices remain platform-agnostic and focused on business logic.

替代方案

A single API with field selection (GraphQL) can reduce the need for BFFs. API versioning per client type is simpler but harder to maintain. This template helps teams decide when a dedicated BFF layer is worth the operational cost.

Key Facts

Template Name微服务 Backend-for-Frontend (BFF) 架构
CategoryArchitecture
Steps6 workflow steps
FormatFlowZap Code (.fz file)

相关模板

微服务 API 网关架构

Architecture

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

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

Architecture

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

事件驱动编排架构

Architecture

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

Saga 编排模式架构

Architecture

Saga 编排架构图,中央编排器协调跨订单、库存和支付服务的多步骤分布式事务,具有专用的补偿链用于失败时的回滚。该模板模拟基于编排的 Saga 模式,单个协调器管理事务生命周期并在任何步骤失败时触发补偿操作。对于实施不使用两阶段提交的可靠分布式事务的架构师至关重要。

API 组合模式架构

Architecture

API 组合架构图,组合器服务向多个微服务扇出并行请求,收集带超时处理的响应,并将结果合并为单一统一响应,支持部分降级。该模板模拟当单个客户端查询需要来自多个服务的数据时使用的 API 组合模式,避免直接的服务间调用。适合在微服务架构中构建聚合层的团队。

微服务服务网格架构

Architecture

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

返回所有模板