Laravel Queue

Laravel Queues & Jobs: Delayed Tasks Made Easy (Chapter 9)

๐Ÿš€ 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 WelcomeMail Mailable 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

TaskIdeal 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.


๐Ÿ”— Further Reading

Leave a Reply

Your email address will not be published. Required fields are marked *