Performance: Why background processing improves performance
Background processing improves page load speed and responsiveness by offloading slow tasks from the main request cycle.
Jump into concepts and practice - no test required
<?php use App\Jobs\SendWelcomeMail; Route::post('/submit', function () { // Dispatch slow task to background SendWelcomeMail::dispatch('user@example.com'); return response('Done'); });
<?php Route::post('/submit', function () { // Slow task done inline Mail::to('user@example.com')->send(new WelcomeMail()); return response('Done'); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous slow tasks in request | Minimal | 0 | Blocks paint until done | [X] Bad |
| Background jobs for slow tasks | Minimal | 0 | Paints immediately, tasks run later | [OK] Good |
::dispatch() to send jobs to the queue for background processing.ProcessReport::dispatch(); uses correct syntax. dispatchNow(new ProcessReport()); runs job immediately, not in background. ProcessReport::run(); is invalid method. dispatchLater(ProcessReport); is not Laravel syntax.ProcessReport::dispatch(); -> Option AProcessReport::dispatch();
return response()->json(['status' => 'done']);
ProcessReport::dispatch() queues the job to run later, not blocking the current request.response()->json(), so user does not wait for the job.ProcessReport::dispatch;
ProcessReport::dispatch; without parentheses, so PHP treats it as a property, causing error.