StackExchange / StackExchange.Redis

General purpose redis client
https://stackexchange.github.io/StackExchange.Redis/
Other
5.91k stars 1.51k forks source link

Feature request for supporting connecting to individual nodes with hostname in cluster mode #2791

Open peijiehao opened 2 months ago

peijiehao commented 2 months ago

Context:

Starting from redis 7.x. two new properties cluster-preferred-endpoint-type and cluster-announce-hostname properties have been added. When cluster-preferred-endpoint-type is set to hostname, Moved and Ask responses contain the hostnames. When cluster-announce-hostname is set, cluster nodes outputs the hostname along with the Ip. https://redis.io/docs/latest/commands/cluster-nodes/

cluster nodes output:

07c37dfeb235213a872192d90877d0cd55635b91 127.0.0.1:30004@31004,hostname4 slave e7d1eecce10fd6bb5eb35b9f99a514335d9ba9ca 0 1426238317239 4 connected

Problem:

When the nodes in the cluster have announced hostnames. StackExchange.Redis should parse the hostnames instead of IPs from cluster nodes output and connect to the nodes via hostnames. By default, StackExchange.Redis runs cluster nodes and uses the IPs to make connections to the individual nodes. This integration will not work when the nodes are behind a proxy/NAT gateway, thus, we are expecting that when cluster-preferred-endpoint-type is set to hostname, on moving and connecting to a different node StackExchange.Redis will use hostname instead of IP.

Solution:

Suggesting StackExchange.Redis to make connections via hostnames if available and default to IPs if not present

mgravell commented 2 months ago

Ultimately we're always going to be connecting by IP, but if the point here is that we should use the announced IP or the IP we get after a DNS check, rather than the IP address reported by CLUSTER NODES, then sure: that should be fine. If CLUSTER NODES also now contains the host name, then we need to figure out what all the rules are here - for example: say we get a name from MOVED: do we attempt to lookup the name in our current CLUSTER NODES metadata and use the corresponding IP from there? Or do we rely on DNS?

tharunsikhinam commented 1 month ago

if the point here is that we should use the announced IP or the IP we get after a DNS check, rather than the IP address reported by CLUSTER NODES, then sure: that should be fine

^ yes.

I feel the rules should be, if hostname is present in cluster nodes output then use the hostname. Perform the DNS check and connect via IP. Same is applicable for MOVED/ASK responses, if hostname is returned (when cluster-preferred-endpoint-type is set to hostname) then do a DNS check and connect via IP. Do not need to do a reverse lookup against cluster nodes.

This is the behavior I'm seeing with other Redis clients (lettuce, jedis etc.,) https://github.com/redis/lettuce/blob/c33cea93f54c3bc5cd320634ffde53c3a363032a/src/main/java/io/lettuce/core/cluster/models/partitions/ClusterPartitionParser.java#L203