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
Predis, Sharding and Moved responses #259
Comments
It looks like you did not configure Predis to use redis-cluster but instead you are using it with the plain old client-side sharding logic (which is also the default behaviour). You should configure the client setting the option $client = new Predis\Client([$node1, $node2, ...], ['cluster' => 'redis']); Doing so will make it possible for the client to automatically handle I'm closing this issue since I guess it is indeed only a mis-configuration of the client, but feel free to re-open it if you think there is actually more to that. |
Hi, I have a Redis Cluster with 3 master nodes and 3 slave ones. <?php
require "/opt/php/vendor/predis/autoload.php";
Predis\Autoloader::register();
try {
$parameters = array('tcp://X.X.X.X/?password=secret', 'tcp://Y.Y.Y.Y/?password=secret','tcp://Z.Z.Z.Z/?password=secret');
$options = array("cluster", "redis");
$redis = new Predis\Client(
$parameters
, $options);
echo $redis->set('test','It works !') .PHP_EOL;
echo "Successfully connected to Redis";
}
catch (Exception $e) {
echo "Couldn't connect to Redis" .PHP_EOL;
echo $e->getMessage() .PHP_EOL;
$pos =strpos((string)$e->getMessage(), 'MOVED');
if ($pos === false)
echo "Understood" .PHP_EOL;
else
echo "Needs to relaunch with new server" .PHP_EOL;
$pattern = '/MOVED (\d+) ([\d\.]+)\:(\d+)/';
preg_match($pattern, (string)$e->getMessage(), $matches, PREG_OFFSET_CAPTURE);
$parameters = array((string)('tcp://' . $matches[2][0] . '/?password=secret'));
$redis = new Predis\Client(
$parameters
, $options));
echo $redis->set('test','It works !') .PHP_EOL;
?> Here is what I'm getting when I launch my php code: Couldn't connect to Redis So I would like to know what I'm doing wrong for the client to automatically handle -MOVED or -ASK responses coming from Redis nodes please. Thanks a lot ! |
The client options array must be a named array, you are passing the wrong values so that's why it's not working: $options = array("cluster" => "redis");` and not $options = array("cluster", "redis"); |
Thanks a lot ! Unfortunately now I'm getting the following error: NOAUTH Authentication required. This is my current code $parameters = array('tcp://X.X.X.X/?password=secret', 'tcp://Y.Y.Y.Y/?password=secret','tcp://Z.Z.Z.Z/?password=secret'); $options = array("cluster" => "redis"); As you can see the password is in the URI itself so I'm thinking it should work but it doesn't ... Thanks a lot ! |
If you set a password for the known nodes in It is possible to apply a set of common parameters (in your case, the password) for the connections to new nodes returned by Redis in $parameters = [
"tcp://127.0.0.1:7001?password=secret",
"tcp://127.0.0.1:7002?password=secret"
];
$options = [
// You can choose any name instead of "default_params",
// as this is just a custom option.
"default_params" => ['password' => 'secret'],
"cluster" => function ($options) {
$cluster = new Predis\Connection\Aggregate\RedisCluster($options->connections);
$cluster->setDefaultParameters($options->default_params);
return $cluster;
},
];
$client = new Predis\Client($parameters, $options); This aspect of the configuration may be improved in the future. |
Hi,
Some of the queries has worked fine but several queries from the application would get -MOVED error message. |
After some testing, it seems like the pipeline command caused the issue. |
I've finally managed to connect to redis cluster without getting MOVED issue when used connection string like this:
Sharing here for next people who might come here looking for this. |
|
Hi folks,
I’m working with your great Predis PHP library to connect to a Redis Cluster.
Everything works fine, I can connect to the nodes but when I set a testing key in the Cluster I get the following response:
MOVED 1033 X.X.X.X:6379
This is totally fine as I understand that using sharding we need to point to the right instance.
My question is on what to do next. Should I programmatically use the value returned by your library and try my command again or is there a automatic feature that can take care of that ?
Thanks a lot !!
The text was updated successfully, but these errors were encountered: