laravelmastery
SnippetsArticlesSeriesNews

Queues in production · Lesson 1 of 3

Queues in production · Lesson 1

Keep the request short

Hand the work offQueues

Paying for order 1042 should return before the card provider answers. The request that takes a payment should record the order and hand the rest to a job. Charging the card, writing the PDF and sending the receipt can wait. The person who clicked Pay should get a response before any of that finishes.

Video

01 Leave the slow work for a worker

Validate the input, store the order, dispatch the job, and redirect. The controller does not call the payment provider.

Example.phpPHP
class Example
{
    public function store(PayOrderRequest $request): RedirectResponse
    {
        $order = Order::query()->create([
            'number' => $request->validated('number'),
            'status' => 'pending',
        ]);

        MarkOrderPaid::dispatch($order->id, (string) Str::uuid());

        return redirect()->route('orders.show', $order);
    }
}

A worker can take 30 seconds and retry. A browser request should not. If the provider is down, the order still exists and the job can try again. The person sees the order page, not a timeout.

02 The page can render while the job is still waiting

The show action does not ask the provider whether the card went through. It reads the row the request just stored. pending means the job has not finished, or has not started. The template can say the payment is being taken.

Example.phpPHP
class Example
{
    public function show(Order $order): View
    {
        return view('orders.show', [
            'order' => $order,
            'pending' => $order->status === 'pending',
        ]);
    }
}

A refresh later shows paid, once the worker has written that status. If the page cannot be useful until that write has happened, the work was not really handed off. The order number is enough to render. The receipt PDF is not, and it stays on the job. The person can leave the page. The job does not need them to stay.

Recap

You can record the order and redirect before the card, the PDF, or the receipt finishes.

  • The browser gets the order page while the status is still pending.
  • A provider outage leaves a row the job can retry.
  • If the page is useless until the job finishes, the work was not handed off.

Check yourself · 3 questions

Question 1 of 3The provider times out for 30 seconds. Where should that wait happen?

The request returns before the provider answers. Next, the class that does the work.

← PreviousOverview

On this page

  • Leave the slow work for a worker
  • The page can render while the job is still waiting
  • Recap
  • Check yourself

Queues in production

  • 1 Overview

Hand the work off

  • 2 Keep the request short
  • 3 The job class
  • 4 Dispatch from actions, not controllers

When it goes wrong

  • 5 Retries, backoff and timeouts
  • 6 Failed jobs you can replay
  • 7 Unique jobs and WithoutOverlapping
  • 8 Batches and then()

Run it for real

  • 9 Make the job safe to run twice
  • 10 Horizon in production

Queues in production

Lessons

  1. 01Overview

Hand the work off

  1. 02Keep the request short
  2. 03The job class
  3. 04Dispatch from actions, not controllers

When it goes wrong

  1. 05Retries, backoff and timeouts
  2. 06Failed jobs you can replay
  3. 07Unique jobs and WithoutOverlapping
  4. 08Batches and then()

Run it for real

  1. 09Make the job safe to run twice
  2. 10Horizon in production

Get the Friday email

One snippet and the week’s Laravel news, in your inbox every Friday morning. Unsubscribe whenever you like.

laravelmastery

Practical Laravel learning for working developers, made in the UK.

Learn

  • Snippets
  • Articles
  • Series

Stay current

  • News
  • Friday email
  • RSS feed

Company

  • About
  • Privacy
  • Terms
© 2026 Laravel Mastery. Independent and not affiliated with Laravel Holdings Inc.Designed and built by Half Shell Studios

We remember the videos you watch, where you left off, and which series lessons you mark as read, against this connection’s address. Nothing is stored until you agree.