Setup Laravel Queue with Redis and Supervisor
Summary:
Use Redis for queues and configure Supervisor for running workers.
Queueing in Laravel provides an efficient way to defer time-consuming tasks, such as sending emails or processing uploads, to a background job. Leveraging Redis as the queue driver offers speed and reliability, while Supervisor ensures your queue workers stay running and are automatically restarted if they fail. This guide will walk you through setting up Laravel Queue with Redis and Supervisor on a typical Linux server.
Prerequisites
- A Laravel application installed.
- SSH access to your Linux server (Ubuntu examples provided).
- Redis installed and running.
- Supervisor installed.
- Basic knowledge of the terminal.
1. Install and Configure Redis
Install Redis:
sudo apt-get update
sudo apt-get install redis-server
Start and Enable Redis:
sudo systemctl start redis-server
sudo systemctl enable redis-server
Use redis-cli ping
to ensure Redis is running (should reply with PONG
).
Install Redis PHP Extension:
sudo apt-get install php-redis
2. Configure Laravel to Use Redis Queues
Update your .env
:
QUEUE_CONNECTION=redis
CACHE_DRIVER=redis
SESSION_DRIVER=redis
Make sure config/queue.php
includes the redis
connection (it does by default in Laravel).
Install Laravel Redis Package:
composer require predis/predis
Optional:
You can use the phpredis
extension (already installed), but predis/predis
is a pure PHP library for Redis.
3. Create and Dispatch Jobs
Create a Job:
php artisan make:job SendWelcomeEmail
Edit app/Jobs/SendWelcomeEmail.php
:
public function handle()
{
// Logic to send the welcome email
}
Dispatch a Job:
use App\Jobs\SendWelcomeEmail;
SendWelcomeEmail::dispatch($user);
4. Run the Laravel Queue Worker
You can test locally:
php artisan queue:work redis
However, on production, you want workers running in the background and managed reliably — that's where Supervisor comes in.
5. Install and Configure Supervisor
Install Supervisor:
sudo apt-get install supervisor
Create a Supervisor Configuration File for Laravel Queue Worker:
sudo nano /etc/supervisor/conf.d/laravel-worker.conf
Sample Configuration:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/project/artisan queue:work redis --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/your/project/storage/logs/laravel-worker.log
stopwaitsecs=3600
Replace /path/to/your/project/
with your actual project path.
Key configuration options:
numprocs=1
: Number of worker processes. Use more for heavy workloads.user=www-data
: Ensure this matches your web server user.stdout_logfile
: Worker output logs go here.
6. Start and Monitor Supervisor
After adding the configuration:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
Check worker status:
sudo supervisorctl status
Workers will now start automatically on boot and restart if they crash.
7. Useful Commands
-
Restart all workers:
sudo supervisorctl restart all
-
View worker logs:
tail -f /path/to/your/project/storage/logs/laravel-worker.log
-
Check current jobs in Redis queue:
Usephp artisan queue:failed
andphp artisan queue:retry
.
8. Additional Recommendations
- Monitor your queues: You can use Laravel Horizon for a beautiful dashboard and detailed metrics.
- Handling failed jobs: Configure a
failed_jobs
table and monitor failed jobs. - Scale workers: Increase
numprocs
for more workers based on server capacity. - Security: Never run Supervisor as root for queue workers.
- Environment-specific paths: Double-check
command
andstdout_logfile
paths.
Conclusion
Setting up Laravel Queue with Redis as the backend and managing workers with Supervisor is robust, scalable, and production-ready. With this configuration, your application's heavy tasks can be processed reliably in the background, keeping user experiences fast and servers happy.
Have questions or want to share your workflow? Drop a comment below!