elasticache with lettuce

2,054 views
Skip to first unread message

Jignesh Patel

unread,
Dec 16, 2020, 5:24:53 AM12/16/20
to lettuce-redis-client-users
We are using Ec2 instances and older version of redis.
Now we are in a process to migrate to Elasticache redis version 6.x.
It seems this is a right time to move to lettuce(instead of jedis), however I am not finding any example code in java to configure lettuce with elasticache using maven.

I do appreciate to get a pointer if there are sample examples available for me to begin with.

Mark Paluch

unread,
Dec 16, 2020, 5:34:59 AM12/16/20
to lettuce-redis-client-users
What have you tried so far to find an example? Did you check the reference documentation?

Jignesh Patel

unread,
Dec 16, 2020, 5:08:47 PM12/16/20
to Mark Paluch, lettuce-redis-client-users
Ok  I tried the example you provided and now getting following error:
Exception in thread "main" io.lettuce.core.RedisConnectionException: Unable to establish a connection to Redis Cluster
at io.lettuce.core.cluster.RedisClusterClient.lambda$assertInitialPartitions$32(RedisClusterClient.java:867)
at io.lettuce.core.cluster.RedisClusterClient.get(RedisClusterClient.java:886)
at io.lettuce.core.cluster.RedisClusterClient.assertInitialPartitions(RedisClusterClient.java:866)
at io.lettuce.core.cluster.RedisClusterClient.connect(RedisClusterClient.java:389)
at io.lettuce.core.cluster.RedisClusterClient.connect(RedisClusterClient.java:366)
at com.icare.cachemanager.ConnectToRedisCluster.main(ConnectToRedisCluster.java:13)
Caused by: io.lettuce.core.cluster.topology.DefaultClusterTopologyRefresh$CannotRetrieveClusterPartitions: Cannot retrieve cluster partitions from [redis://clustercfg.xxxxx.k3jurk.use1.cache.amazonaws.com]

Details:
[redis://clustercfg.xxxxx.k3jurk.use1.cache.amazonaws.com]: connection timed out: clustercfg.cachemanagerdev.k3jurk.use1.cache.amazonaws.com/172.xx.xx.xx:6379

Suppressed: io.lettuce.core.RedisConnectionException: Unable to connect to [clustercfg.xxxxx.k3jurk.use1.cache.amazonaws.com:6379]: connection timed out: clustercfg.xxxxx.k3jurk.use1.cache.amazonaws.com/172.xx.xx.xx::6379
at io.lettuce.core.cluster.topology.DefaultClusterTopologyRefresh.lambda$openConnections$11(DefaultClusterTopologyRefresh.java:339)
at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:774)
at java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:750)
at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:488)
at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1990)
at io.lettuce.core.AbstractRedisClient.lambda$initializeChannelAsync0$4(AbstractRedisClient.java:405)
at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:577)
at io.netty.util.concurrent.DefaultPromise.notifyListeners0(DefaultPromise.java:570)
at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:549)
at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:490)
at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:615)
at io.netty.util.concurrent.DefaultPromise.setFailure0(DefaultPromise.java:608)
at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:117)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:262)
at io.netty.util.concurrent.PromiseTask.runTask(PromiseTask.java:98)
at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:170)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
Caused by: io.netty.channel.ConnectTimeoutException: connection timed out: clustercfg.cachemanagerdev.k3jurk.use1.cache.amazonaws.com/172.xx.xx.xx::6379
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:261)
... 9 more

--
You received this message because you are subscribed to a topic in the Google Groups "lettuce-redis-client-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lettuce-redis-client-users/lSiTdzJyDhQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lettuce-redis-clien...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lettuce-redis-client-users/ac2b4081-c0ab-440b-bdcb-2736155353bdn%40googlegroups.com.

Jignesh Patel

unread,
Dec 16, 2020, 11:53:47 PM12/16/20
to Mark Paluch, lettuce-redis-client-users
This is what AWS told me


