Lesson 5 of 9 · 26m
Queues That Survive Production
Job design, retries, idempotency, worker operations, and the queue failure modes that only appear after real traffic arrives.
Queues are not an optimization you bolt on when requests get slow. They are a reliability boundary: the difference between 'the email did not send because a worker was restarting' and 'the job will retry with backoff and page someone if it keeps failing.' Treating queued work as an afterthought is how applications lose invoices, webhooks, and customer notifications.
Push slow or unreliable work behind the boundary
final class GenerateUsageReport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 5;
public int $timeout = 120;
public function __construct(public int $teamId, public string $month) {}
public function backoff(): array
{
return [10, 60, 300, 900];
}
public function handle(ReportGenerator $reports): void
{
$reports->generateForTeam($this->teamId, $this->month);
}
}Pass identifiers or small value objects, not huge serialized state. SerializesModels stores model keys and refetches them, which avoids stale serialized attributes. If the model must be allowed to disappear, implement graceful handling instead of letting every retry explode.
Idempotency is not optional
A queue can deliver a job more than once. A worker can crash after performing the work but before acknowledging the job. Design the handler so a second run is safe:
$report = Report::firstOrCreate(
['team_id' => $teamId, 'month' => $month],
['status' => 'pending']
);
if ($report->status === 'complete') {
return;
}Unique jobs prevent duplicate queued jobs, not duplicate execution after a crash. Database constraints are stronger than application-level good intentions.
Operate workers deliberately
# /etc/supervisor/conf.d/laravel-worker.conf
[program:laravel-worker]
command=php /var/www/app/artisan queue:work redis --queue=high,default --sleep=3 --tries=3 --max-time=3600
numprocs=4
autostart=true
autorestart=true
stopwaitsecs=3600Use separate queues for urgent and slow work. A five-minute video export must not sit ahead of password reset mail. Restart workers after deploys (php artisan queue:restart) so they pick up new code. Horizon is excellent for Redis-backed visibility, but it does not replace durable process supervision.
Failure handling is part of the feature
public function failed(Throwable $exception): void
{
Log::error('Usage report failed permanently', [
'team_id' => $this->teamId,
'month' => $this->month,
'exception' => $exception,
]);
}queue:failed shows jobs that exhausted retries; queue:retry redispatches one. Alerting on failed-job count matters more than the fact that a retry command exists.
Field notes
- Never
sleep()inside a busy job as a retry strategy. Release the job back to the queue with a delay. syncqueue driver is useful in tests and tiny demos, not production reliability.- Jobs run in long-lived processes. Static caches and singletons can carry stale tenant or configuration state.
- A job that must run exactly once is a myth. Build 'at least once, safely repeatable' instead.
Was this lesson helpful?