cachinglaravel-5.5configureredis-sentinel

Configure Redis Sentinel in Laravel 5.5


How to configure the Redis sentinel? Redis can be configured in stand alone easily using laravel configs but when using sentinel how to configure is not documented anywhere?

There is one similar question asked on redit: but no help.


Solution

  • In Laravel 5.5 one can do it like this:

    Reference: https://github.com/laravel/framework/pull/18850#issue-116339448

    database.php:

    'redis' => [
    
            'client' => 'predis',
    
            // Keep Default as is you want to use both redis and sentinel for different service(cache, queue)'
            'default' => [
                'host' => env('REDIS_HOST', '127.0.0.1'),
                'password' => env('REDIS_PASSWORD', null),
                'port' => env('REDIS_PORT', 6379),
                'database' => 0,
            ],
    
            // Create a custom connection to use redis sentinel
            'cache_sentinel' => [
                // Set the Sentinel Host from Environment (optinal you can hardcode if want to use in prod only)
                env('CACHE_REDIS_SENTINEL_1'),
                env('CACHE_REDIS_SENTINEL_2'),
                env('CACHE_REDIS_SENTINEL_3'),
                'options' => [
                    'replication' => 'sentinel',
                    'service' => 'cachemaster'),
                    'parameters' => [
                        'password' => env('REDIS_PASSWORD', null),
                        'database' => 0,
                    ],
                ],
            ],
        ],
    

    ```

    Specify the redis connection in your service where you want to use. example if cache needs redis sentinal can create new cache connection to use the above sentinal connection like this:

    'stores' = [
    
        //Keep default too as is
        'redis' => [
            'driver'     => 'redis',
            'connection' => 'default',
        ],
    
        // create own cache connection
        'sentinel_redis' => [
            'driver'     => 'redis',
            'connection' => 'cache_sentinel',
        ],
    

    And in Laravel app you can easily use via Cache Facade:

    Cache::store('sentinel_redis')->get('key');