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!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
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:
Update your
.env
file to set Redis as the queue driver:Start the queue worker:
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:
Publish the Horizon configuration:
Finally, launch 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:
Option 1: Using Redis for Queues
After installation, make sure Redis is running:
.env
file and update the queue connection to use Redis:Set Up Redis Queue Driver: Laravel uses the
redis
configuration defined inconfig/database.php
. By default, Redis is already set up in Laravel. You can modify Redis settings in theconfig/queue.php
file if needed.Start Laravel Queue Worker: You can start processing jobs by running:
If you want to run the worker in the background (so it continues after you log out), you can use something like
supervisor
orsystemd
:Install Supervisor:
Create a Supervisor Configuration File:
Add the following content to this file:
Replace
/path-to-your-project
andyour-username
accordingly. Then start Supervisor:Option 2: Using PostgreSQL for Queues
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:.env
file and set the queue connection todatabase
:Again, you should set this up to run as a background service using Supervisor or a similar tool.
Best Practices
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:
Then publish and configure 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.