Doorgaan naar content

Redis as a queue driver for Statamic

You can run a Redis queue to optimise performance in the control panel when using the Git integration. With a queue driver you can also push other tasks, like generating assets or warming the static cache, into the background. A queue performs jobs in the background. This makes sure that you don't have to wait for tasks when browsing the control panel or when you're deploying your website. This article explains how you can make a Redis queue work for multiple Statamic sites on one server.

Configure Statamic

For Statamic to use a Redis queue you only have to change some environment values.

The environment variables involved.

Set the QUEUE_CONNECTION to redis and instruct Redis which database to use with REDIS_DATABASE. When you configure your server with a tool like Ploi or Forge, Redis is automatically installed and it comes with 16 databases by default. Every site on your server has to use its own database. This means for every site you pick a number between 0 and 15 and you don't use the same database twice.

When your server grows and hosts more sites it can be a chore to keep track of which site is using which database number and figuring out which numbers are currently available. Ploi has a handy notes field in the servers settings tab where you could keep track of that.

A screenshot of Ploi server notes.
The server notes field in Ploi.

If you want to increase the number of available databases. SSH into your server, open /etc/redis/redis.conf and find the line databases 16. Increase the number, save and reboot.

Starting a queue worker

For each site on your server using Redis you have to start a queue worker. This is a server process that keeps track of all the jobs entering the queue and executing them. Starting a worker is very simple within Ploi. This works similar when using Forge. Go to the queue tab for your current site and configure the worker.

Configuring a queue worker in Ploi.
Configuring a queue worker in Ploi.

Set the the connection to redis and the environment to your current environment. Hit the button and that's it.

Make sure you restart your queue worker whenever you deploy a new version of your website. On Ploi you can add {SITE_PHP} artisan queue:restart to your deploy script and and on Forge $FORGE_PHP artisan queue:restart.

Failing tasks

Whenever tasks fail it can be a little tricky to debug. Redis expects your site to run a MySQL database so it fills up your Laravel log with SQL connection errors. The actual reason your job is failing won't be listed. Jonas Siewertsen wrote an excellent and lightweight package you can install that saves the actual failed queue job errors in a log file. As an alternative you could also install Laravel Horizon. It comes with a GUI where you can see all kinds of information about running queues. You have to install this on a per site basis.

That's how to use a Redis queue together with Statamic. If you have any questions or thoughts please let me know.

Responsive images with Statamic, Tailwind and Glide

  • Statamic
  • Peak
  • Toegankelijkheid
  • Antlers