Laravel queues: move slow work to the background

A customer places an order on your Laravel site. Before the thank-you page appears, the app sends a confirmation email, generates a PDF invoice, notifies the warehouse over an API, and updates a sales report. Each step adds a second or two, and if the email service is slow, the customer stares at a spinner. Laravel queues solve this: the request records what needs doing, responds immediately, and a background worker handles the slow parts.

What a queue actually does

A queue is a list of jobs waiting to run. Your web request dispatches a job, which is stored in a database table, Redis or another backend. A separate worker process picks jobs off the list and runs them one by one. The user gets a fast response, and the heavy lifting still happens within seconds.

Good candidates for a queue include sending email and SMS, image and video processing, PDF generation, calls to third-party APIs, imports and exports, and anything that does not need to finish before the page loads.

Create your first job

New Laravel applications use the database queue driver by default, and the jobs table migration is already included. Generate a job class with Artisan:

php artisan make:job SendOrderConfirmation

Laravel creates app/Jobs/SendOrderConfirmation.php. The job implements ShouldQueue, which tells Laravel to run it in the background, and its handle method holds the work:

namespace App\Jobs;

use App\Mail\OrderConfirmed;
use App\Models\Order;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Mail;

class SendOrderConfirmation implements ShouldQueue
{
    use Queueable;

    public $tries = 3;
    public $backoff = [10, 60, 300];

    public function __construct(public Order $order) {}

    public function handle(): void
    {
        Mail::to($this->order->customer_email)
            ->send(new OrderConfirmed($this->order));
    }
}

Then dispatch it from your controller, right after the order is saved:

SendOrderConfirmation::dispatch($order);

Passing the Order model is safe: Laravel stores only its ID in the queue and loads a fresh copy from the database when the job runs.

Run a worker

Jobs wait in the queue until a worker processes them. During development, run:

php artisan queue:work

You can give some work priority by naming queues. Dispatch urgent jobs with ->onQueue('high'), then start the worker with --queue=high,default so it always empties the high-priority queue first.

Retries and failed jobs

External services fail from time to time. In the example above, $tries = 3 lets the job run up to three times, and $backoff waits 10 seconds, then 60, then 300 between attempts. That alone absorbs most temporary outages.

If every attempt fails, Laravel records the job in the failed_jobs table instead of losing it. Two commands help here:

  • php artisan queue:failed lists failed jobs with the error message.
  • php artisan queue:retry all sends them back to the queue once the problem is fixed.

You can also add a failed() method to a job to alert your team or mark the order for manual follow-up.

Running queues in production

  • Keep workers alive with a process monitor. On a Linux server, Supervisor restarts queue:work if it crashes or the server reboots.
  • Restart workers on every deployment. Workers keep your code in memory, so run php artisan queue:restart after deploying or they will keep running the old version.
  • Move to Redis as traffic grows. The database driver is fine for small sites. For high volumes, Redis is faster, and Laravel Horizon adds a dashboard showing throughput, wait times and failures.
  • Keep jobs small and repeatable. A job may run more than once after a retry, so make sure running it twice does no harm, for example by checking whether the email was already sent.

The payoff

Queues are one of the simplest ways to make a Laravel application feel faster without new hardware. Pages respond in milliseconds, temporary failures retry on their own, and slow third-party services stop affecting your customers. Start by moving email sending into a job, measure the difference, and expand from there.

Building a Laravel application or need help making an existing one faster and more reliable? Talk to the Khaga Infotech team.

Category