CodexBloom - Programming Q&A Platform

Redis Performance Bottleneck with Spring Boot Caching in High Concurrency Scenarios

👀 Views: 1177 💬 Answers: 1 📅 Created: 2025-06-25
spring-boot redis caching Java

I can't seem to get Quick question that's been bugging me - I'm relatively new to this, so bear with me... I'm experiencing important performance optimization when using Redis as a cache in my Spring Boot application under high concurrency. The application frequently interacts with an external API, and I cache the responses in Redis to improve performance. However, when multiple users hit the API simultaneously, response times from Redis can spike to over 500ms, causing the overall user experience to degrade. Here's a simplified version of my caching implementation: ```java @Service public class ApiService { private final RedisTemplate<String, Object> redisTemplate; private final RestTemplate restTemplate; @Value("${api.url}") private String apiUrl; public ApiService(RedisTemplate<String, Object> redisTemplate, RestTemplate restTemplate) { this.redisTemplate = redisTemplate; this.restTemplate = restTemplate; } @Cacheable(value = "apiCache", key = "#query") public String fetchApiData(String query) { return restTemplate.getForObject(apiUrl + query, String.class); } } ``` I've also configured Redis with connection pooling, but I notice that the Redis connection usage spikes during heavy load, and I often see `org.springframework.data.redis.RedisConnectionException: Could not get a resource from the pool` errors in my logs. To mitigate this, I increased the maximum pool size in the `application.properties`: ```properties spring.redis.lettuce.pool.max-active=20 spring.redis.lettuce.pool.max-wait=10000 ``` But the performance hasn’t improved significantly. I've tried reducing the time-to-live for cached items and optimizing the external API call, yet I still observe that during peak times, the application experiences a noticeable lag when accessing the cached data. Should I consider any specific strategies for better handling concurrent requests, or is there a more efficient way to manage Redis connections in a high-load environment? Any insights would be greatly appreciated! For context: I'm using Java on Linux. Has anyone else encountered this? This is for a desktop app running on Ubuntu 20.04. I'd be grateful for any help. I'm coming from a different tech stack and learning Java. Could someone point me to the right documentation?