简体   繁体   English

你如何使用许多 Heroku 测功机来限制 REST API?

[英]How do you throttle a REST API using many Heroku dynos?

I configured my REST API throttling, accepting 10 requests max per minute.我配置了我的 REST API 节流,每分钟最多接受 10 个请求。 It works as expected in a local environment.它在本地环境中按预期工作。

However, when deploying on Heroku, this throttling is applied on a dyno basis.但是,在 Heroku 上部署时,此节流是基于测功机应用的。 Since the routing to select a dynos is kind of random, this means for example if I have 2 dynos, an error can occur between the 11th and the 20th request (this often happens around 16 or 17 requests actually).由于到 select 的路由是随机的,这意味着例如,如果我有 2 个 dyno,第 11 和第 20 个请求之间可能会发生错误(这实际上通常发生在 16 或 17 个请求左右)。

How to have a clean throttling with deterministic rates, that does not change when dyno config changes, on Heroku?如何在 Heroku 上以确定的速率进行干净的节流,当测功配置更改时不会改变?

You must currently be storing the throttling data into memory.您当前必须将限制数据存储到 memory 中。 You need to store that in a shared database, such as Redis.您需要将其存储在共享数据库中,例如 Redis。 If you read and write the value to/from that database every time, it will be shared among dynos and you will have something a lot more reliable.如果您每次都在该数据库中读取和写入值,它将在 dynos 之间共享,您将拥有更可靠的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM