You've successfully deployed your Laravel application on Heroku. But then you hit a roadblock: your queue workers won't start the way they do on traditional Apache or Nginx servers. If you've been struggling with this, you're not alone.
Here's a straightforward guide to setting up Laravel queue workers on Heroku so your background jobs run smoothly.
Understanding the Heroku Approach
Heroku uses a different architecture than traditional servers. Instead of SSH access and long-running processes, Heroku relies on dynos (lightweight containers) and a Procfile to define how your application runs.
Step 1: Configure Your Procfile
In the root directory of your Laravel project, you'll need a Procfile that tells Heroku which processes to run. Add the following line to define your queue worker:
worker: php artisan queue:work --tries=3 --timeout=90
Let's break this down:
-
worker: The name of your process type (you can name it anything, but "worker" is conventional) -
php artisan queue:work: The Laravel command that processes queued jobs -
--tries=3: Optional flag to retry failed jobs up to 3 times -
--timeout=90: Optional flag to set a maximum execution time of 90 seconds per job
You can customize these flags based on your application's needs. Other useful options include:
-
--sleep=3: Seconds to wait before polling for new jobs -
--queue=high,default: Specify which queues to process and their priority -
--max-jobs=1000: Maximum number of jobs to process before restarting
Step 2: Scale Your Worker Dyno
Once your Procfile is configured and your changes are deployed to Heroku, you need to provision a worker dyno. Run this command from your terminal:
heroku ps:scale worker=1 --app your-heroku-app
This tells Heroku to spin up one worker dyno for your application. You can scale this up or down depending on your queue volume:
# Scale to 3 workers for higher throughput
heroku ps:scale worker=3 --app your-heroku-app
# Scale down to 0 to stop processing queues
heroku ps:scale worker=0 --app your-heroku-app
Step 3: Verify Your Worker is Running
Check that your worker dyno is active:
heroku ps --app your-heroku-app
You should see output showing both your web and worker dynos running.
Important Considerations
Cost: Worker dynos consume additional dyno hours and may incur costs depending on your Heroku plan. Monitor your usage in the Heroku dashboard.
Queue Driver: Make sure your Laravel application is configured to use a persistent queue driver like Redis or a database. The sync driver won't work properly with background workers.
Monitoring: Use heroku logs --tail --ps worker to monitor your queue worker logs and troubleshoot any issues.
That's it! Your Laravel queues should now be processing jobs in the background on Heroku. If you run into issues, check your logs and verify your queue configuration in config/queue.php.

Top comments (0)