How to use Redis as the queue driver in Laravel?

To use Redis as the queue driver in Laravel, you need to follow these steps:

  1. Install Redis: If you haven't installed Redis, you can do it by following the official Redis installation guide.

  2. Configure Laravel: Open your Laravel project and navigate to the .env file. Update the QUEUE_CONNECTION variable to use the Redis driver:

    QUEUE_CONNECTION=redis
  3. Configure Redis connection: Open the config/database.php file and locate the redis array. Ensure that the default connection is set to use Redis as the driver:

    'default' => env('REDIS_CONNECTION', 'redis'),

    Also, check the redis array and update the connection details such as host, port, password, etc., depending on your Redis server configuration:

    'redis' => [ 'client' => 'predis', 'options' => [ 'cluster' => 'redis', ], 'default' => [ 'scheme' => 'tcp', 'host' => env('REDIS_HOST', '127.0.0.1'), 'port' => env('REDIS_PORT', 6379), 'password' => env('REDIS_PASSWORD', null), 'database' => env('REDIS_DB', 0), ], ],
  4. Start Redis server: Make sure your Redis server is running.

  5. Create and run your queues: You can create your queues using Laravel's artisan command. For example, to create a new queue named emails, run the following command:

    php artisan queue:table php artisan migrate

    Then, you can dispatch jobs to the queue using Laravel's job dispatching functionality.

With Redis configured as the queue driver, Laravel will automatically use Redis to process jobs dispatched to the queues. You can use Laravel's queue worker to continuously process the queued jobs by running the following command:

php artisan queue:work --queue=emails,default

This will start the queue worker and process jobs from both the emails queue and the default queue. You can modify the --queue option to specify which queues you want to process.