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.
Queues in production · Lesson 1
Keep the request short
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.
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.
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?