redux
redux copied to clipboard
How to connect to a redis cluster (replica)
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?
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.
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