Laravel Queues make it easy to move time-consuming tasks out of the main request lifecycle. But running jobs in the background introduces a few important questions:
- What happens when a job fails?
- How many times should Laravel retry it?
- How long should Laravel wait before retrying?
- What happens when a job takes too long to finish?
Understanding tries, backoff, timeout, and retry_after is essential when building reliable queue workers.
Let's look at each one.
1. Understanding Job Retries
Laravel allows you to control how many times a job can be attempted before it is marked as failed.
You can configure this at the worker level:
php artisan queue:work redis --tries=5 Or define it directly inside your job:
public function tries(): int { return 5; } This means Laravel can attempt the job up to five times.
One important detail: an attempt doesn't necessarily mean that the handle() method completed or even ran successfully.
An attempt can also be consumed when:
- An exception is thrown.
- A job is manually released.
- Middleware releases a job because a lock cannot be acquired.
- A job times out.
This is particularly important when using middleware such as WithoutOverlapping.
What about retryUntil()?
Instead of limiting retries by a fixed number, you can define a time window.
use DateTime; public function retryUntil(): DateTime { return now()->addMinutes(10); } Laravel will continue attempting the job until this deadline is reached.
If both tries() and retryUntil() are defined, retryUntil() takes precedence.
Use a maximum attempt count when you want predictable retry limits. Use retryUntil() when the retry window matters more than the number of attempts.
2. Understanding Backoff
Imagine a job calls an external API.
The API is temporarily unavailable, and the request fails.
Should Laravel retry immediately?
Usually, no.
Retrying immediately can increase pressure on an already struggling service.
This is where backoff comes in.
Backoff defines how long Laravel should wait before retrying a failed job.
You can configure it using the backoff() method:
public function backoff(): array { return [10, 30, 60]; } This means:
| Retry | Delay |
|---|---|
| First retry | 10 seconds |
| Second retry | 30 seconds |
| Third retry | 60 seconds |
| Subsequent retries | 60 seconds |
You can also configure backoff at the worker level:
php artisan queue:work redis --tries=5 --backoff=10 For external services, increasing retry delays can help avoid unnecessary requests during temporary outages.
3. Understanding Timeout
A timeout defines how long a job is allowed to run.
For example:
php artisan queue:work redis --timeout=45 If a job takes longer than 45 seconds, Laravel's worker will terminate with an error.
Timeouts are particularly useful when dealing with:
- External API requests.
- Large file processing.
- Database operations.
- Long-running background tasks.
However, worker timeouts do not replace HTTP client timeouts.
For example, when using Laravel's HTTP client, you should configure connection and request timeouts separately:
$response = Http::connectTimeout(5) ->timeout(20) ->get('https://api.example.com/data'); This prevents an external request from blocking your job indefinitely.
Also, Laravel requires the PHP pcntl extension for job timeout handling.
4. Timeout vs retry_after: An Important Difference
This is one of the most important queue configuration details.
Although they sound similar, timeout and retry_after serve different purposes.
timeout controls how long a worker is allowed to process a job.
retry_after controls how long the queue connection waits before making a reserved job available again.
For example, consider this configuration:
// config/queue.php 'redis' => [ 'driver' => 'redis', 'connection' => 'default', 'queue' => env('REDIS_QUEUE', 'default'), 'retry_after' => 60, 'block_for' => null, ], And your worker:
php artisan queue:work redis --timeout=45 Here:
- The worker timeout is 45 seconds.
- The queue retry interval is 60 seconds.
The timeout should be several seconds shorter than retry_after.
What happens if you get this wrong?
Imagine:
- Worker A starts processing a job.
- The job takes longer than expected.
-
retry_afterexpires before Worker A is terminated. - Worker B picks up the same job.
- Both workers may now execute the same operation.
This can lead to duplicate processing and unexpected side effects.
A good starting point is:
Worker timeout: 45 seconds Retry after: 60 seconds These values are examples, not universal defaults. Choose them based on your job's expected execution time.
5. Handling Failed Jobs
Not every job can succeed, even after multiple attempts.
Laravel provides a failed_jobs table to store jobs that have exhausted their allowed attempts or otherwise failed permanently.
You can inspect failed jobs using:
php artisan queue:failed Retry a specific failed job:
php artisan queue:retry <job-id> Or retry all failed jobs:
php artisan queue:retry all You can also define a failed() method inside your job to perform cleanup or logging when the job permanently fails.
public function failed(?Throwable $exception): void { Log::error('Job permanently failed', [ 'message' => $exception?->getMessage(), ]); } This is useful for notifications, monitoring, and recording failures that require manual investigation.
6. Putting It All Together
Here is a simplified example of a job that synchronizes data from an external API:
<?php namespace App\Jobs; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Queue\Queueable; use Illuminate\Queue\Attributes\Timeout; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; use Throwable; #[Timeout(45)] class SyncExternalData implements ShouldQueue { use Queueable; public function __construct( public int $recordId, ) {} public function tries(): int { return 5; } public function backoff(): array { return [10, 30, 60]; } public function handle(): void { $response = Http::connectTimeout(5) ->timeout(20) ->get('https://api.example.com/data'); $response->throw(); // Process the response... } public function failed(?Throwable $exception): void { Log::error('External data synchronization failed', [ 'record_id' => $this->recordId, 'error' => $exception?->getMessage(), ]); } } The worker can be started with:
php artisan queue:work redis --timeout=45 And the Redis queue connection should have a retry_after value greater than 45 seconds, for example 60 seconds.
This gives you control over retries, retry delays, execution time, and permanent failure handling.
Final Thoughts
Laravel provides powerful queue management features, but configuring them correctly is just as important as writing the job itself.
Keep these principles in mind:
- Use
triesorretryUntil()to control retry limits. - Use
backoff()to avoid retrying too aggressively. - Use
timeoutto prevent workers from getting stuck. - Keep
timeoutshorter thanretry_after. - Handle permanently failed jobs explicitly.
And remember: Retries are not a guarantee of successful processing. They are a mechanism for recovering from temporary failures.
Your jobs should still be designed to handle repeated execution safely.
References:
- Laravel Documentation – Queues
- Laravel Documentation – Job Retries
- Laravel Documentation – Job Timeouts
- Laravel Documentation – Failed Jobs

