Skip to content

Redis auto-configuration connects too early before spring.redis.host is resolved #46455

@hj0328

Description

@hj0328

Describe the bug

When using spring-boot-starter-data-redis, the application attempts to connect to Redis during the early initialization phase — before the environment variables or application.yml values are resolved.

Even when spring.redis.host is defined via environment variables or configuration files, the connection still falls back to the default localhost:6379.

This causes Redis connection failures during startup, especially in containerized environments like Docker Compose.

Expected behavior

Redis auto-configuration should resolve spring.redis.host and other relevant properties before attempting to connect to Redis.

Actual behavior

In certain configurations (e.g. when using RedisMessageListenerContainer, or RedisTemplate in early beans), Redis tries to connect using the default value (localhost:6379), even though a different host (e.g. redis) was configured.

Steps to reproduce

  1. Create a Spring Boot app with spring-boot-starter-data-redis
  2. Configure Redis host via environment variable: SPRING_REDIS_HOST=redis
  3. Define a RedisMessageListenerContainer bean or use RedisTemplate early
  4. Start app in Docker Compose alongside a Redis container named redis
  5. App attempts to connect to localhost:6379 and fails

Workaround

Manually define the connection factory to ensure proper resolution timing:

@Bean
public RedisConnectionFactory redisConnectionFactory(
    @Value("${spring.redis.host}") String host,
    @Value("${spring.redis.port}") int port
) {
    return new LettuceConnectionFactory(host, port);
}

Environment

Spring Boot version: 3.2.x

Redis client: lettuce

Runtime: Docker Compose

OS: macOS (host)

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions