Redis Tutorial 0/42 lessons ~6 min read Lesson 31

    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…

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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.
    text
    flowchart LR
    Controller --> Service
    Service --> RedisTemplate
    RedisTemplate --> Redis

    Step-by-step explanation

    1. Boot reads spring.data.redis.host/port/ssl.
    2. Auto-config creates ConnectionFactory bean.
    3. RedisTemplate wired with default serializers.
    4. Service injects RedisTemplate or StringRedisTemplate.
    5. 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.
    bash
    # application.yml
    spring:
    data:
    redis:
    host: redis.internal
    port: 6379
    password: ${REDIS_PASSWORD}
    ssl:
    enabled: true
    lettuce:
    pool:
    max-active: 16

    Informative example

    Minimal Spring Boot 3 configuration with typed template:

    java
    @Configuration
    public class RedisConfig {
    @Bean
    StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
    return new StringRedisTemplate(factory);
    }
    @Bean
    RedisTemplate<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?
    Lettuce (async, thread-safe).
    Q2BeginnerStringRedisTemplate vs RedisTemplate?
    StringRedisTemplate all String; RedisTemplate generic typed serializers.
    Q3IntermediateConfigure Redis Cluster in Spring?
    spring.data.redis.cluster.nodes list + Lettuce cluster config.
    Q4IntermediateLettuce vs Jedis?
    Lettuce async non-blocking default in Boot 3; Jedis blocking pooled legacy.
    Q5AdvancedProduction Redis Spring setup?
    Pooled Lettuce, TLS, ACL password, custom serializers, health check, timeouts, Testcontainers CI.

    Summary

    Spring Data Redis + Lettuce = Boot default stack.

    Ready to mark this lesson complete?Track your journey across the entire course.