2

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.

VIckyb
  • 161
  • 1
  • 9
  • 1
    Why you need multiple connections for Lettuce? Since Lettuce uses NIO networking model and async command for Redis operation, connection pool will not have a lot advantage than a single connection if there are not too much pipeline and transactions. – Gawain Jul 17 '20 at 03:41
  • I have get and set operations almost every 10/20 seconds – VIckyb Jul 17 '20 at 06:49

1 Answers1

2

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.

Ricardo Ferreira
  • 1,236
  • 3
  • 6