Why Queues Matter
Any request that doesn’t need to finish before you respond to the user — sending an email, resizing an image, calling a slow third-party API — is a candidate for a queue. Laravel’s queue system gives you a unified API over Redis, Amazon SQS, Beanstalkd, or even a database table, so you can defer that work without changing how you write it.
Creating Your First Job
Running php artisan make:job SendWelcomeEmail scaffolds a job class with a handle() method. Anything you put there runs asynchronously once the job is dispatched with SendWelcomeEmail::dispatch($user). Constructor arguments are serialized, so pass Eloquent models rather than raw data when possible — Laravel will re-fetch a fresh copy from the database when the job runs.
Choosing a Queue Driver
The database driver is fine for getting started, but Redis is the practical choice for production — it’s fast, supports delayed jobs natively, and works well with Laravel Horizon for monitoring. SQS is worth considering if you’re already deep in the AWS ecosystem and want a managed queue with no server to maintain.
Handling Failures Gracefully
Jobs fail. Network calls time out, third-party APIs return errors, and unexpected exceptions happen. Define a failed() method on your job to handle cleanup or alerting, and set sensible $tries and backoff properties so transient failures get retried without hammering a struggling downstream service.
Monitoring with Horizon
If you’re using Redis, Laravel Horizon gives you a dashboard for queue throughput, failed jobs, and wait times — invaluable once you have more than a handful of job types running in production. It also lets you configure balanced, simple, or false worker strategies per queue, so latency-sensitive jobs don’t get stuck behind bulk processing tasks.
Practical Tips
- Keep jobs small and focused — one job, one responsibility.
- Use separate queues for different priorities (e.g.,
emailsvsreports) so a backlog in one doesn’t starve another. - Make jobs idempotent where possible, since retries mean a job might run more than once.
- Set reasonable timeouts so a stuck job doesn’t block a worker indefinitely.
Queues are one of the highest-leverage tools in Laravel for keeping response times fast and your application resilient to slow dependencies. Once you’ve moved your first few slow operations off the request cycle, it’s hard to imagine building without them.