Spring Boot Caching with Redis – Performance Optimization (Complete Guide)
In modern microservices, performance matters more than ever. Redis is a high-performance in-memory cache that significantly speeds up APIs, reduces DB load, and improves scalability.
- Why Redis is perfect for caching
- Spring Boot 3.x setup with Redis & Docker
- Cache annotations: @Cacheable, @CachePut, @CacheEvict
- TTL (cache expiry), key strategies, serialization formats
- Distributed caching + cache warming techniques
- Monitoring Redis Performance (CLI + Actuator + Grafana)
- Benchmark: DB vs Redis response time comparison
1️⃣ Why Redis for Spring Boot Caching?
| Feature | Why It Matters |
|---|---|
| In-Memory Speed | Up to 100x faster than DB queries |
| TTL (Time-to-Live) | Auto-remove stale data |
| Distributed Cache | Perfect for microservices & horizontal scaling |
| Supports JSON | Optimized for caching objects |
2️⃣ Setup Redis using Docker
docker run --name redis -p 6379:6379 -d redis
Verify Redis is running:
docker ps
redis-cli ping ➝ returns "PONG"
3️⃣ Spring Boot 3.x Project Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
4️⃣ Redis Configuration for JSON Caching
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public RedisCacheConfiguration cacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10))
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(
new GenericJackson2JsonRedisSerializer()
)
);
}
}
✔ Configures global TTL = 10 minutes
✔ Stores POJOs as JSON
5️⃣ Using Spring Cache Annotations – Real App Example
📌 Service: Cache DB Calls
@Service
public class ProductService {
@Autowired
private ProductRepository repo;
@Cacheable(value = "products", key = "#id")
public Product getProduct(Long id) {
simulateSlowService(); // DB call → expensive
return repo.findById(id).orElseThrow();
}
@CachePut(value = "products", key = "#product.id")
public Product updateProduct(Product product) {
return repo.save(product);
}
@CacheEvict(value = "products", key = "#id")
public void deleteProduct(Long id) {
repo.deleteById(id);
}
private void simulateSlowService() {
try { Thread.sleep(2000); } catch (InterruptedException ignored) {}
}
}
Next requests: 💥 Redis: < 10ms
6️⃣ Performance Benchmark: SQL vs Redis
| Operation | Database | Redis Cache |
|---|---|---|
| Single Product Fetch | 2,000ms | < 10ms |
| Load 1K products | 3-6s | < 100ms |
| Repeat calls | Re-query DB | No DB hit |
⏱ Up to 200x faster response times.
7️⃣ Cache Eviction Policies (Super Important)
- @CacheEvict – on update/delete
- TTL Expiry – auto clean stale data
- Cache Warming – preload most-used data on startup
@EventListener(ApplicationReadyEvent.class)
public void warmCache() {
getProduct(1L);
getProduct(2L);
}
8️⃣ Redis Monitoring (CLI + Dashboard)
redis-cli INFO stats
redis-cli MONITOR
Recommended tools:
- Spring Boot Actuator - RedisInsight (UI) - Prometheus + Grafana dashboards9️⃣ Cache Serialization Strategy (Don’t Ignore This)
| Format | Pros | Cons |
|---|---|---|
| JSON | Readable, flexible | Slightly slower |
| Binary(Java serialization) | Fast | Not portable |
| String | Simple | Only for primitive data |
Best choice = GenericJackson2JsonRedisSerializer ✔
🔟 Best Practices for Production
- Use proper TTL to prevent memory explosion
- Evict cached items after update/delete
- Use distributed cache for microservices
- Monitor cache hit ratio (aim ≥ 80%)
- Don’t cache: authentication tokens, rapidly changing data
Conclusion
Redis + Spring Boot = massive performance boost for high-load systems. With proper TTL, cache warming, monitoring & distributed strategy — Redis becomes the backbone of a scalable microservices architecture.
Implement these optimizations and you’ll drastically reduce DB load, cut latency, and improve user experience 🎯
🚀 Optimize Performance with Redis Caching
Redis caching is one of the most effective ways to improve application performance and scalability. Strengthen your understanding by exploring these closely related Spring Boot performance and system design topics.
🚀 Spring Boot Performance Tuning & Optimization
Learn how caching fits into overall Spring Boot performance strategies.
🗄️ Spring Boot Database Performance Tuning
Understand when to cache and when to optimize database queries instead.
⚡ High-Performance Spring Boot with Java 25
Combine Redis caching with JVM tuning and modern Java concurrency.
🚫 Blocking Calls & Scalability Issues
Learn how caching reduces blocking database calls under load.
🔄 Threading & Async Execution
Understand how cached responses improve thread utilization.
🏗️ Spring Boot Microservices Architecture
Use Redis as a shared cache in distributed microservices systems.
🏛️ Java System Design Interview Questions
Practice cache-related design decisions asked in senior system design interviews.