From 2ebbf1aa5c7680a2cd3eb07d72a47050385bc213 Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Thu, 20 Mar 2025 17:13:17 +0100 Subject: [PATCH 1/2] Lettuce: how to connect and configure Cluster connections --- content/develop/clients/lettuce/connect.md | 20 ++++++++ content/develop/clients/lettuce/produsage.md | 52 ++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/content/develop/clients/lettuce/connect.md b/content/develop/clients/lettuce/connect.md index 1aefc240dd..d610d36124 100644 --- a/content/develop/clients/lettuce/connect.md +++ b/content/develop/clients/lettuce/connect.md @@ -46,6 +46,26 @@ public class ConnectBasicTest { } ``` +## Connect to a Redis cluster + +To connect to a Redis cluster, use `RedisClusterClient`. + +```java +import io.lettuce.core.cluster.RedisClusterClient; +import io.lettuce.core.cluster.api.StatefulRedisClusterConnection; + +//... +try (RedisClusterClient clusterClient = RedisClusterClient.create(redisURI)) { + StatefulRedisClusterConnection connection = clusterClient.connect(); + + //... + + connection.close(); +} +``` + +Learn more about Cluster connections and how to configure them in [the reference guide](https://redis.github.io/lettuce/ha-sharding/#redis-cluster). + ## Asynchronous connection ```java diff --git a/content/develop/clients/lettuce/produsage.md b/content/develop/clients/lettuce/produsage.md index 7931ccd63a..91c0ea5500 100644 --- a/content/develop/clients/lettuce/produsage.md +++ b/content/develop/clients/lettuce/produsage.md @@ -68,6 +68,58 @@ try (RedisClient client = RedisClient.create(redisURI)) { } ``` +## Cluster topology refresh +The Redis Cluster configuration is dynamic and can change at runtime. +New nodes may be added, and the primary node for a specific slot can shift. +Lettuce automatically handles MOVED and ASK redirects, but to enhance your application's resilience, you should enable adaptive topology refreshing: + +```java +RedisURI redisURI = RedisURI.Builder + .redis("localhost") + // set the global default from the default 60 seconds to 30 seconds + .withTimeout(Duration.ofSeconds(30)) + .build(); + +// Create a RedisClusterClient with adaptive topology refresh +try (RedisClusterClient clusterClient = RedisClusterClient.create(redisURI)) { + // Enable TCP keep-alive and TCP user timeout just like in the standalone example + SocketOptions.TcpUserTimeoutOptions tcpUserTimeout = SocketOptions.TcpUserTimeoutOptions.builder() + .tcpUserTimeout(Duration.ofSeconds(20)) + .enable() + .build(); + + SocketOptions.KeepAliveOptions keepAliveOptions = SocketOptions.KeepAliveOptions.builder() + .interval(Duration.ofSeconds(5)) + .idle(Duration.ofSeconds(5)) + .count(3) + .enable() + .build(); + + SocketOptions socketOptions = SocketOptions.builder() + .tcpUserTimeout(tcpUserTimeout) + .keepAlive(keepAliveOptions) + .build(); + + // Enable adaptive topology refresh + // Configure adaptive topology refresh options + ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions.builder() + .enableAllAdaptiveRefreshTriggers() + .adaptiveRefreshTriggersTimeout(Duration.ofSeconds(30)) + .build(); + + ClusterClientOptions options = ClusterClientOptions.builder() + .topologyRefreshOptions(topologyRefreshOptions) + .socketOptions(socketOptions).build(); + + clusterClient.setOptions(options); + + StatefulRedisClusterConnection connection = clusterClient.connect(); + System.out.println(connection.sync().ping()); + connection.close(); +} +``` +Learn more about topology refresh configuration settings in [the reference guide](https://redis.github.io/lettuce/ha-sharding/#redis-cluster). + ## DNS cache and Redis From 6ef4447c520a076fb2e413e367fe8bea2582e5af Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Thu, 20 Mar 2025 17:59:47 +0100 Subject: [PATCH 2/2] Update content/develop/clients/lettuce/produsage.md Co-authored-by: andy-stark-redis <164213578+andy-stark-redis@users.noreply.github.com> --- content/develop/clients/lettuce/produsage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/develop/clients/lettuce/produsage.md b/content/develop/clients/lettuce/produsage.md index 91c0ea5500..c5a2324a6c 100644 --- a/content/develop/clients/lettuce/produsage.md +++ b/content/develop/clients/lettuce/produsage.md @@ -71,7 +71,7 @@ try (RedisClient client = RedisClient.create(redisURI)) { ## Cluster topology refresh The Redis Cluster configuration is dynamic and can change at runtime. New nodes may be added, and the primary node for a specific slot can shift. -Lettuce automatically handles MOVED and ASK redirects, but to enhance your application's resilience, you should enable adaptive topology refreshing: +Lettuce automatically handles [MOVED]({{< relref "/operate/oss_and_stack/reference/cluster-spec#moved-redirection" >}}) and [ASK]({{< relref "/operate/oss_and_stack/reference/cluster-spec#ask-redirection" >}}) redirects, but to enhance your application's resilience, you should enable adaptive topology refreshing: ```java RedisURI redisURI = RedisURI.Builder