Search code examples
javaredislettuceamazon-elasticache

Lettuce - Java - AWS ElastiCache - Redis - Connection Timeout


I am completely new to Redis and AWS ElastiCache service. I created a Redis cluster (5.0.6 v) with 1 Shard, 2 Nodes, No Encryption in-transit, No Encryption at-rest, No Redis Auth and Multi-AZ disabled.

I could see Primary Endpoint, Reader Enpoint and clicking on cluster, could see 2 node endpoints different than primary and reader endpoints.

I am using Lettuce with plain maven project to connect but getting Connection timeout. I tried with all combination of above endpoints no luck. I have added my ec2instance (where i am running code) to ElastiCache Subnet Groups. Still no luck.

Code Used:

RedisClusterClient clusterClient = RedisClusterClient.create(Arrays.asList(node1, node2));
StatefulRedisClusterConnection<String, String> connection = clusterClient.connect();
RedisAdvancedClusterAsyncCommands<String, String> asyncCommands = connection.async();

pom (only dependency):

 <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
            <version>5.2.2.RELEASE</version>
 </dependency>

I have use case with plain java-maven and no frameworks like spring boot

Appreciate for help.


Solution

  • If some one looking for similar question of connecting to ElastiCache primary, this may help: Modifying security groups inbound traffic and with below model of client creation solved:

    RedisURI upstreamUri = RedisURI.Builder.redis(primayNodeUrl, 6379).build();
    RedisClient redisClient = RedisClient.create();
    StatefulRedisMasterReplicaConnection<String, String> connection = 
    MasterReplica.connect(
                    redisClient,
                    StringCodec.UTF8,
                    upstreamUri);
    connection.setReadFrom(ReadFrom.REPLICA_PREFERRED);
    RedisAsyncCommands<String, String> asyncCommands = connection.async();