Performance: Dispatching jobs
This affects how quickly the main web request responds by offloading work to background processes, improving user experience and server responsiveness.
Jump into concepts and practice - no test required
<?php
use App\Jobs\ProcessLargeFile;
public function store(Request $request) {
ProcessLargeFile::dispatch($request->file);
return response()->json(['status' => 'queued']);
}
// Job class handles processing asynchronously<?php
// Controller method
public function store(Request $request) {
// Heavy task directly in request
$this->processLargeFile($request->file);
return response()->json(['status' => 'done']);
}
private function processLargeFile($file) {
// Simulate heavy processing
sleep(10);
// Process file logic
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous heavy task in request | N/A | Blocks main thread | Delays paint | [X] Bad |
| Dispatch job to queue | N/A | No blocking | Fast paint | [OK] Good |
SendEmailJob in Laravel?::dispatch() on the job class to send it to the queue.SendEmailJob::dispatch(); uses SendEmailJob::dispatch(); which is the correct Laravel syntax.SendReportJob::dispatch(); return 'Report sent';
dispatch SendEmailJob();
::dispatch() on the job class, not by calling dispatch as a function.ProcessOrderJob with an order ID parameter 123. Which code correctly dispatches this job with the parameter?ProcessOrderJob::dispatch(123); calls ProcessOrderJob::dispatch(123); which passes 123 to the job constructor correctly.