DEV Community

Oluwajubelo
Oluwajubelo

Posted on

4

Laravel Queues for Beginners πŸš€

Laravel queues allow you to handle time-consuming tasks asynchronously, meaning they run in the background without slowing down your main application. This is especially useful for tasks like sending emails, processing file uploads, or handling large database operations.

πŸ“Œ Why Use Queues?

1. Improves Performance

Your application doesn’t have to wait for tasks like email sending
to complete before responding.

2. Enhances User Experience

Users get immediate responses while tasks run in the background.

3. Efficient Resource Management

Heavy tasks can be processed separately, reducing the risk of timeouts.

πŸ›  Setting Up Laravel Queues

By default, Laravel comes with a queue system that supports different queue drivers like:

  • sync (Runs jobs immediately, no real queue)

  • database (Stores jobs in a database table)

  • redis (High-performance queue storage)

  • sqs (Amazon SQS)

1️⃣ Configure the Queue Driver
Open your .env file and set the queue driver:

QUEUE_CONNECTION=database
Enter fullscreen mode Exit fullscreen mode

For Redis:

QUEUE_CONNECTION=redis
Enter fullscreen mode Exit fullscreen mode

2️⃣ Create a Queue Job
Run the Artisan command to generate a job:

php artisan make:job SendWelcomeEmail
Enter fullscreen mode Exit fullscreen mode

This creates a job file in app/Jobs/SendWelcomeEmail.php. Inside the handle() method, you define what the job should do.

namespace App\Jobs;

use App\Mail\WelcomeMail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Mail;

class SendWelcomeEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;

    public function __construct($user)
    {
        $this->user = $user;
    }

    public function handle()
    {
        Mail::to($this->user->email)->send(new WelcomeMail($this->user));
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“ Key Points:

  • ShouldQueue tells Laravel to process this job asynchronously.

  • handle() contains the logic for sending the email.

3️⃣ Dispatching a Job
You can dispatch the job from anywhere in your application:

dispatch(new SendWelcomeEmail($user));
Enter fullscreen mode Exit fullscreen mode

Or use the dispatch() helper:

SendWelcomeEmail::dispatch($user);
Enter fullscreen mode Exit fullscreen mode

For a delay:

SendWelcomeEmail::dispatch($user)->delay(now()->addMinutes(5));
Enter fullscreen mode Exit fullscreen mode

4️⃣ Processing the Queue
Before running jobs, you must start the queue worker:

php artisan queue:work
Enter fullscreen mode Exit fullscreen mode

This will continuously process new jobs in the queue.

For database queues, you need to create the table:

php artisan queue:table
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

If you want the worker to restart automatically after crashes, use:

php artisan queue:restart
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Queue Management Tips

  • Retry Failed Jobs – If a job fails, you can retry it:
php artisan queue:retry all
Enter fullscreen mode Exit fullscreen mode
  • Monitor Jobs – Use Horizon for Redis-based queues:
php artisan horizon
Enter fullscreen mode Exit fullscreen mode

🎯 Conclusion
Laravel queues make your application faster and more responsive by handling heavy tasks in the background. Start simple with database queues, then move to Redis or SQS for better scalability.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly β€” using the tools and languages you already love!

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay