Spring Data Redis
Spring Boot 3 auto-configures Lettuce connection factory, RedisTemplate, and cache abstraction when spring.data.redis.* properties are set — standardizing serialization, pooling…
Introduction
Spring Boot 3 auto-configures Lettuce connection factory, RedisTemplate, and cache abstraction when spring.data.redis.* properties are set — standardizing serialization, pooling, and health indicators across microservices.
Lettuce is async and thread-safe by default; share one connection factory bean per app. Configure timeout, SSL, and cluster/sentinel via properties or RedisConfiguration beans.
Wrong serializer causes ClassCastException in prod — define StringRedisTemplate for plain text and Jackson2JsonRedisSerializer consciously for objects.
Understanding the topic
Key concepts
- spring-boot-starter-data-redis dependency.
- LettuceConnectionFactory — thread-safe shared.
- RedisTemplate
generic operations API. - StringRedisTemplate — String keys/values.
- @EnableRedisRepositories optional.
- Actuator health redis indicator.
flowchart LRController --> ServiceService --> RedisTemplateRedisTemplate --> Redis
Step-by-step explanation
- Boot reads spring.data.redis.host/port/ssl.
- Auto-config creates ConnectionFactory bean.
- RedisTemplate wired with default serializers.
- Service injects RedisTemplate or StringRedisTemplate.
- Cache abstraction uses RedisCacheManager when enabled.
Syntax reference
Common commands
- Pool max-active sized to pod concurrency.
- Use spring.data.redis.cluster for Cluster.
- Custom beans override auto-config.
# application.ymlspring:data:redis:host: redis.internalport: 6379password: ${REDIS_PASSWORD}ssl:enabled: truelettuce:pool:max-active: 16
Informative example
Minimal Spring Boot 3 configuration with typed template:
@Configurationpublic class RedisConfig {@BeanStringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {return new StringRedisTemplate(factory);}@BeanRedisTemplate<String, Product> productRedisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Product> t = new RedisTemplate<>();t.setConnectionFactory(factory);t.setKeySerializer(new StringRedisSerializer());t.setValueSerializer(new Jackson2JsonRedisSerializer<>(Product.class));t.afterPropertiesSet();return t;}}
Java 21 records work with Jackson serializer. Register modules for Java time types.
Real-world use
Real-world use cases
- Standard Redis access layer in Spring microservices.
- Centralized serializer configuration.
- Sentinel/Cluster via properties.
- Health check in Kubernetes probes.
- Integration tests with Testcontainers Redis.
Best practices
- One ConnectionFactory singleton per app.
- Explicit serializers per template bean.
- Externalize secrets via env/vault.
- Enable SSL for managed Redis.
- Testcontainers in CI for Redis integration.
- Monitor pool exhausted metrics.
Common mistakes
- Default JDK serialization — opaque and fragile.
- New connection per request without pool.
- Missing timeout — threads hang on network partition.
- Mixing RedisTemplate serializers on same key prefix.
Advanced interview questions
Q1BeginnerDefault Spring Boot Redis client?
Q2BeginnerStringRedisTemplate vs RedisTemplate?
Q3IntermediateConfigure Redis Cluster in Spring?
Q4IntermediateLettuce vs Jedis?
Q5AdvancedProduction Redis Spring setup?
Summary
Spring Data Redis + Lettuce = Boot default stack.