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

速率限制器架构

Architecture

速率限制器架构图,实现令牌桶算法,使用 Redis 支持的分布式计数器、滑动窗口日志、API 密钥识别、速率限制头和多节点同步以实现一致的执行。该模板展示如何保护 API 免受滥用并确保客户端间的公平使用,包括适当的 HTTP 429 响应和 Retry-After 头。对于构建生产级速率限制基础设施的 API 平台团队至关重要。

完整 FlowZap 代码

Client { # Client
n1: circle label:"API Request"
n2: rectangle label:"Attach API Key"
n3: rectangle label:"Receive Response"
n4: circle label:"End"
n1.handle(right) -> n2.handle(left)
n2.handle(bottom) -> RateLimiter.n5.handle(top) [label="Request"]
n3.handle(right) -> n4.handle(left)
}
RateLimiter { # Rate Limiter (Token Bucket)
n5: rectangle label:"Extract Client Identity"
n6: rectangle label:"Lookup Rate Limit Config"
n7: rectangle label:"Check Token Bucket"
n8: diamond label:"Tokens Available?"
n9: rectangle label:"Consume Token"
n10: rectangle label:"Return 429 Too Many Requests"
n11: rectangle label:"Add Rate Limit Headers"
n12: rectangle label:"Forward to Backend"
n5.handle(right) -> n6.handle(left) [label="API Key / IP"]
n6.handle(right) -> n7.handle(left) [label="100 req/min"]
n7.handle(right) -> n8.handle(left)
n8.handle(right) -> n9.handle(left) [label="Yes"]
n8.handle(bottom) -> n10.handle(top) [label="No"]
n9.handle(right) -> n11.handle(left) [label="Decrement"]
n11.handle(right) -> n12.handle(left) [label="X-RateLimit-Remaining"]
n10.handle(right) -> n11.handle(left) [label="Retry-After"]
n10.handle(top) -> Client.n3.handle(bottom) [label="429"]
n12.handle(bottom) -> Backend.n13.handle(top) [label="Allowed"]
}
Backend { # Backend Service
n13: rectangle label:"Process Request"
n14: rectangle label:"Return Response"
n13.handle(right) -> n14.handle(left) [label="Execute"]
n14.handle(top) -> Client.n3.handle(bottom) [label="200"]
}
Store { # Rate Limit Store (Redis)
n15: rectangle label:"Redis Counter"
n16: rectangle label:"Sliding Window Log"
n17: rectangle label:"Distributed Sync"
n7.handle(bottom) -> Store.n15.handle(top) [label="INCR + EXPIRE"]
n15.handle(right) -> n16.handle(left) [label="Window Check"]
n16.handle(right) -> n17.handle(left) [label="Multi-Node"]
}

为什么需要这个工作流?

Without rate limiting, a single abusive client or a traffic spike can overwhelm your API, degrading performance for all users. A rate limiter enforces fair usage quotas per client, protecting backend services from overload while providing clear feedback through standard HTTP headers.

工作原理

  1. Step 1: The client identity is extracted from the API key or IP address.
  2. Step 2: The rate limit configuration is looked up for the identified client tier.
  3. Step 3: The token bucket in Redis is checked for available tokens.
  4. Step 4: If tokens are available, one is consumed and the request proceeds with rate limit headers.
  5. Step 5: If no tokens are available, a 429 Too Many Requests response is returned with a Retry-After header.
  6. Step 6: Distributed Redis synchronization ensures consistent rate limiting across multiple API gateway nodes.

替代方案

Application-level rate limiting does not work across multiple instances. Cloud provider rate limiting (AWS API Gateway) is simpler but less customizable. This template shows the distributed token bucket implementation with Redis.

Key Facts

Template Name速率限制器架构
CategoryArchitecture
Steps6 workflow steps
FormatFlowZap Code (.fz file)

相关模板

CQRS 读写分离架构

Architecture

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

微服务 API 网关架构

Architecture

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

微服务服务网格架构

Architecture

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

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

Architecture

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

按业务能力分解微服务架构

Architecture

按业务能力组织的微服务分解架构图:身份认证、产品目录、定价和订单履行,每个都有独立的数据存储和 API。该模板展示如何将单体应用拆分为与业务领域对齐的服务,使用 Backend-for-Frontend (BFF) 模式进行客户端特定的聚合。适合规划领域驱动微服务边界的架构师。

微服务绞杀者模式迁移架构

Architecture

绞杀者模式迁移架构图,展示使用路由层在新旧系统之间分流流量,逐步用新微服务替换遗留单体应用。该模板模拟经过验证的迁移策略,新功能作为微服务构建,遗留端点逐步退役。对于在不进行高风险大爆炸重写的情况下现代化遗留系统的团队至关重要。

返回所有模板