Connect to all nodes of a RedisCluster at startup #3197
-
Hello community, I am wondering if it is possible to open the connection to all nodes within a redis cluster on startup. Right now, we are calling Is it possible to have this method create connections to all nodes (essentially to warm them up)? Or is there another way to warm up connections to all nodes in the cluster? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
A somewhat similar question was asked in #1111 where warming the connections up could help reduce the problem they are having with manual flushing. By design the driver does not pre-warm cluster connections:
You could iterate over all connections of the cluster, as suggested by @mp911de in #1111 (comment) and - for instance - send a Have in mind - though - that if there is a topology change and a new node is added the connection to it would not be pre-warmed. In this case you might want to listen for ClusterTopologyChangedEvent too. Ultimately we might consider adding such a built-in functionality for edge cases like the one in #1111 but for now it is not planned. Does that help? |
Beta Was this translation helpful? Give feedback.
-
Going to write a succinct answer here for others who have the same question about warming up all Redis Cluster connections. As @tishun mentioned in the other answer,
In order to successfully do this, try the following code RedisClusterClient lettuceClient = RedisClusterClient.create(resources, redisURI);
StatefulRedisClusterConnection<byte[], byte[]> lettuceClusterConnection = lettuceClusterClient.connect(ByteArrayCodec.INSTANCE);
// Warm up connections to upstream (primary) nodes
lettuceClusterConnection.getPartitions().stream()
.filter(node -> node.getRole().isUpstream())
.forEach(node -> {
String hostname = node.getUri().getHost();
int port = node.getUri().getPort();
// Each node has a separate connection for read and write operations, so we need to warm up both here.
// Additionally, using the connections returned by getConnection(String nodeId, ConnectionIntent) is NOT the right connection
// to warm up as that connection is not used by redis operations, so warming up that connection does nothing.
// Instead, we must use the connections returned by getConnection(String host, int port, ConnectionIntent)
StatefulRedisConnection<byte[], byte[]> readConn = this.lettuceClusterConnection.getConnection(hostname, port, ConnectionIntent.READ);
StatefulRedisConnection<byte[], byte[]> writeConn = this.lettuceClusterConnection.getConnection(hostname, port, ConnectionIntent.WRITE);
readConn.sync().ping();
writeConn.sync().ping();
}); |
Beta Was this translation helpful? Give feedback.
Going to write a succinct answer here for others who have the same question about warming up all Redis Cluster connections.
As @tishun mentioned in the other answer,
In order to successfully do this, try the following code