Search code examples
laravelapivimeo-apirate-limiting

How to handle external API rate limits?


When using services such as Twitter or Vimeo they use API rate limit for each request that happen to there API and they will return ( in the response's headers ) something like this:

X-RateLimit-Limit X-RateLimit-Remaining X-RateLimit-Reset

For example, In my app I will use Vimeo API for alot of things.. like:

  • Upload a video
  • Edit a video
  • Get a video

For each request to Vimeo API I have to check if X-RateLimit-Remaining value is > 0 to continue or return error if the rate limit has been exceeded. ( Vimeo API rate limit ).


Is there a good practice to handle external API rate limit?

What is the best place to store these values, so that I can retrieve them when needed?

Note: Vimeo API doesn't just return an error when rate limit has been reached, It will banned my app when I exceed that limit as the docs says. So I need to store them some where. ( #Vimeo API rate limit ).


Solution

  • You should chache the values locally, depending on your cache driver, which can be redis, memcached, etc...

    Read more here: https://laravel.com/docs/5.7/cache

    I'd suggest you to create a class to manage the cached values, updating/creating them from the APIs when needed. This class could be a dependency of the class which actually contains the logic to call the external APIs.