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?
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 ...
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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With