Architecture Patterns
API Gateway
统一 API 入口
API Gateway 是位于 client 与一组 backend services 之间的 API 管理层。它是系统的单一入口,封装内部架构,并为不同 client 提供定制化 API,同时承担认证、监控、load balancing、caching、throttling、logging 等职责。

为什么需要 API Gateway?
Microservices 提供的 API 往往更细粒度,client 通常需要调用多个服务。API Gateway 提供统一入口,并附加管理能力,简化 client 交互。
Features
常见功能:
- Authentication & Authorization
- Service discovery
- Reverse Proxy
- Caching
- Security
- Retry & Circuit breaking
- Load balancing
- Logging, Tracing
- API composition
- Rate limiting & throttling
- Versioning
- Routing
- IP whitelisting / blacklisting
Advantages
- 封装内部结构
- 提供 API 的集中视图
- 简化 client 代码
- 统一监控、分析与 tracing
Disadvantages
- 可能成为 single point of failure
- 性能可能受影响
- 若不 scale,可能成为 bottleneck
- 配置复杂
Backend For Frontend (BFF) pattern
BFF 模式会为特定 frontend 创建专属 backend。适用于不同端(web、mobile、partner)需求差异大时。
有时 microservices 返回的数据格式并不适合前端,需要额外组装与过滤;BFF 可以把这部分逻辑放在中间层。

BFF 的核心职责是聚合与格式化数据并返回给 frontend。
GraphQL 常作为 BFF 的实现方式。
什么时候用 BFF?
- 通用 backend 维护成本很高
- 需要针对某个 client 定制优化
- 不同端有大量差异化需求