How to Use Redis for Queuing in Laravel 5

by Noor Ali  February 12, 2016

What is Redis?

Redis is an in-memory but persistent on disk database, that represents a different trade-off where very high write and read speed is achieved with the limitation of data sets that can’t be larger than the memory. Once larger than memory, it starts trading the data from the backend SQL databases by pushing and pulling the data. This, in some scenarios, can bring out great speed to the overall system.

For example, a number of people are liking or commenting on a post. While concurrent requests reach the server, it’s impossible to process all of them simultaneously at high speed because accessing the database requires a little section of the overall process time.


We tackle this by pushing the customer’s “like” request in a queue and show him the updated page while database is not updated. After sometime when the jobs in the queues start firing, his job will then be fulfilled and database will be updated at the back-end servers.

This doesn’t explain the whole queuing structure, but the example was just to give an idea of how Redis can be used.

Related: Learn How to Use Laravel Horizon for Redis Queue

How To Setup Redis.

  • Make sure you have Redis installed at your local server. You can check it by following the steps below, but if you are working on Cloudways, you can skip this step and follow the next one.
  • Now to check whether your Redis server is working or not.

    Redis Connection
    or if you are connected you will get this:

    redis for laravel

This shows the Redis is working fine.

How To Setup Redis Application on Your Cloudways Account:

  • Log in to your Cloudways account. At first, you will see the summary screen. Press “Manage Server” button of your desired cloud server.Cloudways Console Admin Panel
  • Select the “Server Management” tab.Server Management
  • Scroll down to “Server Settings & Packages” section. Click the header to view the full details. Then, select the “Packages” tab.
    Server Setting and Packages
  • Inside the “Packages” tab, you will see the Redis option. Click on the pencil icon to change status.redis for laravel
  • Select “Yes” to enable Redis and press the “Save” button
  • You will then see a waiting screen for the server where Redis is being activated.

Changes Required to Your Project

  • Make sure composer.json that is in your root directory has mentioned the package required for Redis.
  • While deploying your code on git and pulling it on your server, make sure you deploy the empty project first and run composer install/update before deploying your code.
  • Some conflicts were found while running the composer update with codes that are already using dependencies found in composer.

How to Make Changes to Your Project

  • nano the (dot)env file that is in the root directory of your project and change these settings:
  • nano the queue.php file in the directory /config.. and make following changes
  • nano the session.php file
  • Some changes will occur on your code where you are calling the Redis class. So every time you get connection to Redis class you have to declare the location, and here it is:

    You can use  functions to make Queues for example in routes.php I configured my code like this:

Now Let’s See If Our Queue Is Working:

  • Make sure your application is pushing the jobs in the queue, you can take examples from the above codes.
  • Open Up two terminals:
  • In one of them run
  • You will get the number of jobs present in the queueScreenshotfrom2016-01-01130919
  • Now on the other terminal
  • You will get this as an outputScreenshotfrom2016-01-01131103
  • Now run lrange command againScreenshotfrom2016-01-01131442

Here you go! You’re done setting up your Redis queue successfully on Laravel 5.2. One can see the jobs moving up the queue as you call up the worker.

By Default:

Laravel offers “SYNC” queuing driver by default. It needs no installation, no changes in your code but if you are switching back from the above configurations make sure you make following changes.

  • nano the (dot)env file that is in the root directory of your project and change these settings:
  • nano the queue.php file in the directory /config.. and make following changes
  • Note that if you run the lrange command in your redis command line interface you won’t be seeing any more jobs entering the queue (As sync driver will be handling all of them)

To generate database queue drivers you have to look up for the documentation of Laravel click here.

Create Laravel apps without the worry of server management.

Deploy your app on optimized PHP hosting servers for Laravel.

About Noor Ali

Noor Ali is an Associate Software Engineer at Cloudways. He loves to solve technical problems through programming and mathematics.

Stay Connected:

You Might Also Like...