0
0
LaravelHow-ToBeginner · 4 min read

How to Use Redis for Queue in Laravel: Simple Guide

To use Redis for queues in Laravel, set the QUEUE_CONNECTION to redis in your .env file and configure Redis in config/database.php. Then, dispatch jobs normally and run the queue worker with php artisan queue:work redis.
📐

Syntax

Laravel uses the QUEUE_CONNECTION environment variable to select the queue driver. Setting it to redis tells Laravel to use Redis for queue management.

The main parts are:

  • .env: Set QUEUE_CONNECTION=redis
  • config/database.php: Redis connection settings
  • Queue jobs: Use Laravel's dispatch() or dispatchNow() methods
  • Run worker: Use php artisan queue:work redis to process jobs
bash
QUEUE_CONNECTION=redis

// config/database.php (partial Redis config)
'redis' => [
    'client' => env('REDIS_CLIENT', 'phpredis'),
    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_QUEUE_DB', 0),
    ],
],
💻

Example

This example shows how to create a simple job, dispatch it to the Redis queue, and run the worker to process it.

php
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendWelcomeEmail implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    protected $userEmail;

    public function __construct($userEmail)
    {
        $this->userEmail = $userEmail;
    }

    public function handle()
    {
        // Simulate sending email
        logger("Sending welcome email to {$this->userEmail}");
    }
}

// Dispatching the job somewhere in your controller or service
SendWelcomeEmail::dispatch('user@example.com');

// Run this command in terminal to process jobs:
// php artisan queue:work redis
Output
Processing: App\Jobs\SendWelcomeEmail [INFO] Sending welcome email to user@example.com
⚠️

Common Pitfalls

Common mistakes when using Redis queues in Laravel include:

  • Not setting QUEUE_CONNECTION=redis in .env, so Laravel uses the default sync driver.
  • Redis server not running or misconfigured connection details.
  • Forgetting to run php artisan queue:work redis to process queued jobs.
  • Not setting up supervisor or a process manager for production queue workers.

Example of wrong and right .env setting:

bash
// Wrong (default sync driver, no queue processing)
QUEUE_CONNECTION=sync

// Right (use Redis queue)
QUEUE_CONNECTION=redis
📊

Quick Reference

StepCommand / SettingDescription
1QUEUE_CONNECTION=redisSet Redis as queue driver in .env
2Configure Redis in config/database.phpEnsure Redis host, port, and password are correct
3Create and dispatch jobsUse Laravel job classes and dispatch() method
4php artisan queue:work redisRun worker to process queued jobs
5Use supervisor in productionKeep queue workers running reliably

Key Takeaways

Set QUEUE_CONNECTION=redis in your .env to enable Redis queues in Laravel.
Configure Redis connection details correctly in config/database.php.
Create jobs implementing ShouldQueue and dispatch them to queue.
Run queue workers with php artisan queue:work redis to process jobs.
Use a process manager like supervisor for production queue workers.