Some tasks — sending emails, processing images, calling slow third-party APIs — shouldn't block the response to the user. Laravel's queue system lets you defer that work to run asynchronously in the background.
php artisan make:job ProcessPodcast
class ProcessPodcast implements ShouldQueue
{
use Queueable;
public function __construct(public Podcast $podcast) {}
public function handle(AudioProcessor $processor): void
{
$processor->process($this->podcast);
}
}
Dispatching:
ProcessPodcast::dispatch($podcast);
ProcessPodcast::dispatch($podcast)->delay(now()->addMinutes(10));
ProcessPodcast::dispatch($podcast)->onQueue('podcasts');
Laravel supports several queue drivers: sync (runs immediately, useful for local dev), database, redis (most common in production), and sqs. Start processing jobs with:
php artisan queue:work
Failed jobs are logged to a failed_jobs table (or your configured store) and can be retried with php artisan queue:retry. For production, use a process monitor like Supervisor to keep queue:work running continuously, and consider Laravel Horizon for a beautiful dashboard over Redis queues.