I’m currently working on a Laravel project hosted on a DigitalOcean Droplet, and I need to process some background tasks.
I’ve heard about using queues in Laravel for handling jobs asynchronously, but I’m not sure how to set them up on my server. What’s the best way to configure a queue system, like Redis or shall I use a Postgres database, on a DigitalOcean Droplet? Any specific steps or best practices I should follow?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
Hey there! 👋
Setting up queues in Laravel is an awesome way to handle background tasks like sending emails, processing jobs, or any time-consuming operations that you don’t want blocking your app’s performance.
If you haven’t already, I’d highly recommend using the Laravel 1-Click Droplet from the DigitalOcean Marketplace. It sets up Laravel with everything you need to get started in no time. Super easy! 🚀
For Laravel queues, you have a few different drivers available (Redis, Beanstalkd, SQS, etc.). On DigitalOcean, a popular and high-performance setup is using Redis as your queue driver and PostgreSQL as your database.
Redis is fast and lightweight, making it perfect for managing queues. You can either install Redis directly on your Droplet or go with Managed Redis, which saves you the headache of managing and scaling Redis yourself.
To set up Redis for queues in Laravel:
First, install Redis locally if you aren’t using a Managed Redis service:
sudo apt-get install redis-server
Update your .env
file to set Redis as the queue driver:
QUEUE_CONNECTION=redis
Start the queue worker:
php artisan queue:work
Running the queue worker will start processing jobs from your Redis queue. You can also run the worker in the background using Supervisor to keep it running.
Alternatively, you can use Laravel Horizon for advanced queue monitoring and management. Laravel Horizon works with Redis and gives you a great way to manage your queues and also gives advanced control, monitoring, and insights into your job queues. Horizon provides a nice dashboard to monitor your queue workers, retry jobs, and scale dynamically based on your workload.
Install Horizon via Composer:
composer require laravel/horizon
Publish the Horizon configuration:
php artisan horizon:install
Finally, launch Horizon:
php artisan horizon
Let me know how it goes! Whether you’re setting up Redis queues or diving into Horizon, this setup will keep your app fast and efficient
- Bobby
Heya,
Using Laravel queues is an excellent way to handle background tasks asynchronously. Laravel supports several queue backends, such as Redis, database, and others like Amazon SQS. For your case on a DigitalOcean droplet, Redis is a popular choice due to its performance, but you can also use PostgreSQL if that fits better with your existing infrastructure.
Here’s how you can configure queues using Redis or a Postgres database on your droplet:
sudo apt update
sudo apt install redis-server
After installation, make sure Redis is running:
sudo systemctl status redis
sudo systemctl enable redis
sudo apt install php-redis
.env
file and update the queue connection to use Redis:QUEUE_CONNECTION=redis
Set Up Redis Queue Driver: Laravel uses the redis
configuration defined in config/database.php
. By default, Redis is already set up in Laravel. You can modify Redis settings in the config/queue.php
file if needed.
Start Laravel Queue Worker: You can start processing jobs by running:
php artisan queue:work
If you want to run the worker in the background (so it continues after you log out), you can use something like supervisor
or systemd
:
Install Supervisor:
sudo apt install supervisor
Create a Supervisor Configuration File:
sudo nano /etc/supervisor/conf.d/laravel-worker.conf
Add the following content to this file:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path-to-your-project/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=your-username
numprocs=1
redirect_stderr=true
stdout_logfile=/path-to-your-project/worker.log
Replace /path-to-your-project
and your-username
accordingly. Then start Supervisor:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
If you want to use PostgreSQL for your queues, Laravel’s database
queue driver works by storing the jobs in a database table.
jobs
table:php artisan queue:table
php artisan migrate
.env
file and set the queue connection to database
:QUEUE_CONNECTION=database
php artisan queue:work
Again, you should set this up to run as a background service using Supervisor or a similar tool.
Use Supervisor for Long-Running Workers: As mentioned earlier, using Supervisor or another process control tool will keep your worker running continuously, ensuring that background tasks are processed without interruption.
Optimize Worker Settings: You can customize worker settings such as --sleep
, --timeout
, and --tries
to handle specific workloads and optimize resource usage.
Monitor Queues: Laravel Horizon is a great tool for monitoring Redis queues, providing a dashboard for tracking jobs and workers. You can set it up for Redis by running:
composer require laravel/horizon
Then publish and configure Horizon:
php artisan horizon:install
php artisan horizon
Either Redis or Postgres can work well for your queue management. Redis is usually preferred for its speed and efficiency, while PostgreSQL is better suited if you’re already using it and prefer a more straightforward setup without introducing another service.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.