Using Redis with Laravel: Installation, Configuration, and Cluster Setup

This tutorial walks through compiling the php‑redis extension, installing Laravel, configuring Redis as a cache and database driver, setting up a Redis cluster, and provides sample PHP code for read/write operations, offering a comprehensive guide for backend developers.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Using Redis with Laravel: Installation, Configuration, and Cluster Setup

This article explains how to integrate Redis with a Laravel application, covering the compilation of the php‑redis extension, Laravel installation, and Redis configuration for caching and database usage.

1. Compile and install php‑redis extension

# cd /usr/local/src
# wget http://pecl.php.net/get/redis-5.2.2.tgz
# tar -zxvf redis-5.2.2.tgz
# cd redis-5.2.2
# /usr/local/php7.2/php/phpize
# ./configure --with-php-config=/usr/local/php7.2/bin/php-config
# make && make install
echo "extension=redis.so" >> php.ini

2. Install Laravel framework

composer -v
composer create-project --prefer-dist laravel/laravel blog

Generate the application key: php artisan key:generate Access the project at http://localhost/blog/public/index.php.

3. Install predis extension composer require predis/predis 4. Configure Redis in Laravel

In config/cache.php set the driver to redis and define the store:

'default' => env('CACHE_DRIVER', 'file'),
'stores' => [
    'redis' => [
        'driver' => 'redis',
        'connection' => 'cache',
    ],
],

In config/database.php configure the Redis client, options, and connections, including cluster settings:

'redis' => [
    'client' => env('REDIS_CLIENT', 'predis'),
    'options' => [
        'cluster' => env('REDIS_CLUSTER', 'predis'),
        'prefix' => env('APP_NAME', 'laravel').'_database_',
    ],
    'default' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_DB', 0),
    ],
    'cache' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', 6379),
        'database' => env('REDIS_CACHE_DB', 1),
    ],
    // Cluster configuration example using three nodes
    'clusters' => [
        'write' => [
            [
                'host' => '192.168.1.100',
                'password' => null,
                'port' => 6379,
                'database' => 0,
            ],
        ],
        'read' => [
            [
                'host' => '192.168.1.101',
                'password' => null,
                'port' => 6379,
                'database' => 0,
            ],
            [
                'host' => '192.168.1.102',
                'password' => null,
                'port' => 6379,
                'database' => 0,
            ],
        ],
    ],
],

5. Sample PHP code for Redis read/write

$redis = new Redis();
$redis->connect('localhost', 6379);
$redis->set('username', 'wangwu');
var_dump($redis->get('username')); // outputs string(6) "wangwu"

// Using Laravel's Redis facade with cluster connections
$res = Redis::connection('clusters.write')->set('key', 'value');
$res = Redis::connection('clusters.read')->get('key');

The steps above provide a complete guide to set up Redis for caching and as a primary data store within a Laravel application, including cluster configuration for high availability.

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.

BackendCachedatabaseredisClusterPHPLaravel
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.