Speed ​​limit algorithm suggestions for this scenario

What is the best mechanism for implementing the following usage restrictions. You can use general algorithms like Token Bucket, but I want the implementation to be in the memcached context. Please rate the help on this.

  • allow only 100 calls from the ABC client every other day.
  • Allow only 50 api calls for the ABC client in an hour.
  • allow only 5 api-calls for ABC client for any user within an hour.
+2
source share
2 answers

What do you want when you reach this limit? You can disable the service or redirect to the back-back-tomorrow page, but this is pretty ugly.

You can slow down the processing speed of your requests, but it is more useful for bids per second per second. for example, the ABC client is limited to 100 per second or minute. At the very low rates that you are talking about, the client will time out and think that the server is dead.

0
source

If I understand correctly, each API request comes with a user ID and a client. Each client can have several user identifiers. You want to evaluate the limit both at the client level and at the user level.

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

An alternative is cacheismo. It is a cache, like memcached, and supports the memcached protocol, and it provides in-cache scripts. 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 a selective implementation of speed limits. You can get it from here. https://github.com/iamrohit/cacheismo .

0
source

Source: https://habr.com/ru/post/1387919/


All Articles