Mastering Redis Integration in Laravel: Configuration, Commands, and Advanced Features

This guide explains how to install Redis clients, configure single and clustered connections, customize Predis and PhpRedis options, and use Laravel's Redis facade for basic commands, pipelining, multiple connections, and publish/subscribe messaging, all with clear code examples.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Mastering Redis Integration in Laravel: Configuration, Commands, and Advanced Features

Redis is an open‑source, in‑memory key‑value store that provides native data structures such as strings, hashes, lists, sets and sorted sets.

Installation

Laravel requires a Redis client library. The most common choice is the predis/predis package installed via Composer: composer require predis/predis For higher performance on heavy workloads you can install the PECL phpredis extension instead.

Configuration

All Redis settings are defined in config/database.php under the 'redis' key.

'redis' => [
    'client' => 'predis',
    'default' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => 0,
    ],
];

You can add additional named connections or clusters by extending this array.

Cluster configuration

When using a Redis cluster, add a 'clusters' entry and enable native clustering via the 'options' array:

'redis' => [
    'client' => 'predis',
    'options' => [
        'cluster' => 'redis',
    ],
    'clusters' => [
        'default' => [
            [
                'host' => env('REDIS_HOST', 'localhost'),
                'password' => env('REDIS_PASSWORD', null),
                'port' => env('REDIS_PORT', 6379),
                'database' => 0,
            ],
        ],
    ],
];

Predis extra options

Any additional Predis connection parameters can be added to the server definition, for example:

'default' => [
    'host' => env('REDIS_HOST', 'localhost'),
    'password' => env('REDIS_PASSWORD', null),
    'port' => env('REDIS_PORT', 6379),
    'database' => 0,
    'read_write_timeout' => 60,
];

PhpRedis client

Switch the client to phpredis by changing the 'client' value. PhpRedis supports extra parameters such as persistent, prefix, read_timeout and timeout:

'redis' => [
    'client' => 'phpredis',
    // other connection settings …
];

Interacting with Redis

Laravel provides a Redis facade that forwards any Redis command.

use Illuminate\Support\Facades\Redis;

$user = Redis::get('user:profile:' . $id);
return view('user.profile', ['user' => $user]);

Other common methods:

Redis::set('name', 'Taylor');
$values = Redis::lrange('names', 5, 10);
$values = Redis::command('lrange', ['name', 5, 10]);

Multiple connections

Retrieve a specific connection with Redis::connection(). Pass the connection name defined in the configuration to obtain a different server or cluster:

$default = Redis::connection();
$custom = Redis::connection('my-connection');

Pipelining

For bulk operations, wrap commands in Redis::pipeline(). All commands inside the closure are sent in a single round‑trip:

Redis::pipeline(function ($pipe) {
    for ($i = 0; $i < 1000; $i++) {
        $pipe->set("key:$i", $i);
    }
});

Publish / Subscribe

Laravel exposes publish and subscribe methods. A typical subscriber is implemented as an Artisan command that runs a long‑lived process:

<?php
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class RedisSubscribe extends Command
{
    protected $signature = 'redis:subscribe';
    protected $description = 'Subscribe to a Redis channel';

    public function handle()
    {
        Redis::subscribe(['test-channel'], function ($message) {
            echo $message;
        });
    }
}

Publishing a message can be done from any part of the application, for example a route:

Route::get('publish', function () {
    Redis::publish('test-channel', json_encode(['foo' => 'bar']));
});

Wildcard subscriptions use psubscribe to receive messages from pattern‑matched channels:

Redis::psubscribe(['*'], function ($message, $channel) {
    echo $message;
});

Redis::psubscribe(['users.*'], function ($message, $channel) {
    echo $message;
});
Redis Pub/Sub diagram
Redis Pub/Sub diagram
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendrediscachingPHPLaravel
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.