redis / redis-rb

A Ruby client library for Redis
MIT License
3.97k stars 1.03k forks source link

Cluster Mode: Ability for client to send a command to specific cluster node(s) or all cluster nodes. #986

Open charlez opened 3 years ago

charlez commented 3 years ago

RE: Cluster Mode

supercaracal commented 3 years ago

Hello,

Would you tell us what is the purpose of the use case? Is it for monitoring or load balancing?

charlez commented 3 years ago

@supercaracal - Yes, as you surmised, the use case is for cluster monitoring and load balancing.

supercaracal commented 3 years ago

You can extract all node information with workaround like this:

r = Redis.new cluster: ['redis://127.0.0.1:7000']
r._client.instance_variable_get(:@node).call_all(%w[info]).map { |reply| Redis::HashifyInfo.call(reply) }

It needs more parsing steps if you specify commandstats as section of the command. https://github.com/redis/redis-rb/blob/fa76a2661739f56e3458fc5ed5a7b7c31861eff5/lib/redis.rb#L311-L317

The Gem certainly sends the command to a random node when using cluster mode. That behavior might be pointless. https://github.com/redis/redis-rb/blob/fa76a2661739f56e3458fc5ed5a7b7c31861eff5/lib/redis/cluster.rb#L128-L154

It seems that Redis Cluster Proxy doesn't support the command. https://github.com/RedisLabs/redis-cluster-proxy/blob/unstable/COMMANDS.md#unsupported-commands

Basically, keys and nodes go hand in hand when servers are cluster mode and clients send commands including keys. I would say that the Gem need not dare to support of exposing interfaces each node.

charlez commented 3 years ago

@supercaracal - Thanks for the feedback and work-around suggestions.

Following is an approach I believe should work to send commands to individual cluster nodes (in addition to your #call_all suggestion to call all cluster nodes). Please let me know your thoughts as to the viability, thanks:

supercaracal commented 3 years ago

It seems that there are no problems except one of the first step:

s/@Clients/@clients/

https://github.com/redis/redis-rb/blob/fa76a2661739f56e3458fc5ed5a7b7c31861eff5/lib/redis/cluster/node.rb#L18