Question

How Do I Set Up a Queue in Laravel on DigitalOcean?

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?


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
October 13, 2024
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! 🚀

Laravel 1-Click Droplet

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.

DigitalOcean Managed Redis

To set up Redis for queues in Laravel:

  1. First, install Redis locally if you aren’t using a Managed Redis service:

    sudo apt-get install redis-server
    
  2. Update your .env file to set Redis as the queue driver:

    QUEUE_CONNECTION=redis
    
  3. 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.

  1. Install Horizon via Composer:

    composer require laravel/horizon
    
  2. Publish the Horizon configuration:

    php artisan horizon:install
    
  3. Finally, launch Horizon:

    php artisan horizon
    

Laravel Horizon Documentation

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

KFSys
Site Moderator
Site Moderator badge
October 14, 2024

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

  1. Install Redis on Your Droplet: Run the following commands to install Redis:
sudo apt update
sudo apt install redis-server

After installation, make sure Redis is running:

sudo systemctl status redis
  • You can enable Redis to start automatically on boot:
sudo systemctl enable redis
  • Install Redis PHP Extension: Install the Redis extension for PHP to allow Laravel to communicate with Redis:
sudo apt install php-redis
  • Configure Laravel for Redis Queues: Open your .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:*

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.

  1. Set Up the Database Queue Table: Run the migration to create the jobs table:
php artisan queue:table
php artisan migrate
  1. Configure Laravel for Database Queues: Open your .env file and set the queue connection to database:
QUEUE_CONNECTION=database
  1. Start Laravel Queue Worker: Similar to Redis, you can start processing jobs with:
php artisan queue:work

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:

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.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more