Attach to a cluster with reds-cli -c -h redis-1
Issues command: "keys *"
You will only receive the keys that are stored on the attached node. The command should probably query one host from each slot.
If you have a single master with n slaves, it's probably fine. But if your cluster actually has multiple slots (multiple master nodes, each holding a subset of keys), then you do not retrieve full results.
Comment From: itamarhaber
Hello @jplflyer
A connection from a Redis client to the cluster is always to one specific node, and commands run via that connection are always executed by that one node. KEYS in that sense behave correctly and returns the names of keys in all slots managed by the node. Currently, the lion's share of Redis' commands are not propagated in the cluster (IIRC only some CLUSTER commands are), and it is up to the client application to do that if needed.
redis-cli does offer cluster support, and specifically, the call subcommand that connects to all nodes to execute a given command (from command line), e.g.:
$ redis-cli -h redis-1 --cluster call "KEYS *"
The cli's -c switch affects the REPL mode, and follows redirection instructions from the cluster.
As this is a question rather than an actual issue, please close it if the answer is satisfactory.
Comment From: jplflyer
redis-cli --help offers this line of output:
-c Enable cluster mode (follow -ASK and -MOVED redirections).
Perhaps this output line should be a little more informative, telling us that only a small subset of commands will be fully supported in cluster mode, and some will only communicate with the primary node.
As a programmer, I shouldn't have to change my programming based on whether I'm talking to a Redis instance with a single node vs. a cluster. Programmers are rarely the production sys admins, after all, and I could be long gone when the sys admin decides to switch to using a cluster from a single node.
That may be a side point.
Regardless, barring clear documentation, it's reasonable to assume if I use redis-cli in cluster mode (that is, I specify -c), and if I can set and retrieve values across the cluster after only specifying a single node, that the other commands will also be complete. It's rather odd that I can "PUT foo bar" and "GET FOO", but then do "KEYS *" and have it return an empty list.
In cluster mode, the command should actually do the right (complete) thing, or it should be clearly and readily documented that it's not going to.
Comment From: itamarhaber
I'll bite :)
Perhaps this output line should be a little more informative
It is a single help line, which is kind of the default for such utilities' usage/help clause. To actually understand what that line means one would have to Refer To Full Manual, which is a common expectation from technical folks.
As a programmer, I shouldn't have to change my programming based on whether I'm talking to a Redis instance with a single node vs. a cluster.
(/me not a programmer) Perhaps, but the Redis docs clearly state the opposite - single-instance and cluster are not the same thing.
That may be a side point.
Agreed, different opinions, let's agree to disagree.
Regardless, barring clear documentation, it's reasonable to assume...
If I learned anything it is that it is reasonable to assume nothing. In this specific case, your assumption is as good as anyone's imo. Personally, I like clis that do not manipulate/transform/try to outsmart me - these things are supposed to be literal and simple. Put differently, I'd be more confused if the cli packed more intelligence and tried to guess what I meant instead of following my instructions blindly. I much prefer having to be verbose and explicit rather than rely on baked-in assumptions.
But again, that's probably a matter of opinion.
Comment From: jplflyer
You know what? It's not my product, and I'm not offering to become a contributor. I'm already working enough hours in the week. I took the time to explain what I felt is a shortcoming. You disagree. I then said in effect, "I disagree with you, but do you think it could at least be documented?"
And you said, in effect, "No."
I'm sure changing that printf to say something like:
-c Support cluster mode for some commands (follow -ASK and -MOVED redirections).
would be product-breaking.
Personally, I feel that -c should actually be a complete implementation. Currently, it's only a partial implementation.
If I didn't want to actually have the tool handle the cluster communications for me, I wouldn't add -c, after all.
But as I'm not a contributor, all I can do is make suggestions. You're free to ignore them.
I've now spent more time explaining this than I did to realize why my "keys *" command wasn't giving me the expected results. And so I'm done.
Close it if you want.
Comment From: itamarhaber
Dear @jplflyer
Please forgive me if my answer had offended you in any way - my intention was to explain the rationale as I perceive it and share my personal view, not discourage you from asking and proposing changes.
Personally, I feel that the cli in particular and Redis in general, are easy to get started with but do require some learning to get the most out of. Furthermore, Redis usually does not provide out of the box solutions, but rather building blocks that you can use to make it your own. In these respects, the cli's behavior and implementation are very aligned - it is almost bare bones and requires verbosity to bend your will.
As a side note, from what I've seen, other Redis clients behave pretty much the same way in the sense that they offer different APIs to access single-instance and cluster deployments as well as by not performing multi-node operations. Arguably, such high-level cluster-abstracting clients may exist or perhaps will be developed, all I'm saying I've never seen one.
Just so we're perfectly clear, while I am a contributor to this project, I do not call shots - this is up to the community and the maintainers. I respect your time and the effort you've put into understanding the topic and explaining your POV - I hope that this thread will be of use to future generations of Redis users.
Comment From: antirez
Hello @jplflyer, the redis-cli cluster mode does just what it advertises:
follow -ASK and -MOVED redirections
Basically it just will follow redirections. All the commands will still be locally executed in the context of the local node. I don't feel like this should be better documented, because the Redis Cluster documentation in general explains exactly that each node is independent and what level of features the Redis Cluster provides. Yet your question makes total sense. Closing.
Comment From: kpx-dev
I just ran into this issue too, the updated command is:
redis-cli --cluster call redis-cluster-ip:6379 keys *
Comment From: nishantgeorge
As a programmer, I shouldn't have to change my programming based on whether I'm talking to a Redis instance with a single node vs. a cluster. Programmers are rarely the production sys admins, after all, and I could be long gone when the sys admin decides to switch to using a cluster from a single node.
Reviving this discussion, the new redis-cluster-proxy might be a good fit for this use case.
Comment From: jeromedoucet
@itamarhaber, @jplflyer I just see this thread, and I am almost certain you are aware of such mechanism, but at leat one redis client (go-redis) does expose an UniversalClient that abstracts the redis configuration (cluster, sentinel or standalone) for the application that use it.