Performance: Job retries and failure
This affects backend job processing speed and resource usage, indirectly impacting frontend responsiveness and user experience.
Jump into concepts and practice - no test required
public function handle() {
try {
// job logic
} catch (\Exception $e) {
$this->release(30); // retry after 30 seconds delay
}
}public function handle() {
try {
// job logic
} catch (\Exception $e) {
$this->release(0); // immediate retry without delay
}
}| Pattern | CPU Usage | Queue Pressure | Retry Delay | Verdict |
|---|---|---|---|---|
| Immediate retry on failure | High (CPU spikes) | High (queue congestion) | None | [X] Bad |
| Delayed retry with backoff | Moderate (smoothed spikes) | Moderate (spaced queue) | Yes (e.g., 30s) | [OK] Good |
| No failure handling | Wasted CPU on doomed jobs | High (retries continue) | N/A | [X] Bad |
| Failure logging and alerting | Efficient CPU use | Low (stops retries) | N/A | [OK] Good |
$tries property in a job class control?$tries in Laravel jobs$tries property defines how many times Laravel will attempt to run the job if it fails.$tries controls retry attempts [OK]$tries as a public property to set retry limits.$tries to work properly.public $tries [OK]class SendEmailJob implements ShouldQueue {
public $tries = 3;
public function handle() {
// code that may fail
throw new \Exception('Failed');
}
public function failed() {
// handle failure
Log::info('Job failed permanently');
}
}failed()?$tries works$tries property sets the maximum number of times the job will be attempted, including the initial attempt.$tries = 3, Laravel attempts the job 3 times total before calling failed().$tries = 3 [OK]class ProcessOrder implements ShouldQueue {
public $tries = 2;
public function handle() {
// some code
throw new \Exception('Error');
}
public function failed() {
// log failure
Log::error('Order processing failed');
}
}failed() method never be called after job failures?failed()failed() to accept an Exception parameter to receive failure details.failed() lacks the Exception parameter, Laravel may not call it properly after retries.failed() method must accept an Exception parameter -> Option Dfailed(Exception $exception) required [OK]$tries to limit retry attemptspublic $tries = 3; ensures the job retries 3 times on failure.failed(Exception $exception) to handle final failurefailed() with the Exception parameter allows sending email after all retries fail.public $tries = 3; and define a failed(Exception $exception) method to send the email notification -> Option C