Has your app ever encountered a 429 (Too Many Requests) status code when making requests to a third-party API? Getting rate limited can be a nussance and if not handled properly can cause a bad experience for you and your users. While one solution is to catch the exception and ignore it, a better solution is to retry the request.
Let's take a look at how we can alleviate rate-limiting woes by utilizing a background job system. In this example we'll use delayed_job
, since it provides the ability to retry failed jobs.
Let's pretend that we are going to be accessing the API of a popular website. Let's setup a background job that makes a request to that API.
class MyCustomJob < Struct.new(:param1, :param2)
def perform
PopularSiteApi.get('/posts')