Connecting to redis cluster using Java/Python clients
This story is in continuation of my 3 parts redis series. First — Creating a redis cluster. Second — Horizontally scaling in/out a redis cluster and lastly this story which will explain how can we connect to a redis cluster from within our java or python applications.
Using Java
Let us first see how can we connect to a redis cluster using Java. For this purpose i created a spring boot project. We will be using jedis client to connect to redis cluster. Know more about Jedis here. The dependencies
tag of my pom.xml
looks something like as follows-
application.properties
file under resources directory should look like as follows-
Notice that i haven’t set a password for simplicity. You can use a password if your redis cluster is password protected.
Next we would want to create a Bean using class JedisCluster
of package redis.clients.jedis
to establish a connection with redis cluster which will use values in application.properties
file as shown in following gist-
In the above gist, we provided only 1 host i.e
localhost
with port6001
to connect to, However when connection is establishedJedisCluster
gathers information about other nodes in the cluster. So even iflocalhost:6001
goes down, it will still be able to communicate with the cluster through some other node eg.6002
or6003
etc. This essentially means thatJedisCluster
manages failover scenarios. Until and unless there is an operational cluster,JedisCluster
will be able to connect to it.
We can now Autowire JedisCluster
class in our application’s DAO layer to interact with redis cluster as shown below-
@Component
public class RedisDaoImpl {
@Autowired
private JedisCluster getRedisCluster;
public void setKeyInRedis(String key, String value) {
getRedisCluster.set(key, value);
}
public String getValueByKey(String key) {
return (String) getRedisCluster.get(key);
}
}
setKeyInRedis()
will set a key in redis and getValueByKey()
will get the value of key. Private autowired variable getRedisCLuster
will now be capable to do a host of operations apart from just get()
and set()
eg. — exists()
, persist()
, expire()
and many more redis operations.
Using Python
To connect a python application to redis cluster we will use redis-py-cluster. Know more about redis-py-cluster here.
Firstly you need to install redis-py-cluster
$ pip install redis-py-cluster
All we need to do now is import RedisCLuster
and define some configuration.
See gist below-
Variable rc
will now be capable to do all redis operations like exists()
, expire()
etc.
It is recommended to add all nodes in the cluster in List
startup_nodes
. Even if you don’t, it will still identify all nodes in cluster on its own and will manage failover scenarios. This means that in above scenario, if127.0.0.1:6001
goes down, it will still be able to connect to redis cluster without us explicitly telling it which all nodes are part of the cluster.
This marks the end of 3 part redis series where we learnt how to create a redis cluster then we learnt how to horizontally scale in/out a redis cluster and after this story we also know how to connect to a redis cluster programatically.
Enjoy redis…!!!