'JedisConnectionException java.net.SocketTimeoutException when AWS instance hosting one of the five redis nodes fails

I am using Spring-data-redis(1.8.9) and Jedis(2.10.2) in my application.

I have 5 master and 5 slave REDIS cluster running on 5 EC2 instances in a shuffled way.

When one of the AWS EC2 instance fails the cluster recovers itself from failover by promoting slave(running on different EC2 instance) as master.

But the latency of call starts increasing with application intermittently throwing JedisConnectionException java.net.SocketTimeoutException exception

NOTE: Latency comes back to normal when EC2 instance is Up again

 public RedisConnectionFactory connectionFactory(){

        if(StringUtils.equalsIgnoreCase(redisClusterSwitch, "true"))
            return new JedisConnectionFactory(clusterConfiguration());
        else{
            JedisConnectionFactory jedisConFactory = new JedisConnectionFactory();
            jedisConFactory.setHostName(redisSingleNodeHost);
            jedisConFactory.setPort(port);
            return jedisConFactory;
        }
    }

    public RedisClusterConfiguration clusterConfiguration(){
        logger.info("Redis Cluster Nodes " + nodes);
        String delimiter = ",";
        List<String> hostAndPort = Arrays.asList(nodes.split(delimiter));
        RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration(hostAndPort);
        clusterConfig.setMaxRedirects(Integer.parseInt(maxRedirects));
        logger.info(String.valueOf(clusterConfig.getClusterNodes().size()));
        logger.info(clusterConfig.getClusterNodes().toString());
        return clusterConfig;
    }


    public RedisTemplate<String,Object> redisTemplate(){
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory());
        template.setEnableDefaultSerializer(false);
        template.setKeySerializer(new StringRedisSerializer());
        template.setEnableTransactionSupport(true);
        template.afterPropertiesSet();
        return template;
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source