Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable auto reconnect of redis connection in Jedis client

Tags:

java

jedis

I have an application that stores and reads date in Redis. When Redis goes down and then comes back up, the Jedis connection is not being recreated.

Is there is a way to create a connection retry in Jedis?

like image 359
Vigneshwaran Avatar asked Jun 29 '17 14:06

Vigneshwaran


People also ask

How does Jedis connect to Redis?

The Java CodeJedis jedis = new Jedis("localhost"); // prints out "Connection Successful" if Java successfully connects to Redis server. Here is a breakdown of the above code: Jedis jedis = new Jedis("localhost"); This connects our Java to Redis server running on our local host.

What is Redis connection timeout?

The maximum length of time to wait while establishing a connection to a Redis server.

Do we need to close Redis connection?

Continuously opening connections without closing is not a good practice. This will not only consume your resources but may also lead to program crash. The maximum number of file descriptors that you can open simultaneously is 1024.

Does Jedis support Redis cluster?

The latest version of Jedis has support to connect with Redis Cluster.


1 Answers

You may use JedisPool. In case of broken connection JedisPool just invalidates broken connections.

Also you could use Redisson Redis Java client which has internal ConnectionWatchDog to avoid broken connections. Redisson exposes few settings to control Redis command retry logic: retryAttempts, retryInterval, timeout

like image 181
Nikita Koksharov Avatar answered Sep 21 '22 12:09

Nikita Koksharov