Laravel interview question · Most asked
Explain $tries, $backoff, $timeout and retry_after for Laravel jobs. How do they interact?
Summary
$tries is how many attempts a job gets, $backoff how long to wait between attempts, $timeout how long one attempt may run before the worker kills it, and the connection's retry_after how long the queue waits before assuming a job was lost and handing it out again.
Longer Answer
retry_after (in config/queue.php) must be longer than the longest job $timeout. If it is shorter, the queue releases a job that is still running, and a second worker runs it concurrently: duplicate emails, double charges.
$backoffcan be an array for exponential backoff:[10, 60, 300].retryUntil()sets a deadline instead of a count.$maxExceptionsstops retrying after N unhandled exceptions even if releases are allowed.failed(Throwable $e)runs after the final failure, for alerts or compensation.$this->release(30)puts the job back deliberately (for example when rate-limited) and counts as an attempt.
Since any job can run more than once, jobs must be idempotent.
Key Points
retry_aftermust exceed$timeout.- Backoff arrays give exponential delays.
failed()for final-failure handling.- Every job may run twice: make it idempotent.
Deep dive: Queues & Background Processing in Depth
Explanation
What interviewers look for
The retry_after versus timeout interaction. Candidates who have debugged duplicate job execution know it.
Follow-up questions
- How would you make a 'charge customer' job idempotent?
More most-asked Laravel questions
- How do you validate requests in Laravel? Why use Form Requests?
- Gates or policies: how does authorization work in Laravel?
- What is mass assignment, and how do $fillable and $guarded protect against it?
- How do database transactions work in Laravel, and what pitfalls should you know?
- Which Artisan commands would you run when deploying Laravel to production, and why?
- What is the service container, and how does dependency injection work in Laravel?
- What is the N+1 query problem in Eloquent and how do you prevent it?
- How do queues work in Laravel?