๐ Introduction: Why Queues Matter in Laravel
In modern web applications, responsiveness is everything. You donโt want your users to wait while the system sends emails, resizes images, or pushes notifications. Thatโs where Laravel Queues come in.
With queues, you offload time-consuming tasks to run in the background, letting your app stay fast and smooth.
๐ง What Are Laravel Queues?
Queues in Laravel allow you to defer time-consuming processes like:
- Sending emails
- Processing video/audio files
- Generating PDFs
- Running heavy reports
- Web scraping
- Sending SMS/push notifications
Instead of running immediately, these tasks are dispatched and executed by a queue worker asynchronously.
๐ง Prerequisites
Before using Laravel Queues:
- Laravel 8 or 9+ installed
- Database connection setup
- Basic understanding of Artisan commands
- Optional: Redis or Amazon SQS (for production-grade queues)
๐ ๏ธ Step 1: Setting Up Queue Configuration
In your .env file, set the default queue driver:
QUEUE_CONNECTION=database
Laravel supports multiple drivers:
sync(default, immediate execution)database(stores jobs in DB)redis(recommended for production)sqs,beanstalkd, etc.
๐๏ธ Step 2: Create the Jobs Table
If you’re using the database driver, you must create the table to hold queued jobs.
php artisan queue:table
php artisan migrate
This generates a jobs table to store pending tasks.
๐งฑ Step 3: Creating a Job
Letโs create a job that sends a welcome email to new users.
php artisan make:job SendWelcomeEmail
Laravel will generate the job class in app/Jobs/SendWelcomeEmail.php.
Update it:
namespace App\Jobs;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeMail;
class SendWelcomeEmail implements ShouldQueue
{
use Queueable;
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function handle()
{
Mail::to($this->user->email)->send(new WelcomeMail($this->user));
}
}
โ Tip: Ensure your
WelcomeMailMailable class is ready.
๐ค Step 4: Dispatching a Job
Dispatch the job like this:
SendWelcomeEmail::dispatch($user);
This queues the job โ it wonโt execute until a worker picks it up.
๐งโ๐ญ Step 5: Running the Queue Worker
Now run the worker to start processing jobs:
php artisan queue:work
For local dev, keep this running in a separate terminal.
If you want the worker to run continuously (e.g. on a server), consider using Supervisor (Linux) or Laravel Horizon (Redis).
๐ Delayed Jobs (Optional)
Delay a job by seconds:
SendWelcomeEmail::dispatch($user)->delay(now()->addSeconds(30));
Or schedule it later:
SendWelcomeEmail::dispatch($user)->delay(now()->addMinutes(5));
โ Handling Failures
If a job fails, Laravel logs it. You can add a failed() method inside your job class:
public function failed(\Exception $exception)
{
// Notify admin or log failure
}
To retry failed jobs:
php artisan queue:retry all
๐ Monitoring with Laravel Horizon (Optional)
If you’re using Redis:
composer require laravel/horizon
php artisan horizon:install
php artisan migrate
php artisan horizon
Then visit /horizon to view your jobs dashboard in real time.
๐ Common Use Cases for Queues
| Task | Ideal With Queues? |
|---|---|
| Sending Emails | โ Yes |
| Notifications (SMS/Push) | โ Yes |
| Payment Processing | โ Yes (Webhooks) |
| Image/File Uploading | โ Yes |
| User Sign-in/Sign-out | โ No (real-time) |
๐ก Pro Tips
- Use Redis for production (itโs faster and supports Laravel Horizon).
- Set timeouts and retries to avoid stuck jobs.
- Always test your jobs locally before deploying.
- Log job failures and notify developers/admins.
๐ Conclusion
Laravel Queues & Jobs offer an elegant, scalable way to handle background tasks efficiently. Whether youโre sending emails or processing huge data files, queues keep your app fast and your users happy.
Start simple with the database driver, and when you’re ready โ scale to Redis + Horizon.
