Get "PHP 8 in a Nuthshell" (Soon includes PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Retry operations with high probability of failure in Laravel

There are certain operations that can have a high probability of failure. For instance, sending a request to a third-party API. You’re not sure whether it will work every time flawlessly because it’s not in your control. If the API endpoint you’re hitting is not responding, you might end up with an exception straight away.

I’m sure we all don’t want that to happen. So, what would you do in such a scenario? You’ll “retry” the same operation, no?

Well, if your application is built on top of Laravel, there’s a really handy helper function that exists in the framework which can help you do just that.

The retry helper function

Essentially, retry is a general-purpose helper function that helps you attempt the given callback until the given maximum attempt threshold is met.

Here’s the signature of this function.

function retry($times, callable $callback, $sleep = 0, $when = null);

As you can see, the function accepts four arguments.

  • $times - The maximum number of times you want to attempts the callback.
  • $callback - The callback that you want to get retried (on an exception).
  • $sleep (optional) - The number of milliseconds that Laravel should wait in between attempts.
  • $when (optional) - The callback where you can specify an alternative logic (a predicate) on which you want the original callback to be attempted.

So, if you want to perform an API call that you want to be retried (on failure) 3 times every 200 milliseconds, you can use the retry function like so.

$response = retry(3, function () {
    return Http::get('http://example.com/users/1');
}, 200)

Here, if the callback throws an exception, it will automatically be retried otherwise its return value will be returned. Once the maximum attempt count is exceeded, the exception will be thrown.

If you don’t provide the number of milliseconds, Laravel will attempt the callback immediately.

Retry in the Http client

Alternatively, you can directly call the retry method (which is very similar to the retry helper function) if you’re using the Http client like so.

$response = Http::retry(3, 200)->get('http://example.com/users/1');

As you can tell,

  • The first argument that retry accepts is the maximum number of times the request should be attempted
  • And the second argument (Optional, default = 0) is the number of milliseconds that Laravel should wait in between attempts.

If all of the requests fail, an instance of Illuminate\Http\Client\RequestException will be thrown.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?