简体   繁体   中英

Spring data Redis Cluster Lettuce Connection Settings

We are using spring data redis with lettuce, lettuce uses single connection but in web application it is better to use connection pool as per my assumption. Below is the code for java config

 @Configuration
    @ComponentScan(basePackages = { "com.test.*" })
    public class AppConfig {
    
        @Bean
        public LettuceConnectionFactory getLettuceConnectionFactory() {
              List<String> clusterNodes = Arrays.asList("redis-cluster----0001-001.redis-cluster---.usw2.cache.amazonaws.com:6379", "redis-cluster----0001-002.redis-cluster---.usw2.cache.amazonaws.com:6379");
              final LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(new RedisClusterConfiguration(clusterNodes));
              lettuceConnectionFactory.setTimeout(10000);
              lettuceConnectionFactory.setUseSsl(true);
              lettuceConnectionFactory.setVerifyPeer(false);
              lettuceConnectionFactory.setStartTls(false);
              lettuceConnectionFactory.afterPropertiesSet();
              return lettuceConnectionFactory;
        }
        
        @Bean
        public RedisTemplate<String, Object> redisTemplate() {
              final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
              redisTemplate.setKeySerializer(new StringRedisSerializer());                                           
              redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
              redisTemplate.setConnectionFactory(getLettuceConnectionFactory());
              return redisTemplate;
        }
    
    }

Since we are using Spring data redis 1.8.23 and Lettuce 4.5.0.final, we cannot use LettucePoolingClientConfiguration.
Does making use of DefaultLettucePool for AWS Elastic Cache is good optoin, what is disadvantage of setting using setShareNativeConnection to false.

Any other better option for to have connection pool.

Does making use of DefaultLettucePool for AWS Elastic Cache is good option

From the functional perspective it works just fine the only problem is that it has been deprecated and thus, using it creates a technical debt.

what is disadvantage of setting using setShareNativeConnection to false

It will increase the network I/O of your application since every operation will open and close a socket. Keeping it to true ensures that multiple LettuceConnection objects could reuse the native connection.

As an alternative, try to use Jedis as your engine instead of Lettuce. I've had some great performance using this library and it supports connection pooling as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM