redisspring-data-redislettuce

Redis connection pool configured using spring-data-redis but not working correctly


What I'm using:
spring-data-redis.1.7.0.RELEASE
Lettuce.3.5.0.Final

I configured Spring beans related to Redis as follows:

@Bean
public LettucePool lettucePool() {
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMasIdle(10);
    poolConfig.setMinIdle(8);
    ... // setting others..
    return new DefaultLettucePool(host, port, poolConfig)
}

@Bean
public RedisConnectionFactory redisConnectionFactory() {
    new LettuceConnectionFactory(lettucePool());
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
    RedisTemplate<Stirng, Object> redisTemplate = new RedisTemplate<String, Object>();
    redisTemplate.setConnectionFactory(redisConnectionFactory());
    redisTemplate.setEnableTransactionSupport(true);
    ... // setting serializers..
    return redisTemplate;
}

And redisTemplate Bean is autowired and used for Redis opertions.

The connections look correctly established when I check using an 'info' command via redis-cli. The client count is exactly the same with the value set to the lettucePool Bean + 1. (redis-cli is also a client)

However, my application's log says it sends operation requests through always the one same port. So I checked client status using 'client list' command and it says there are the pooling number of clients and just port is sending requests.

What am I missing?


Solution

  • This is caused by Lettuce's specific feature, 'sharing native connection'.

    LettuceConnectionFactory in spring-data-redis has a setter method named setShareNativeConnection(boolean), set to true by default. This means no matter how many connections are created and pooled, only one native connection is used as long as non-blocking and non-transactional operation called.

    As you can see, I didn't manually set the value so it was set to the default, 'true' and I had no blocking or transactional operations.

    Additionally, the reason why the default value is set to true is that Redis itself is single-threaded, which means even though clients send many operations simultaneously, Redis must execute them one by one, so settings this value to 'false' doesn't mean that it increases Redis' throughput.