We are trying to connect to AWS Elastic Redis Cluster using following code:
public class RedisConnection {
private JedisPooled jedisPooled;
public RedisConnection(final RedisConfiguration configuration) {
this.jedisPooled = new JedisPooled(new GenericObjectPoolConfig<>(), configuration.redisHostname(),
configuration.redisPort(),
2000,
configuration.redisPassword());
}
public JedisPooled getJedisPooled() {
return this.jedisPooled;
}
}
I am using the above class to read the data from redis cluster:
public class RedisCacheService implements CacheService {
private static final Logger LOG = LoggerFactory.getLogger(RedisCacheService.class);
private final RedisConnection redisConnection;
public RedisCacheService(final RedisConnection redisConnection) {
this.redisConnection = redisConnection;
}
@Override
public String getValue(final String key) {
LOG.debug("start to find cache with {}" , key);
final Optional<String> result = Optional.ofNullable(redisConnection.getJedisPooled().get(key));
return result.orElse(null);
}
}
Getting Read timed out error:
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.util.RedisInputStream.ensureFill(RedisInputStream.java:208)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.util.RedisInputStream.readByte(RedisInputStream.java:46)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.Protocol.process(Protocol.java:126)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.Protocol.read(Protocol.java:192)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:316)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:243)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.Connection.auth(Connection.java:372)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.Connection.initializeFromClientConfig(Connection.java:345)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.Connection.<init>(Connection.java:53)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.ConnectionFactory.makeObject(ConnectionFactory.java:71)
2022-09-16T13:13:19.883+02:00 at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:571)
2022-09-16T13:13:19.883+02:00 at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:298)
2022-09-16T13:13:19.883+02:00 at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:223)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.util.Pool.getResource(Pool.java:34)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.ConnectionPool.getResource(ConnectionPool.java:28)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.ConnectionPool.getResource(ConnectionPool.java:7)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.providers.PooledConnectionProvider.getConnection(PooledConnectionProvider.java:54)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.executors.DefaultCommandExecutor.executeCommand(DefaultCommandExecutor.java:23)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.UnifiedJedis.executeCommand(UnifiedJedis.java:148)
2022-09-16T13:13:19.883+02:00 at redis.clients.jedis.UnifiedJedis.get(UnifiedJedis.java:570)
2022-09-16T13:13:19.883+02:00 at com.govipul.learning.redis.impl.RedisCacheService.getValue(RedisCacheService.java:36)
The beauty is when i try to run the code from my local using tunnel to AWS cluster it works, but when i run the code from the EC2 instance it is failing. so it will be helpful if anyone can help on how can i run my code in EC2 instance.
The problem was related to SSL, i have enabled the SSL on the AWS Elastic redis side and hence the flag has to be enabled when creating the connection. The JedisPooled have a constructor with last parameter as Boolean SSL flag with value true/false.
this.jedisPooled = new JedisPooled(new GenericObjectPoolConfig<>(),
configuration.redisHostname(),
configuration.redisPort(),
2000,
configuration.redisPassword(),
true);