Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with API rate limits?

I've an app that's set up to make scheduled calls to a number of APIs once a day. This works very nicely but i'm aware that some of the APIs i'm calling (Twitter for example) have a rate limit. As the number of calls i'm making is set to continually grow, can anyone recommend a way to throttle my calls so I can send in bursts of x per hour/minute etc?

I've found the Glutton Ratelimit gem, is anyone using this and is it any good? Are there others I should be looking at?

like image 322
Raoot Avatar asked May 20 '13 08:05

Raoot


People also ask

What are the best practices for API rate limiting?

Best Practices For API Rate Limiting 1 Are requests throttled when they exceed the limit? 2 Do new calls and requests incur additional fees? 3 Do new calls and requests receive a particular error code and, if so, which one? More ...

What happens when an API request exceeds its rate limit?

If a slew of requests is made when the window refreshes, your API could still be stampeded. A sliding log algorithm involves tracking each request via a time-stamped log. Logs with timestamps that exceed the rate limit are discarded. When a new request comes in, the sum of the logs are calculated to determine the request rate.

How do you limit API access?

Best Practices For API Rate Limiting One approach to API rate limiting is to offer a free tier and a premium tier, with different limits for each. There are many things to consider when deciding what to charge for premium API access. For cost estimates, read our piece on API Pricing or API business models for ideas.

What is LinkedIn’s API rate limiting?

LinkedIn’s rate limiting documentation explains how different API endpoints have different limits. LinkedIn’s API features three different kinds of rate limiting: application throttle, user throttle, and developer throttle. The documentation also specifies the time zone used to define the beginning and end of the day.


1 Answers

If you're using some kind of background worker to perform your API calls, you could reschedule the task to be reperformed in the next time slot, when the rate limits have been reset.

class TwitterWorker
  include Sidekiq::Worker

  def perform(status_id)
    status = Twitter.status(status_id)
    # ...

  rescue Twitter::Error::TooManyRequests
    # Reschedule the query to be performed in the next time slot
    TwitterWorker.perform_in(15.minutes, status_id)
  end
end

No scientific solution though, there's e.g. the risk that a query might be rescheduled each time if you try to perform much more API calls in a day than the rate limit allows for. But until then, something easy might do the trick!

like image 66
Thomas Klemm Avatar answered Oct 12 '22 09:10

Thomas Klemm