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
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionDefault Spring Boot Redis client?+
Answer
2BeginnerQuestionStringRedisTemplate vs RedisTemplate?+
Answer
3IntermediateQuestionConfigure Redis Cluster in Spring?+
Answer
4IntermediateQuestionLettuce vs Jedis?+
Answer
5AdvancedQuestionProduction Redis Spring setup?+
Answer
Summary
Spring Data Redis + Lettuce = Boot default stack.