Category: Laravel, api

Let's see how we may deal with a job that sends an HTTP request to an API that only allows 30 requests per minute: Here's how the job may look: If we hit a rate limit response 429 Too Many Requests, we're going to release the job back to the queue to be retried again after 30 seconds.

For example, if we sent all 30 requests at 10:10:45, we won't be able to send requests again before 10:11:00. If we know requests will keep failing, there's no point in sending them and delaying processing other jobs in the queue. Instead, we're going to set a key in the cache when we hit the limit, and release the job right away if the key hasn't expired yet.

Now we're going to check for that cache key at the beginning of the handle() method of our job and release the job back to the queue if the cache key hasn't expired yet: $timestamp - time() will give us the seconds remaining until requests are allowed.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners