针对给定场景的速率限制算法建议

发布于 2024-12-22 22:39:57 字数 208 浏览 2 评论 0原文

实现以下速率限制用例的最佳机制是什么?可以使用像令牌桶这样的通用算法,但我希望在 memcached 的上下文中实现。感谢对此的帮助。

  • 一天内仅允许来自客户 ABC 的 100 个呼叫。
  • 一小时内仅允许客户 ABC 进行 50 次 api 调用。
  • 对于任何用户 ID,一小时内仅允许客户 ABC 进行 5 次 api 调用。

What is the best mechanism to implement the below rate limiting use-cases. General algorithms like Token Bucket can be used but I want the implementation to be in the context of memcached. Appreciate the help on this.

  • allow only 100 calls from customer ABC in a day.
  • allow only 50 api calls for customer ABC in an hour.
  • allow only 5 api calls for customer ABC for any userid in an hour.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大海や 2024-12-29 22:39:57

当达到此限制时,您希望发生什么?您可以阻止服务或重定向到明天返回的页面,但这非常难看。

您可以减慢处理请求的速率,但这对于每秒处理多个请求的速率更有用。例如,客户 ABC 限制为每秒或每分钟 100 个。对于您所说的非常低的速率,客户端会超时并认为服务器已死亡。

What do you want to happen when when this limit is reached? You can prevent service or redirect to a come-back-tomorrow page, but that's pretty ugly.

You can slow down the rate their requests are processed but this is more useful for rates of several per second. e.g. customer ABC is limited to 100 per second or per minute. For the very low rates you are talking about, the client would time out and think the server is dead.

み零 2024-12-29 22:39:57

如果我理解正确的话,每个 API 请求都带有一个用户 ID 和客户。每个客户可以有多个用户 ID。您希望在客户和用户 ID 级别上进行速率限制。

您将需要使用多个键 ABC_day、ABC_hour、ABC_userID1_hour、ABC_userID2_hour 等来计算这些活动发生的次数。这种方法的问题是何时重置这些计数器。由于 memcached 仅支持数值上的增量运算符,因此我们需要将此信息编码在密钥本身中。我们可以使用 ABC_2012_02_28 作为日速率限制的键。在您的代码中,只需使用当前日期创建它们的密钥并递增它。当日期更改时,您的代码将查找不存在的 ABC_2012_02_29,并让您有机会创建新密钥。

另一种选择是缓存主义。它是一个类似于 memcached 的缓存,支持 memcached 协议,并提供“缓存中”脚本。您可以实现自己的“速率限制”对象,而不是创建如此多的密钥,该对象将为您完成所有这些簿记工作。
请参阅http://chakpak.blogspot.in/2011/09/ rate-limitingquota-with-cacheismo.html 用于速率限制的示例实现。你可以从这里得到它。 https://github.com/iamrohit/cacheismo

If I understand this correctly each API request comes with a userID and customer. Each customer can have multiple user ids. You want to rate limit both at customer and userID level.

You will need to use multiple keys ABC_day, ABC_hour, ABC_userID1_hour, ABC_userID2_hour etc to count the number of times these activities have occurred. The problem with this approach is when to reset these counters. Since memcached only supports increment operator on numerical values we need to encode this information in the key itself. We can use ABC_2012_02_28 as the key for day rate limit. In your code just create they key using current date and increment it. When the day changes your code will look for ABC_2012_02_29 which doesn't exist and gives you a chance to create a new key.

Alternative is cacheismo. It is a cache like memcached and supports memcached protocol and it provides "in cache" scripting. Instead of creating so many keys, you can implement your own "ratelimiting" object which will do all this bookkeeping for you.
See http://chakpak.blogspot.in/2011/09/rate-limitingquota-with-cacheismo.html for sample implementation of rate limiting. You can get it from here. https://github.com/iamrohit/cacheismo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文