We are upgrading from spring boot version 1.5.2 to 2.0.0. I have found that spring data redis is using lettuce as default and requires no connection pool management and uses only single underlying tcp connection. We also use ConcurrentMetadatStore to do atomic/blocking operations like putIfAbsent. Is it ok to use auto configured LettuseConnectionFactory without connection pooling or configure LettuceConnectionFactory to use pooling?
Asked
Active
Viewed 2,340 times
1 Answers
0
By default, the LettuceConnectionFactory
is configured with default values for the client
and the pool
, note that Sentinel
and Cluster
modes use always connection-pooling regardless of the pooling setting.
In case you want to config the client
and the pool
with different values, you need to pass the new configuration to the LettuceConnectionFactory
, like this example for cluster mode:
private static final String COMMA = ",";
@Value("${redis.cluster.nodes}")
private String clusterNodes;
@Value("${redis.cluster.max-redirects}")
private Integer maxRedirects;
@Value("${redis.cluster.useDefaultPoolConfig}")
private boolean useDefaultPoolConfig;
@Value("${redis.password}")
private String password;
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
return loadFactory();
}
public LettuceConnectionFactory loadFactory() {
RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();
clusterConfiguration.setClusterNodes(getNodes(clusterNodes));
clusterConfiguration.setMaxRedirects(maxRedirects);
clusterConfiguration.setPassword(password);
if (useDefaultPoolConfig) {
return new LettuceConnectionFactory(clusterConfiguration);
} else {
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
// Values you need to change on client like
// .clientName("")
.poolConfig(buildLettucePoolConfig()).build();
return new LettuceConnectionFactory(clusterConfiguration, clientConfig);
}
}
protected GenericObjectPoolConfig buildLettucePoolConfig() {
// Values you need to change on PoolConfig
final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(maxTotal);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMinIdle(minIdle);
poolConfig.setTestOnBorrow(testOnBorrow);
poolConfig.setTestOnReturn(testOnReturn);
poolConfig.setTestWhileIdle(testWhileIdle);
poolConfig.setMinEvictableIdleTimeMillis(minEvictableMs);
poolConfig.setTimeBetweenEvictionRunsMillis(evictionRunsMs);
poolConfig.setMaxWaitMillis(maxWaitMills);
poolConfig.setNumTestsPerEvictionRun(numberTestsPerEvictionRun);
poolConfig.setBlockWhenExhausted(blockWhenExhausted);
return poolConfig;
}
private List<RedisNode> getNodes(String nodes) {
List<RedisNode> result;
if (StringUtils.isBlank(nodes)) {
result = Collections.emptyList();
} else {
result = new ArrayList<>();
for (String hostPort : nodes.split(COMMA)) {
result.add(RedisNode.fromString(hostPort));
}
}
return result;
}
Note, this code example run with
Spring-Data-Redis
version2.6.8
and above.

Ebraheem Alrabeea
- 2,130
- 3
- 23
- 42