Due to transitEncryption is "Enabled" for the Redis, the connection needs establish via TLS.

In your sample code, I could not make sure the connection to the Redis is via TLS.



In lettuce documents, StartTLS is disabled by default. If you need to issue a StartTLS before you can use SSL, set the startTLS property of RedisURI to true. Please make sure the connection to the Redis is transitted via TLS.

1) TLS connection - https://lettuce.io/core/release/reference/#starttls

  RedisURI redisUri = RedisURI.Builder.redis(host(), sslPort()).withSsl(true).withStartTls(true).build();

2) Connecting to a Redis Cluster - https://github.com/lettuce-io/lettuce-core/wiki/Redis-Cluster#client-options
----------
  RedisURI redisUri = RedisURI.Builder.redis("localhost").withPassword("authentication").build();

  RedisClusterClient clusterClient = RedisClusterClient.create(redisUri);
  StatefulRedisClusterConnection<String, String> connection = clusterClient.connect();
  RedisAdvancedClusterCommands<String, String> syncCommands = connection.sync();

  ...

  connection.close();
  clusterClient.shutdown();



I am not sure how I use TLS uri and password based URI together.

Jignesh Patel

unread,
Dec 20, 2020, 11:14:42 PM12/20/20
to Mark Paluch, lettuce-redis-client-users
It still doesn't work.
New code:

public class ConnectToRedisCluster {
public static void main(String[] args) {

// Syntax: redis://[password@]host[:port]
// Syntax: redis://[username:password@]host[:port]
// RedisClusterClient redisClient = RedisClusterClient.create("redis://password@localhost:7379");
RedisDetails redisDetails = new RedisDetails();
// RedisURI redisURI = RedisURI.create(redisDetails.getHost(), );
RedisURI redisURI = RedisURI.Builder.redis(redisDetails.getHost(), redisDetails.getPort()).withSsl(true).withStartTls(true).build();
//if(properties.getIsRedisEncrypted()){
// redisURI.setSsl(Boolean.TRUE);
//redisURI.setStartTls(Boolean.TRUE);
//}
RedisClusterClient clusterClient = RedisClusterClient.create(redisURI);
        StatefulRedisClusterConnection<String, String> connection = clusterClient.connect();
RedisAdvancedClusterCommands<String, String> syncCommands = connection.sync();


        System.out.println("Connected to Redis");

connection.close();
clusterClient.shutdown();
}


}
The exception is as follows:


Caused by: io.lettuce.core.cluster.topology.DefaultClusterTopologyRefresh$CannotRetrieveClusterPartitions: Cannot retrieve cluster partitions from [redis+tls://cluster:6379]

Details:
[redis+tls://cluster:6379]: Unable to connect to [cluster:6379]: Connection closed prematurely

Suppressed: io.lettuce.core.RedisConnectionException: Unable to connect to [cluster:6379]: Connection closed prematurely

at io.lettuce.core.cluster.topology.DefaultClusterTopologyRefresh.lambda$openConnections$11(DefaultClusterTopologyRefresh.java:339)
at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:774)
at java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:750)
at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:488)
at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1990)
at io.lettuce.core.AbstractRedisClient.lambda$null$3(AbstractRedisClient.java:439)

at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:774)
at java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:750)
at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:488)
at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1990)
at io.lettuce.core.protocol.RedisHandshakeHandler.lambda$fail$4(RedisHandshakeHandler.java:131)
at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:577)
at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:551)
at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:490)


Any help.

Mark Paluch

unread,
Dec 22, 2020, 4:40:38 AM12/22/20
to lettuce-redis-client-users
Hi there, 

StartTLS is an option that emits "STARTTLS" in plain text to enable SSL, typically for legacy use cases, such as SMTP or proxies.

I think you should not enable start TLS, only SSL to fix the issue.

Cheers,
Mark

Reply all
Reply to author
Forward
0 new messages