@@ -68,6 +68,58 @@ try (RedisClient client = RedisClient.create(redisURI)) {
68
68
}
69
69
```
70
70
71
+ ## Cluster topology refresh
72
+ The Redis Cluster configuration is dynamic and can change at runtime.
73
+ New nodes may be added, and the primary node for a specific slot can shift.
74
+ Lettuce automatically handles MOVED and ASK redirects, but to enhance your application's resilience, you should enable adaptive topology refreshing:
75
+
76
+ ``` java
77
+ RedisURI redisURI = RedisURI . Builder
78
+ .redis(" localhost" )
79
+ // set the global default from the default 60 seconds to 30 seconds
80
+ .withTimeout(Duration . ofSeconds(30 ))
81
+ .build();
82
+
83
+ // Create a RedisClusterClient with adaptive topology refresh
84
+ try (RedisClusterClient clusterClient = RedisClusterClient . create(redisURI)) {
85
+ // Enable TCP keep-alive and TCP user timeout just like in the standalone example
86
+ SocketOptions . TcpUserTimeoutOptions tcpUserTimeout = SocketOptions . TcpUserTimeoutOptions . builder()
87
+ .tcpUserTimeout(Duration . ofSeconds(20 ))
88
+ .enable()
89
+ .build();
90
+
91
+ SocketOptions . KeepAliveOptions keepAliveOptions = SocketOptions . KeepAliveOptions . builder()
92
+ .interval(Duration . ofSeconds(5 ))
93
+ .idle(Duration . ofSeconds(5 ))
94
+ .count(3 )
95
+ .enable()
96
+ .build();
97
+
98
+ SocketOptions socketOptions = SocketOptions . builder()
99
+ .tcpUserTimeout(tcpUserTimeout)
100
+ .keepAlive(keepAliveOptions)
101
+ .build();
102
+
103
+ // Enable adaptive topology refresh
104
+ // Configure adaptive topology refresh options
105
+ ClusterTopologyRefreshOptions topologyRefreshOptions = ClusterTopologyRefreshOptions . builder()
106
+ .enableAllAdaptiveRefreshTriggers()
107
+ .adaptiveRefreshTriggersTimeout(Duration . ofSeconds(30 ))
108
+ .build();
109
+
110
+ ClusterClientOptions options = ClusterClientOptions . builder()
111
+ .topologyRefreshOptions(topologyRefreshOptions)
112
+ .socketOptions(socketOptions). build();
113
+
114
+ clusterClient. setOptions(options);
115
+
116
+ StatefulRedisClusterConnection<String , String > connection = clusterClient. connect();
117
+ System . out. println(connection. sync(). ping());
118
+ connection. close();
119
+ }
120
+ ```
121
+ Learn more about topology refresh configuration settings in [ the reference guide] ( https://redis.github.io/lettuce/ha-sharding/#redis-cluster ) .
122
+
71
123
72
124
## DNS cache and Redis
73
125
0 commit comments