Performance: Queue configuration
Queue configuration affects how background jobs are processed, impacting server response time and user experience by offloading tasks from the main request cycle.
Jump into concepts and practice - no test required
<?php
use IlluminateContractsQueueShouldQueue;
// Dispatch job to queue
public function upload(Request $request) {
$image = $request->file('image');
ProcessImageJob::dispatch($image->path());
return response()->json(['status' => 'queued']);
}
// Job class handles resizing asynchronously
class ProcessImageJob implements ShouldQueue {
public $path;
public function __construct($path)
{
$this->path = $path;
}
public function handle() {
$image = Image::make($this->path)->resize(2000, 2000)->save();
}
}
<?php
// In controller
public function upload(Request $request) {
// Process file upload and heavy image resizing synchronously
$image = $request->file('image');
$resized = Image::make($image)->resize(2000, 2000)->save();
return response()->json(['status' => 'done']);
}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous heavy task in request | Minimal | N/A | Blocks paint until task completes | [X] Bad |
| Asynchronous queue job processing | Minimal | N/A | Paints quickly, task runs in background | [OK] Good |
config/queue.php file?config/queue.php:
'connections' => [
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
],
]
What will happen if a job fails when using the 'database' queue driver?'default' => env('QUEUE_CONNECTION', 'redis') in config/queue.php but your jobs are not being processed. Which of these is the most likely cause?php artisan queue:work, not routes. Redis is a valid driver, so no need to switch to database. Laravel fully supports Redis queues.config/queue.php?