TechnicalMedium PHP & Laravel
What are queues and jobs in Laravel and when should you use them?
Asked for Backend Developer
Why they ask this: Shows you know how to keep web requests fast and handle slow work reliably.
Answer
A job is a class that does one piece of work. A queue stores jobs so that a separate worker process (php artisan queue:work) runs them in the background.
Use queues for anything slow or unreliable that the user should not wait for:
- sending emails and SMS,
- generating PDFs or resizing images,
- calling third-party APIs (payment verification, AI),
- importing large CSV files.
php
class SendWelcomeEmail implements ShouldQueue
{
use Queueable;
public int $tries = 3;
public function __construct(public User $user) {}
public function handle(): void
{
Mail::to($this->user)->send(new WelcomeMail($this->user));
}
}
SendWelcomeEmail::dispatch($user);Queue drivers include database, redis and Amazon SQS. Failed jobs are stored and can be retried with php artisan queue:retry. In production, keep the worker running with Supervisor (or a platform's worker process) and restart it after each deploy.
Can you explain this in your own words?