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

How to connect to a redis cluster (replica) #25

Open
jacobxk opened this issue Oct 31, 2018 · 2 comments
Open

How to connect to a redis cluster (replica) #25

jacobxk opened this issue Oct 31, 2018 · 2 comments

Comments

@jacobxk
Copy link

jacobxk commented Oct 31, 2018

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
Copy link
Author

jacobxk commented Nov 1, 2018

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.

@richfitz
Copy link
Owner

richfitz commented Nov 2, 2018

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 redis/hiredis#591, redis/hiredis#401, redis/hiredis#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

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

No branches or pull requests

2 participants