How do you use Redis with Laravel queues?

How do you use Redis with Laravel queues?

Here’s a streamlined process to use Redis with Laravel queues:

1. Install Redis and Predis

Install Redis on your system (e.g., brew install redis for macOS). Then install the Redis package for Laravel if needed:

Example

composer require predis/predis

2. Configure Redis in Laravel

Set Redis as your queue driver in the .env file:

Example

QUEUE_CONNECTION=redis

In config/database.php, ensure Redis is properly configured:

Example

<?php
'redis' => [
    'client' => 'predis', // Or 'phpredis'
    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_DB', 0),
    ],
],
?>

3. Create and Dispatch a Job

Create a new job:

Example

php artisan make:job ExampleJob

Then, dispatch the job to the Redis queue:

Example

<?php
ExampleJob::dispatch()->onQueue('default');
?>

4. Start Queue Worker

Start processing the Redis queue:

Example

php artisan queue:work redis

Related Questions & Topics