Laravel interview question · Most asked
How do queues work in Laravel?
Summary
A job class implementing ShouldQueue is serialised and pushed to a queue backend (Redis, database, SQS, Beanstalkd). Worker processes (php artisan queue:work) pop jobs and run their handle method, retrying on failure and moving jobs that keep failing to the failed_jobs table.
Longer Answer
Dispatching (ProcessPodcast::dispatch($podcast)) stores the job payload. Eloquent models are serialised as their IDs (SerializesModels) and re-fetched when the job runs, so the job sees current data (or fails if the row is gone).
Workers are long-running PHP processes managed by Supervisor or systemd (or Horizon for Redis). Key options: queue names and priorities (--queue=high,default), --tries, --backoff, --timeout, --max-jobs / --max-time to recycle workers, and --memory.
Because workers are long-lived, they keep code and static state in memory: restart them on deploy (queue:restart) and avoid state leaking between jobs. The sync driver runs jobs inline, useful in tests and local development.
Key Points
- Jobs are serialised; models by ID.
- Long-running workers under a supervisor.
- Tries, backoff, timeout, failed_jobs.
- Restart workers on deploy.
Deep dive: Queues & Background Processing in Depth
Explanation
What interviewers look for
Operational understanding: supervisors, restarts, failure handling, and that workers are long-lived processes.
Follow-up questions
- What happens to a job if the worker is killed mid-job?
- How do you prioritise some jobs over others?
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?
- A Laravel endpoint is slow. How do you find and fix the cause?