Home About Skills Products
Work
Projects Services Experience
Learn
Tutorials Courses Blogs Resources
Contact
Blog · Laravel

Queues and Jobs in Laravel: Processing Tasks in the Background

Queues and Jobs in Laravel: Processing Tasks in the Background
Advertisement

Anything that talks to the outside world — sending an email, calling a third-party API, generating a report — shouldn't happen while a user is sitting there waiting for a page to load. Laravel's queue system exists exactly for this.

Why Queue at All

A request that sends a welcome email synchronously is only as fast as your SMTP provider's slowest day. Dispatch it to a queue instead, and the HTTP response returns immediately while a background worker handles the email whenever it gets to it.

Creating and Dispatching a Job

php artisan make:job SendWelcomeEmail
class SendWelcomeEmail implements ShouldQueue
{
    use Queueable;

    public function __construct(public User $user) {}

    public function handle(): void
    {
        Mail::to($this->user)->send(new WelcomeMail($this->user));
    }
}
SendWelcomeEmail::dispatch($user);

That single dispatch() call serializes the job and pushes it onto whatever queue driver you've configured — database, Redis, or SQS.

Running a Worker

Jobs sit in the queue until a worker process picks them up:

php artisan queue:work --tries=3

--tries=3 means a failing job (a timeout, a third-party API being down) retries up to three times before landing in the failed_jobs table for you to inspect.

Delaying and Chaining Jobs

Need to send a follow-up email exactly 24 hours later? Delay the dispatch instead of building your own scheduler:

SendFollowUpEmail::dispatch($user)->delay(now()->addDay());

Or chain several jobs that must run in strict order, where each only starts after the previous succeeds:

Bus::chain([
    new ProcessPayment($order),
    new GenerateInvoice($order),
    new SendInvoiceEmail($order),
])->dispatch();

Production Considerations

  • Run workers under Supervisor (or an equivalent process manager) so a crashed worker restarts automatically.
  • Use Redis over the database driver once you have real traffic — it's dramatically faster under load.
  • Set a sensible --timeout so one stuck job doesn't block the entire worker indefinitely.

Queues are the single highest-leverage change you can make to a Laravel app's perceived speed — most of what feels "slow" in a web app is actually something that should never have been synchronous in the first place.

Laravel for Beginners: Setting Up Your First Project

Laravel for Beginners: Setting Up Your First Project

A complete first-day guide to Laravel — installation, folder structure, your first route, and your first migration.

Building a REST API with Laravel: A Complete Guide

Building a REST API with Laravel: A Complete Guide

API routes, resources, Form Request validation, correct status codes, and Sanctum auth — a real, production-reasonable Laravel API.

Laravel Eloquent Relationships Explained with Real Examples

Laravel Eloquent Relationships Explained with Real Examples

One-to-many, many-to-many, polymorphic, and the N+1 query trap that catches almost every Laravel developer at least once.

Advertisement
Esc