0

I am using redis as a cache in one of my spring boot java application. I am performing only get operation on this redis instance from a scheduler code which runs every 30 seconds and write operation is performed by some other application. I have a scenario where if my redis instance goes down due to any reason then I have to stop doing a particular action. I am using jedis as redis client. Assume my jedis pool was configured and connection pool was created successfully.

For couple of iterations my scheduler worked fine and was able to perform the get operation on redis. Now assume redis went down so now at this point after 30 seconds my scheduler will run again and it will fail to perform get operation. I just need to identify if redis went down at this point. Is there any way to identify if the redis went down in above mentioned scenario?

Tahir Mirza
  • 31
  • 1
  • 7
  • 2
    Does this answer your question? [Check connection Redis](https://stackoverflow.com/questions/25236198/check-connection-redis) – MarsAtomic May 16 '21 at 19:08
  • Jedis won’t throw an exception. However Redis, will so I recommend looking there. – aleksa_95 May 16 '21 at 19:10
  • Both the questions are quite different in terms of when I want to identify redis being down.Assume that redis was up and my connection pool was created successfully now f the redis went down and I have scheduler which does a get operation from redis every 30 seconds will fail to perform get operation in this, now how can I identify redis being down in my scheduler get call is what I am specifically looking for @MarsAtomic – Tahir Mirza May 16 '21 at 19:25
  • redis has a ping command which allow you to check if redis is responsive. – Tuan Anh Tran May 17 '21 at 03:29
  • @Tuan Anh Tran I am already doing a get operation, is there a need to use ping command apart from the get operation? Is there a way to check all possible exceptions when get itself fails instead of using one more network call for ping? – Tahir Mirza May 17 '21 at 06:34

2 Answers2

0

It took me sometime to figure out as I am new to redis but I found a simple way by catching the JedisConnectionException as this exception is thrown when redis server is down. Posting the answer if someone might face this scenario.

Tahir Mirza
  • 31
  • 1
  • 7
0

Connect to redis server via Jedis client and run info command to get more information:

    try (Jedis jedis = new Jedis("localhost", 6379)) {
        String result = new String((byte[]) jedis.sendCommand(Protocol.Command.INFO));
    ... 
    }
M2E67
  • 937
  • 7
  • 23