From 8f37ecdfe38b23401ec7c52d1dd638fceb125447 Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Wed, 15 Oct 2025 18:17:08 +0200 Subject: [PATCH 1/2] Lettuce: Add example on enabling keep-alive in SDR --- content/develop/clients/lettuce/produsage.md | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/content/develop/clients/lettuce/produsage.md b/content/develop/clients/lettuce/produsage.md index 1eb47d517d..652b0f2e76 100644 --- a/content/develop/clients/lettuce/produsage.md +++ b/content/develop/clients/lettuce/produsage.md @@ -112,6 +112,51 @@ try (RedisClient client = RedisClient.create(redisURI)) { } ``` +### Setting timeouts in Spring Data Redis + +If you are using Spring Data Redis, you can set timeouts and keepalive settings using `LettuceClientConfigurationCustomizer`: + +```java +@Bean +public LettuceClientConfigurationBuilderCustomizer lettuceClientConfigurationBuilderCustomizer() { + return clientConfigurationBuilder -> { + // Configure TCP User Timeout + // This is useful for scenarios where the server stops responding without + // acknowledging the last request + SocketOptions.TcpUserTimeoutOptions tcpUserTimeout = SocketOptions.TcpUserTimeoutOptions.builder() + .tcpUserTimeout(Duration.ofSeconds(20)) + .enable() + .build(); + + // Configure TCP Keep-Alive + // This is good for detecting dead connections where there is no traffic + // between the client and the server + SocketOptions.KeepAliveOptions keepAliveOptions = SocketOptions.KeepAliveOptions.builder() + .interval(Duration.ofSeconds(5)) // TCP_KEEPINTVL: interval between probes + .idle(Duration.ofSeconds(5)) // TCP_KEEPIDLE: time before first probe + .count(3) // TCP_KEEPCNT: number of probes + .enable() + .build(); + + // Build SocketOptions with both TCP User Timeout and Keep-Alive + SocketOptions socketOptions = SocketOptions.builder() + .tcpUserTimeout(tcpUserTimeout) + .keepAlive(keepAliveOptions) + .build(); + + // Build ClientOptions with the configured SocketOptions + ClientOptions clientOptions = ClientOptions.builder() + .socketOptions(socketOptions) + .build(); + + // Apply the client options and command timeout to the builder + clientConfigurationBuilder + .clientOptions(clientOptions) + .commandTimeout(Duration.ofSeconds(30)); // Global command timeout + }; +} +``` + ## Cluster topology refresh The Redis Cluster configuration is dynamic and can change at runtime. From ae62794ab589e4c66845de2f9a65c2dfadbb7f3a Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Wed, 15 Oct 2025 18:21:20 +0200 Subject: [PATCH 2/2] Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@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 652b0f2e76..1696680163 100644 --- a/content/develop/clients/lettuce/produsage.md +++ b/content/develop/clients/lettuce/produsage.md @@ -114,7 +114,7 @@ try (RedisClient client = RedisClient.create(redisURI)) { ### Setting timeouts in Spring Data Redis -If you are using Spring Data Redis, you can set timeouts and keepalive settings using `LettuceClientConfigurationCustomizer`: +If you are using Spring Data Redis, you can set timeouts and keepalive settings using `LettuceClientConfigurationBuilderCustomizer`: ```java @Bean