【文章自荐】API 限流:几种算法怎么选、429 该带哪些响应头、多实例下怎么算
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
- Domain
- backend-api-design, devops, documentation
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 rate limiting: algorithms, headers and code
- 文章链接:https://usagerail.com/api-rate-limiting
- 文章摘要:
这是我们团队写的一篇 API 限流长文(英文),偏实操,适合要给 API 加按客户限流的后端和运维同学:
- 五种算法(固定窗口、滑动窗口日志、滑动窗口计数、令牌桶、漏桶)放在一张表里比,每种要存什么状态、允不允许突发、适合什么场景。顺带提到 nginx 的
limit_req属于漏桶,超出突发量默认返回 503,要设limit_req_status 429才是 429。 - 令牌桶从头算一遍:容量 10、每秒补 1 个,第 11 个请求什么时候被拒、
Retry-After为什么要向上取整,以及桶设太大会让「每分钟 60 次」的套餐在空闲后第一分钟放进约 120 个请求。 - 429 该带什么头:RFC 6585、RFC 9110 的
Retry-After,以及 IETF 的 RateLimit 头草案从RateLimit-Limit/Remaining/Reset三个字段演变成现在RateLimit-Policy加RateLimit的过程(最新是 2026 年 5 月的 -11 版,还不是 RFC)。 - 按客户限流时中间件的顺序:先验 key,再按客户 ID(不是 key 字符串)建桶,否则一个客户开五个 key 就有五倍额度。
- 多实例部署时的三种做法:每个实例各算各的、按实例数平分、放 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from eryajf/learning-weekly
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
eryajf/learning-weekly#211 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 70/100
eryajf/learning-weekly#210 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
eryajf/learning-weekly#207 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
eryajf/learning-weekly#205 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 70/100
eryajf/learning-weekly#169 ·
All issues in eryajf/learning-weekly
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Deltares/numba_celltree#100 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
hexlet-basics/hexlet-basics#968 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
kubernetes-sigs/scheduler-plugins#1031 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
stratum-mining/sv2-tp#167 ·
Maintainers usually reply within 1 day
-
bug help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
heysidhant/appvanish#3 ·