用速率限制器扩展你的API
Availability and reliability are paramount for all web applications and APIs. If you’re providing an API, chances are you’ve already experienced sudden increases in traffic that affect the quality of your service, potentially even leading to a service outage for all your users.
可用性和可靠性是所有网络应用程序和API的首要条件。如果你提供一个API,你有可能已经经历了流量的突然增加,影响了你的服务质量,甚至有可能导致所有用户的服务中断。
The first few times this happens, it’s reasonable to just add more capacity to your infrastructure to accommodate user growth. However, when you’re running a production API, not only do you have to make it robust with techniques like idempotency, you also need to build for scale and ensure that one bad actor can’t accidentally or deliberately affect its availability.
在头几次发生这种情况时,在你的基础设施上增加更多的容量以适应用户增长是合理的。然而,当你运行一个生产型的API时,你不仅要用idempotency等技术使其健壮,你还需要建立规模,并确保一个坏行为者不能意外地或故意地影响其可用性。
Rate limiting can help make your API more reliable in the following scenarios:
在以下情况下,速率限制可以帮助使你的API更加可靠。
- One of your users is responsible for a spike in traffic, and you need to stay up for everyone else.
- 你的一个用户对流量激增负责,而你需要为其他人保持清醒。
- One of your users has a misbehaving script which is accidentally sending you a lot of requests. Or, even worse, one of your users is intentionally trying to overwhelm your servers.
- 你的一个用户有一个行为不端的脚本,不小心向你发送了大量的请求。或者,更糟糕的是,你的一个用户故意想让你的服务器不堪重负。
- A user is sending you a lot of lower-priority requests, and you want to make sure that it doesn’t affect your high-priority traffic. For example, users sending a high volume of requests for analytics data could affect critical transactions for other users.
- 一个用户向你发送大量低优先级的请求,你要确保它不会影响你的高优先级流量。例如,用户发送大量的分析数据请求可能会影响其他用户的关键交易。
- Something in your system has gone wrong internally, and as a result you can’t serve all of your regular traffic and need to drop low-priority requests.
- 你的系统内部出了问题,因此你不能为所有的正常流量服务,需要放弃低优先级的请求。
At Stripe, we’ve found that carefully implementing a few rate limiting strategies helps keep the API available for everyone. In this post, we’ll explain in detail which rate limiting strategies we find the most...