@@ -112,6 +112,51 @@ try (RedisClient client = RedisClient.create(redisURI)) {
112112}
113113```
114114
115+ ### Setting timeouts in Spring Data Redis
116+
117+ If you are using Spring Data Redis, you can set timeouts and keepalive settings using ` LettuceClientConfigurationCustomizer ` :
118+
119+ ``` java
120+ @Bean
121+ public LettuceClientConfigurationBuilderCustomizer lettuceClientConfigurationBuilderCustomizer() {
122+ return clientConfigurationBuilder - > {
123+ // Configure TCP User Timeout
124+ // This is useful for scenarios where the server stops responding without
125+ // acknowledging the last request
126+ SocketOptions . TcpUserTimeoutOptions tcpUserTimeout = SocketOptions . TcpUserTimeoutOptions . builder()
127+ .tcpUserTimeout(Duration . ofSeconds(20 ))
128+ .enable()
129+ .build();
130+
131+ // Configure TCP Keep-Alive
132+ // This is good for detecting dead connections where there is no traffic
133+ // between the client and the server
134+ SocketOptions . KeepAliveOptions keepAliveOptions = SocketOptions . KeepAliveOptions . builder()
135+ .interval(Duration . ofSeconds(5 )) // TCP_KEEPINTVL: interval between probes
136+ .idle(Duration . ofSeconds(5 )) // TCP_KEEPIDLE: time before first probe
137+ .count(3 ) // TCP_KEEPCNT: number of probes
138+ .enable()
139+ .build();
140+
141+ // Build SocketOptions with both TCP User Timeout and Keep-Alive
142+ SocketOptions socketOptions = SocketOptions . builder()
143+ .tcpUserTimeout(tcpUserTimeout)
144+ .keepAlive(keepAliveOptions)
145+ .build();
146+
147+ // Build ClientOptions with the configured SocketOptions
148+ ClientOptions clientOptions = ClientOptions . builder()
149+ .socketOptions(socketOptions)
150+ .build();
151+
152+ // Apply the client options and command timeout to the builder
153+ clientConfigurationBuilder
154+ .clientOptions(clientOptions)
155+ .commandTimeout(Duration . ofSeconds(30 )); // Global command timeout
156+ };
157+ }
158+ ```
159+
115160## Cluster topology refresh
116161
117162The Redis Cluster configuration is dynamic and can change at runtime.
0 commit comments