redux icon indicating copy to clipboard operation
redux copied to clipboard

How to connect to a redis cluster (replica)

Open jacobxk opened this issue 6 years ago • 2 comments

Suppose there were several hosts and ports of the redis server, like 10.0.1.1:6381 10.0.1.1:6382 10.0.1.2:6381 10.0.1.2:6382 how can I configure the redux::hiredis()?

I have google around but can't find a solution. And I noticed that there was a note on db parameter of the redis_config with "Do not use in a redis clustering context.", so I inspect that this was a way to connect to a cluster. In addition, I have also try to pass a redis://10.0.1.1:6381,10.0.1.1:6382,10.0.1.2:6381,10.0.1.2:6382 to the url parameter, but still failed.

Any suggestions?

jacobxk avatar Oct 31 '18 21:10 jacobxk

My initial solution is writing a function to point to the correct node based on the error message.

check_redis <- function(key = "P10000", host = "10.7.3.46", port = 6381) {
      r <- redux::hiredis(host = host, port = port)
      status <- tryCatch(
        {
          r$EXISTS(key = key)
        },
        error = function(e){
          address <- str_match(e$message, 
                               "[0-9]+.[0-9]+.[0-9]+.[0-9]+:[0-9]+")
          host <- str_split(address, ":", simplify = T)[1]
          port <- str_split(address, ":", simplify = T)[2]
          return(list(host = host, port = port))
        }
      )
      if (is.list(status)) {
        r <- redux::hiredis(host = status$host, port = status$port)
      } 
      return(r)
    }

It can help to direct to the correct node, but this solution is neither elegant nor efficient. So please advise.

jacobxk avatar Nov 01 '18 13:11 jacobxk

I have never configured a redis cluster before! If you can point me at a quick how-to I can see how to get started with testing something.

The underlying hiredis library apparently does not support redis clusters (see https://github.com/redis/hiredis/issues/591, https://github.com/redis/hiredis/issues/401, https://github.com/redis/hiredis/issues/366)

I'm completely unclear what the issues involved here are I'm afraid but can look into it. From the look of this repo by the redis/hiredis author this is a nontrivial task though

richfitz avatar Nov 02 '18 17:11 richfitz