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
- Create a Spring Boot app with
spring-boot-starter-data-redis
- Configure Redis host via environment variable:
SPRING_REDIS_HOST=redis
- Define a
RedisMessageListenerContainer
bean or use RedisTemplate early - Start app in Docker Compose alongside a Redis container named
redis
- 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)
Comment From: wilkinsona
Thanks for the report. Spring Boot 3.2.x is no longer supported. Please upgrade to a supported version (3.4.x or later).
What you have described should not have happened as the Environment
(from which @ConfigurationProperties
and @Value
are resolved) is prepared before the application context and any of its beans have been created. If the problem remains after you have upgraded, please provide a minimal sample that we can easily run to reproduce the problem as there must be something unusual going on that is not clear from the steps provided thus far.
Comment From: domiyang
Seems the problem was that Spring Boot's auto-configuration wasn't properly picking up the Redis host/port from the application.properties file while creating the LettuceConnectionFactory.
output
test2 host/port from env: host=s232,port=6379
connectionFactory=org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory@7e0bc8a3
host/port from redisTemplate: host=localhost,port=6379
I had a simple setup: pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.upp</groupId>
<artifactId>redis</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.redis.host=s232
spring.redis.port=6379
RedisJavaTest2.java
package com.upp;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@SpringBootApplication
public class RedisJavaTest2 implements CommandLineRunner {
@Autowired
private Environment env;
public static void main(String[] args) {
SpringApplication.run(RedisJavaTest2.class, args);
}
@Override
public void run(String... args) {
redisTest2(args);
}
public void redisTest2(String... args) {
// String redisHost = (args.length > 0) ? args[0] : "s232";
// String redisPort = (args.length > 1) ? args[1] : "6379";
// System.out.println("Connecting to Redis at " + redisHost + ":" + redisPort);
// Spring Boot uses application.properties for host/port, so just show info
try {
System.out.println("test2 host/port from env: host=" + env.getProperty("spring.redis.host") + ",port="
+ env.getProperty("spring.redis.port"));
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
System.out.println("connectionFactory=" + connectionFactory);
if (connectionFactory instanceof org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) {
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory lcf = (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) connectionFactory;
System.out.println(
"host/port from redisTemplate: host=" + lcf.getHostName()
+ ",port=" + lcf.getPort());
}
connectionFactory.afterPropertiesSet();
System.out.println("after ps");
RedisTemplate<String, String> template = new RedisTemplate<>();
System.out.println("template=" + template);
template.setConnectionFactory(connectionFactory);
System.out.println("after set cf");
template.setDefaultSerializer(StringRedisSerializer.UTF_8);
template.afterPropertiesSet();
template.opsForValue().set("foo", "bar");
System.out.println("Value at foo:" + template.opsForValue().get("foo"));
connectionFactory.destroy();
} catch (Exception e) {
System.err.println("Failed to connect or operate on Redis: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
}
the output:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.5.0)
2025-07-19T11:52:02.670+08:00 INFO 40780 --- [ main] com.upp.RedisJavaTest2
: Starting RedisJavaTest2 using Java 17.0.12 with PID 40780 (C:\data\gitlab\redis\target\classes started by bill in C:\data\gitlab\redis)
2025-07-19T11:52:02.673+08:00 INFO 40780 --- [ main] com.upp.RedisJavaTest2
: No active profile set, falling back to 1 default profile: "default"
2025-07-19T11:52:03.022+08:00 INFO 40780 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2025-07-19T11:52:03.024+08:00 INFO 40780 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2025-07-19T11:52:03.048+08:00 INFO 40780 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 11 ms. Found 0 Redis repository interfaces.
2025-07-19T11:52:03.525+08:00 INFO 40780 --- [ main] com.upp.RedisJavaTest2
: Started RedisJavaTest2 in 1.195 seconds (process running for 1.48)
test2 host/port from env: host=s232,port=6379
connectionFactory=org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory@7e0bc8a3
host/port from redisTemplate: host=localhost,port=6379
after ps
template=org.springframework.data.redis.core.RedisTemplate@33634f04
after set cf
Failed to connect or operate on Redis: Unable to connect to Redis
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1858)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1789)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1586)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.lambda$getConnection$0(LettuceConnectionFactory.java:1566)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.doInLock(LettuceConnectionFactory.java:1527)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1563)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedConnection(LettuceConnectionFactory.java:1249)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getConnection(LettuceConnectionFactory.java:1055)
at org.springframework.data.redis.core.RedisConnectionUtils.fetchConnection(RedisConnectionUtils.java:195)
at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:144)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:105)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:400)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:380)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:117)
at org.springframework.data.redis.core.DefaultValueOperations.set(DefaultValueOperations.java:200)
at com.upp.RedisJavaTest2.redisTest2(RedisJavaTest2.java:56)
at com.upp.RedisJavaTest2.run(RedisJavaTest2.java:24)
at org.springframework.boot.SpringApplication.lambda$callRunner$5(SpringApplication.java:789)
at org.springframework.util.function.ThrowingConsumer$1.acceptWithException(ThrowingConsumer.java:82)
at org.springframework.util.function.ThrowingConsumer.accept(ThrowingConsumer.java:60)
at org.springframework.util.function.ThrowingConsumer$1.accept(ThrowingConsumer.java:86)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:788)
at org.springframework.boot.SpringApplication.lambda$callRunners$3(SpringApplication.java:773)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:357)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:510)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:773)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:325)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1362)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1351)
at com.upp.RedisJavaTest2.main(RedisJavaTest2.java:19)
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:6379
at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:63)
at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:41)
at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:354)
at io.lettuce.core.RedisClient.connect(RedisClient.java:220)
at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.lambda$getConnection$1(StandaloneConnectionProvider.java:112)
at java.base/java.util.Optional.orElseGet(Optional.java:364)
at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:112)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1787)
... 35 more
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: localhost/127.0.0.1:6379
Caused by: java.net.ConnectException: Connection refused: no further information
at java.base/sun.nio.ch.Net.pollConnect(Native Method)
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:336)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:339)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:784)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:732)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:658)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:998)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:842)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.854 s
[INFO] Finished at: 2025-07-19T11:52:04+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.5.0:run (default-cli) on project redis: Process terminated with exit code: 1 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionExceptio