0
0
Laravelframework~8 mins

Dispatching jobs in Laravel - Performance & Optimization

Choose your learning style9 modes available
Performance: Dispatching jobs
HIGH IMPACT
This affects how quickly the main web request responds by offloading work to background processes, improving user experience and server responsiveness.
Handling time-consuming tasks during a web request
Laravel
<?php
use App\Jobs\ProcessLargeFile;

public function store(Request $request) {
    ProcessLargeFile::dispatch($request->file);
    return response()->json(['status' => 'queued']);
}

// Job class handles processing asynchronously
Dispatching the job queues the task to run in the background, allowing the HTTP response to return immediately.
📈 Performance GainReduces blocking time to under 100ms, improving INP and user experience.
Handling time-consuming tasks during a web request
Laravel
<?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
}
Running heavy tasks synchronously blocks the HTTP response, causing slow page loads and poor user experience.
📉 Performance CostBlocks rendering for 10+ seconds, increasing INP and causing user frustration.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous heavy task in requestN/ABlocks main threadDelays paint[X] Bad
Dispatch job to queueN/ANo blockingFast paint[OK] Good
Rendering Pipeline
Dispatching jobs moves heavy processing out of the critical rendering path, so the browser can receive and paint the page faster.
Network
JavaScript Execution
Rendering
⚠️ BottleneckSynchronous heavy processing during request delays network response and blocks rendering.
Core Web Vital Affected
INP
This affects how quickly the main web request responds by offloading work to background processes, improving user experience and server responsiveness.
Optimization Tips
1Always dispatch heavy or long-running tasks to background jobs to keep HTTP requests fast.
2Avoid synchronous processing of large files or external API calls during web requests.
3Monitor performance with DevTools to ensure no long blocking tasks delay rendering.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of dispatching jobs in Laravel?
AIt moves heavy tasks out of the HTTP request to improve response time.
BIt reduces the size of the JavaScript bundle sent to the browser.
CIt improves CSS selector matching speed.
DIt decreases the number of DOM nodes rendered.
DevTools: Performance
How to check: Record a performance profile while triggering the request. Look for long tasks blocking the main thread.
What to look for: Long blocking tasks over 1 second indicate synchronous heavy processing; short tasks indicate good async dispatch.