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

Support for KEYS* command with redis clustering #172

Closed
integrii opened this issue May 26, 2014 · 15 comments
Closed

Support for KEYS* command with redis clustering #172

integrii opened this issue May 26, 2014 · 15 comments
Labels

Comments

@integrii
Copy link

It would be possible to run a KEYS command on all nodes of a cluster, then combine the results. This makes it possible to search for a pattern of keys.

In my app, I want to be able to delete everything for a specific user. My key path is always username:stuff.

It is nearly a deal breaker to not be able to run KEYS username:* and then delete everything for a specific user found in the reply. I would really appreciate this feature!

@nrk
Copy link
Contributor

nrk commented May 26, 2014

I can't accept your request because it covers a very specific requirement for a single application (which means it could not be generally useful for everyone in terms of actual implementation) and, what's more, built upon a command which usage is generally discouraged if not for debugging or migration purposes. You should implement this in your own application with something like

$allkeys = [];
$client = new Predis\Client([ /* list of nodes */ ], $options);
$cmdKeys = $client->createCommand('keys', ['username:*']);

foreach ($client->getConnection() as $nodeConnection) {
    $nodeKeys = $nodeConnection->executeCommand($cmdKeys);
    $allkeys = array_merge($allkeys, $nodeKeys);
}

You could do this in other ways, which means it's not generic enough to be added to Predis for general use.

@integrii
Copy link
Author

That is awesome @nrk ! Does Predis have a built in function for getting the cluster's node list? That would make this dynamic and prevent the upkeep of a static node list for a few admin commands that will be rarely used.

@nrk
Copy link
Contributor

nrk commented May 28, 2014

Well, yes and no. At least currently.

You can specify one or more nodes without passing the whole list and do the following:

$client = new Predis\Client(['127.0.0.1:6379', '127.0.0.1:6380'], ['cluster' => 'redis']);
$client->getConnection()->askClusterNodes();

Predis will connect to one of the initial nodes specified when creating the client (so they must be reachable) and reconfigure itself automatically to use the full list of nodes as returned by the command CLUSTER NODES, but this won't actually create new connection objects because they are lazily initialized only when needed and using foreach on the aggregate connection iterates only the single connections objects fully initialized in the pool. To put it simple, if you do a foreach over the aggregate connection returned by $client->getConnection() even after using askClusterNodes(), you will still get only two connection objects for 127.0.0.1:6379, 127.0.0.1:6380 even if your cluster is actually composed of 10 nodes.

I will see what can be done to improve things in this sense.

@integrii
Copy link
Author

That would be excellent! Thank you nrk for an excellent library and your dedication to maintaining it. It is because of you that PHP has the most mature Redis library available.

@jadbox
Copy link

jadbox commented Feb 9, 2015

Any updates on this? How do I iterate on all active cluster nodes if the connections are lazily initialized?

@JasonLantz
Copy link

bump for the Keys feature...

@espiegel
Copy link

another bump for this feature

@ravikanthg1
Copy link

I have a very similar requirement, need to search for pattern with keys in cluster, how can i do this in java ? @nrk or anyone..

@diimpp
Copy link

diimpp commented Jul 15, 2015

+1

@JasonLantz
Copy link

have you guys tried using hgetall?

On Wed, Jul 15, 2015 at 2:05 AM, Dmitri Perunov notifications@github.com
wrote:

+1


Reply to this email directly or view it on GitHub
#172 (comment).

@diimpp
Copy link

diimpp commented Jul 17, 2015

@JasonLantz how does it suppose to help to get list of keys?

Anyway, @nrk solution in second comment worked for me.
It connected to 3 out 3 nodes, not like it was stated to connect to only first one.

Maybe it was fixed, maybe I've misunderstood and this is a correct behaviour.
I'm calling predis from SncRedis bundle's service.

@woverton
Copy link

+1

1 similar comment
@ahfeel
Copy link

ahfeel commented Feb 3, 2016

👍

@nrk
Copy link
Contributor

nrk commented May 28, 2016

See my comments on #338 for a better and updated solution on how to iterate over the master nodes of redis-cluster to perform some commands on each node or iterate the keyspace as a whole in a decently efficient way.

@kofrezo
Copy link

kofrezo commented Apr 28, 2017

+1

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

No branches or pull requests

10 participants