【文章自荐】API 限流:几种算法怎么选、429 该带哪些响应头、多实例下怎么算

Open Beginner friendly
#197 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
90/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Active
Tech stack
express, fastapi, javascript, nginx, python, redis

Research direction

The issue provides the article link and a detailed summary of its coverage, including rate limiting algorithms, response headers, per-customer limits, and multi-instance deployment. Start by reviewing the linked article and the repository’s contribution conventions for self-recommended articles. Done when the article is added in the appropriate place with its title, link, and summary.

Written by the indexing model from the issue text.

Description

这是我们团队写的一篇 API 限流长文(英文),偏实操,适合要给 API 加按客户限流的后端和运维同学:

  1. 五种算法(固定窗口、滑动窗口日志、滑动窗口计数、令牌桶、漏桶)放在一张表里比,每种要存什么状态、允不允许突发、适合什么场景。顺带提到 nginx 的 limit_req 属于漏桶,超出突发量默认返回 503,要设 limit_req_status 429 才是 429。
  2. 令牌桶从头算一遍:容量 10、每秒补 1 个,第 11 个请求什么时候被拒、Retry-After 为什么要向上取整,以及桶设太大会让「每分钟 60 次」的套餐在空闲后第一分钟放进约 120 个请求。
  3. 429 该带什么头:RFC 6585、RFC 9110 的 Retry-After,以及 IETF 的 RateLimit 头草案从 RateLimit-Limit/Remaining/Reset 三个字段演变成现在 RateLimit-Policy 加 RateLimit 的过程(最新是 2026 年 5 月的 -11 版,还不是 RFC)。
  4. 按客户限流时中间件的顺序:先验 key,再按客户 ID(不是 key 字符串)建桶,否则一个客户开五个 key 就有五倍额度。
  5. 多实例部署时的三种做法:每个实例各算各的、按实例数平分、放 Redis 共享,各自的代价和失效时的取舍。

文末有 Express 和 FastAPI 两份令牌桶实现,用假时钟写了测试,可以直接拿去改。

  • 截图:无
Dominant language
No language data
Stars
612
Forks
34
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from eryajf/learning-weekly

All issues in eryajf/learning-weekly

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.