skip to Main Content

I’m using lettuce as my connectionFactory in a spring-boot application to connect to redis. It creates a new connection every time before an operation. Checked with the MONITOR command and profiler, there’s a PING command sent before every operation. Attaching the configuration I used below.

public LettuceConnectionFactory redisConnectionFactory() throws FileNotFoundException {
        final SocketOptions socketOptions = SocketOptions.builder()
                .connectTimeout(Duration.ofMillis(connectionTimeout)).keepAlive(true).build();
        final SslOptions sslOptions = sslEnabled ? SslOptions.builder().jdkSslProvider()
                .trustManager(ResourceUtils.getFile(truststorePath)).build() : null;
        final ClientOptions clientOptions = sslEnabled
                ? ClientOptions.builder().sslOptions(sslOptions).socketOptions(socketOptions).build()
                : ClientOptions.builder().socketOptions(socketOptions).build();
        LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
                .commandTimeout(Duration.ofMillis(timeout)).clientOptions(clientOptions).useSsl().build();
        RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration(hostname,
                port);
        serverConfig.setPassword(accessKey);
        final LettuceConnectionFactory lettuceConnectionFactory =
                new LettuceConnectionFactory(serverConfig, clientConfig);
        lettuceConnectionFactory.setValidateConnection(true);
        lettuceConnectionFactory.setEagerInitialization(true);
        lettuceConnectionFactory.afterPropertiesSet();
        return lettuceConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Long> redisTemplate() throws FileNotFoundException {
        RedisTemplate<String, Long> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Long.class));
        return redisTemplate;
    }

Is there a way to reuse the lettuce connection?

2

Answers


  1. You can relay on Connection pooling which according to the wiki: one connection can be shared amongst multiple threads and Lettuce connections auto-reconnection by default

    So you can use pool.borrowObject() to get a possible present connection for your operations.

    Login or Signup to reply.
  2. You can validate against documentation that factory create a new lettuceConnection each time it calls a getConnection() method.

    onnection factory creating Lettuce -based connections.
    This factory creates a new LettuceConnection on each call to getConnection(). Multiple LettuceConnections share a single thread-safe native connection by default.

    lettuceFactory does in fact calls getConnection() on each request but as per documentation multiple connections does share a single thread-sage native connection.

    so I don’t think that should be concern for resource.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search