Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

Spring Boot Redis Caching Creating Many New Redis Connections

$
0
0

I am using the built in Spring Boot 1 Redis library with the @Cacheable annotation to cache some values in Redis. The Redis cluster is hosted in AWS and is unclustered with SSL and password enabled.

An example of my usage of the annotation is:

    @Cacheable(value = "example", key = "'example-'.concat(#id.toString())")    public List<DTO> getById(Long id) {        List<DBObject> dbObjectList = dbClient.getById(id);        return dbObjectList.stream()            .map(dbObject -> transformer.transformToDTO(dbObject))            .collect(Collectors.toList());    }

I am using Lettuce to connect to the cluster because there are issues with Spring Boot 1 Jedis and connecting via SSL. My Redis configuration is:

    @Bean    public RedisConnectionFactory redisConnectionFactory() {        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();        lettuceConnectionFactory.setHostName(host);        lettuceConnectionFactory.setPort(port);        lettuceConnectionFactory.setPassword(password);        lettuceConnectionFactory.setUseSsl(ssl);        return lettuceConnectionFactory;    }    @Bean    public static <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory redisConnectionFactory,                                                             ObjectMapper objectMapper) {        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(objectMapper);        RedisTemplate<String, T> template = new RedisTemplate<>();        template.setConnectionFactory(redisConnectionFactory);        template.setKeySerializer(new StringRedisSerializer());        template.setValueSerializer(genericJackson2JsonRedisSerializer);        template.setHashKeySerializer(new StringRedisSerializer());        template.setHashValueSerializer(genericJackson2JsonRedisSerializer);        return template;    }

where host, port, password, and ssl are all configured via application.yml/environment properties.

My Redis cache is configured via:

    @Bean    public <T> RedisCacheManager redisCacheManager(RedisTemplate<String, T> redisTemplate) {        final List<String> cacheNamesList = Arrays.asList(cacheName);        final RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate, cacheNamesList);        final Map<String, Long> expiresMap = new HashMap<>();        expiresMap.put(cacheName, cacheTTL);        redisCacheManager.setExpires(expiresMap);        return redisCacheManager;    }

This is all working as expected and values are getting cached correctly with the correct TTL. The issue I am encountering is that AWS Redis metrics are showing a lot of new connections created (Upwards of 2000 new connections in a minute according to Cloudwatch metrics).

I was under the assumption that Lettuce shares connections between threads but it seems like that is not the case. Is my Lettuce configuration set up incorrectly? I think I can probably fix this by upgrading to Spring Boot 2 but that is not a trivial upgrade because it is a fairly large application and I was hoping that I could fix this in Spring Boot 1.

Are these new connections not an issue? I know Redis does have connection limits but it doesn't look like my application is anywhere near the limit.

AWS Metrics


Viewing all articles
Browse latest Browse all 2204

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>