Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to use Redis Sentinel configuration #18850

Merged
merged 3 commits into from
Apr 18, 2017
Merged

Added ability to use Redis Sentinel configuration #18850

merged 3 commits into from
Apr 18, 2017

Conversation

mingalevme
Copy link
Contributor

These changes make it possible to use Redis Sentinel with Laravel/Lumen and Predis, configuration example:

return [
   // ...
    'redis' => [
        'client' => 'predis',
        'sentinel' => [
            'tcp://10.24.5.136:26379?timeout=0.100',
            'tcp://10.24.5.137:26379?timeout=0.100',
            'tcp://10.24.5.138:26379?timeout=0.100',
            'options' => [
                'replication' => 'sentinel',
                'service' => env('REDIS_SENTINEL_SERVICE', 'mymaster'),
                'parameters' => [
                    'password' => env('REDIS_PASSWORD', null),
                    'database' => 0,
                ],
            ],
        ],
    ],
];

It's very important to call Arr::pull($config, 'options', []) before passing it to Client.

These changes make it possible to use Redis Sentinel with Laravel/Lumen and Predis, configuration example:
```
return [
   // ...
    'redis' => [
        'client' => 'predis',
        'sentinel' => [
            'tcp://10.24.5.136:26379?timeout=0.100',
            'tcp://10.24.5.137:26379?timeout=0.100',
            'tcp://10.24.5.138:26379?timeout=0.100',
            'options' => [
                'replication' => 'sentinel',
                'service' => env('REDIS_SENTINEL_SERVICE', 'mymaster'),
                'parameters' => [
                    'password' => env('REDIS_PASSWORD', null),
                    'database' => 0,
                ],
            ],
        ],
    ],
];
```
It's very important to call Arr::pull($config, 'options', []) before passing it to Client.
@s-hamid
Copy link

s-hamid commented Apr 13, 2021

These changes make it possible to use Redis Sentinel with Laravel/Lumen and Predis, configuration example:

return [
   // ...
    'redis' => [
        'client' => 'predis',
        'sentinel' => [
            'tcp://10.24.5.136:26379?timeout=0.100',
            'tcp://10.24.5.137:26379?timeout=0.100',
            'tcp://10.24.5.138:26379?timeout=0.100',
            'options' => [
                'replication' => 'sentinel',
                'service' => env('REDIS_SENTINEL_SERVICE', 'mymaster'),
                'parameters' => [
                    'password' => env('REDIS_PASSWORD', null),
                    'database' => 0,
                ],
            ],
        ],
    ],
];

It's very important to call Arr::pull($config, 'options', []) before passing it to Client.

What will be the default key settings?

@CharlesBilbo
Copy link
Contributor

CharlesBilbo commented Apr 14, 2021

In case anyone is visiting this in 2021 this is the appropriate config to get redis sentinel working with your application

Additionally at this time you need to use the dev-master branch of predis and not the current version (1.1.7) as this version hard sets the password to null.

carbon

@Olayiwola72
Copy link

@CharlesBilbo @taylorotwell I am trying your configuration in 2021 and it's working. Thank you so much. Please, I have a few questions

By default in Laravel we have the 'cache configuration', how would you setup your cache configuration with Redis Sentinel

image

Also, I would like to ask why do you have the same value 'tcp://127.0.0.1:26379?timeout=0.1' on 3 different lines in your default configuration? I do have there different sentinels running on three different ports

image

@skiat
Copy link

skiat commented Aug 12, 2022

Let me conribute with another working config (running in 2022 with laravel 8.x)

This includes "private" options that allows to choose different database id and prefix, which is very important in case of shared redis instances.

config/database.php

<?php

return [
//...
   'redis' => [
       'client' => 'predis',

       'default' => [
             'tcp://' . env('REDIS_HOST') . ':' . env('REDIS_PORT') . '?timeout=0.1',
             'tcp://' . env('REDIS_HOST_n') . ':' . env('REDIS_PORT') . '?timeout=0.1',
             'options' => [
                  'replication' => 'sentinel',
                  'service' => 'mymaster',
                  'parameters' => [
                       'password' => env('REDIS_PASSWORD'),
                       'database' => env('REDIS_DB', 0)],
                       'prefix' => 'myapp:',
                  ],
             ],
       ],

       'cache' => [
             'tcp://' . env('REDIS_HOST') . ':' . env('REDIS_PORT') . '?timeout=0.1',
             'tcp://' . env('REDIS_HOST_n') . ':' . env('REDIS_PORT') . '?timeout=0.1',
             'options' => [
                  'replication' => 'sentinel',
                  'service' => 'mymaster',
                  'parameters' => [
                       'password' => env('REDIS_PASSWORD'),
                       'database' => env('REDIS_CACHE_DB', 1)
                       'prefix' => 'myappcache:',
                  ],
             ],
       ],
   ],
];

@DreadfulCode
Copy link

DreadfulCode commented Aug 17, 2022

Thanks @skiat but your example appears to be broken. Is there a closing bracket missing for 'default.parameters'?

@CharlesBilbo
Copy link
Contributor

@DreadfulCode Yeah seems like there is a closing bracket missing @Olayiwola72 Each TCP address in that array is a different redis sentential server. instance they way sentinel works is their one main server and 2 observers.

@skiat
Copy link

skiat commented Aug 17, 2022

Thanks @skiat but your example appears to be broken. Is there a closing bracket missing for 'default.parameters'?

Thanks, yes, I fixed my comment.
Cheers

@LyKos4
Copy link

LyKos4 commented Apr 22, 2024

Does it work with phpredis too?
Also can we use it for default, cache and others that are using different redis databases so that all use sentinel?
How to avoid Undefined array key "host" by using above configuration for default or /and cache in redis database.php?

@skiat
Copy link

skiat commented Apr 22, 2024

Does it work with phpredis too? Also can we use it for default, cache and others that are using different redis databases so that all use sentinel?

Hi LyKos4
unfortunately not. And also this with predis is no more applicable since laravel 10.
see https://github.com/monospice/laravel-redis-sentinel-drivers as replacement

@LyKos4
Copy link

LyKos4 commented Apr 23, 2024

Does it work with phpredis too? Also can we use it for default, cache and others that are using different redis databases so that all use sentinel?

Hi LyKos4 unfortunately not. And also this with predis is no more applicable since laravel 10. see https://github.com/monospice/laravel-redis-sentinel-drivers as replacement

So from version 10, laravel doesn't support sentinel usage without additional packages, right?
Unfortunately monospice has below limitation:
"For instance, if we wish to use Redis behind Sentinel for both the cache and session in Laravel's API, we cannot set separate Redis databases for for both types of data like we can in a standard, single-server Redis setup without Sentinel. This causes issues when we need to clear the cache, because Laravel erases stored session information as well. "

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants