Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect Redis cluster with jedis

Tags:

redis

jedis

Since a single redis instance doesn't meet my requirements, I went for redis cluster. I formed cluster with three nodes and populated data into the cluster. When I get data from cluster using JedisCluster it takes more time than the single instance. So, what's the proper way to connect jedis with redis cluster. How can I make use of connection pool to connect jedis with redis cluster?

like image 891
Hemnath Avatar asked Jul 23 '15 12:07

Hemnath


2 Answers

It's normal that you observe latency when you retrieve datas in a jedis cluster depending of the clustering strategy that you have because you will bonce from one jedis to another in order to take the datas. The red arrow, is the reason of the time added to queries in jedis cluster.

jedis vs jedis cluster

The onlyway to access to the right instance is to understand from the key the location of your datas, introduicing in the key the location exemple : "instance3/user/5" or finding the location instance performing an calculation on the keys.

like image 160
jeorfevre Avatar answered Oct 07 '22 07:10

jeorfevre


JedisCluster creates it's own pooling. You can provide the configuration for the pooling when you create the JedisCluster object. From that point on, you can treat the cluster like a single instance and the requests will go to the proper cluster instance.

JedisCluster cluster - new JedisCluster(...); ... cluster.set(key, value);

Trying to figure out which instance it went to can be done by getSlot() but there's no guarantee that from one moment to the next the key you set is on the same instance. Because of failover, it could have moved.

like image 22
Allan Wax Avatar answered Oct 07 '22 05:10

Allan Wax