Add a Backoff Between Job Retries with retryAfter Laravel

Last updated 5th September, 2023

It took me ages to figure this out so I thought I'd write a short explainer on how to do this.

TLDR; the property you're looking to add to your jobs is - backoff, not retryAfter.

I spent about 15 minutes searching for the property before realising it was deprecated 😅.

The retryAfter method and retryAfter property was added to Laravel's queue system way back in 5.8 -Changelog from Laravel News. These allow you to define the number of seconds a job should wait before retrying after a failure.

In Laravel 8 however, these methods and properties were renamed to backoff. Here's An article documenting the change.

How to Use Backoff in Laravel 8+

<?php

class ExampleJob implements ShouldQueue
{
    public int|array $backoff = [10, 20];

    // Or...

    public function backoff(): int|array
    {
        return [10, 20]; 
    } 
}

The backoff method can provide either an integer or an array of integers. These integers represent the number of seconds that should be waited between retries. If an array is returned, the index of the integer in that array represents the failure count of that job.

So in the example above, once a given job fails on its first attempt, it will retry after 10 seconds. If it fails a second time, it will retry 20 seconds after that failure.

It took me an embarrassing amount of time to figure out that the method had been renamed! I hope this helps you come to the same conclusion faster!

🤙

No comments yet…

DevInTheWild.

Login To Add Comments.

Want More Like This?

Subscribe to occasional updates.