Spring Boot cannot connect to Redis and shows the wrong host
io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379Short answer
Spring Boot connects to localhost:6379 unless it finds Redis properties it knows. The usual reasons are old spring.redis.* keys that Boot 3 no longer reads, or a spring.data.redis.url that silently overrides host and port. Print the effective values, then use the spring.data.redis.* keys once.
Fix it fast
1Find out which cause you have
Search every place a Redis setting can live and compare what you find with the host named in the error.
grep -rn "redis" src/main/resources/ docker-compose.yml .env 2>/dev/null2Match what you see, then jump to its fix
What does the error look like?
io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis
Cause 1 of 2
The config still uses the old spring.redis.* keys
1Check
Spring Boot 3.0 moved the Redis properties from spring.redis.* to spring.data.redis.*. Keys that Boot does not know are ignored, so the default localhost:6379 is used.
grep -rn "spring.redis" src/main/resources/ .2Fix
Rename the keys to the spring.data.redis prefix.
spring.data.redis.host=redis.internal
spring.data.redis.port=6379
spring.data.redis.password=${REDIS_PASSWORD}3Verify
Restart the app. The error now names your host, or disappears.
./mvnw spring-boot:runConfirmed by github.com
Cause 2 of 2
A url property overrides host and port
1Check
Boot's documentation says that when spring.data.redis.url is set, the host, port, username and password properties are ignored.
grep -rn "spring.data.redis.url\|SPRING_DATA_REDIS_URL" src/main/resources/ .env docker-compose.yml2Fix
Use either the url or the separate properties, not both. Put the full address in the url, including credentials.
spring.data.redis.url=redis://user:secret@redis.internal:6379
spring.data.redis.database=03Verify
Restart and confirm the app connects to the address in the url.
./mvnw spring-boot:runConfirmed by docs.spring.io
How do I stop it coming back?
- Keep one source of Redis settings per environment, and delete the unused ones.
- After a Spring Boot major upgrade, read the migration guide's property section before starting the app.
- Check connectivity with redis-cli from the same host or container the app runs in.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Node.js cannot reach Redis: ECONNREFUSED on port 6379
Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
ECONNREFUSED on 6379 means no Redis server accepted the connection at the address the client used.
4 causes, each with a fixDatabases
H2 in PostgreSQL mode still behaves differently from PostgreSQL
Names or null ordering differ from PostgreSQL in test results
H2 compatibility modes are switched on in the JDBC URL and cover only a small part of the differences between databases.
4 causes, each with a fixDatabases
Hibernate fails on a CREATE or ALTER TABLE statement at startup
Error executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC Statement
Hibernate generated a CREATE, ALTER or DROP statement and MySQL refused it.
3 causes, each with a fixDatabases
Hibernate cannot initialize a lazy collection or proxy in Spring Boot
could not initialize proxy - no Session
A lazy collection or proxy is loaded on first access, and that needs an open Hibernate session.
3 causes, each with a fixDatabases
Jedis cannot borrow a connection from its pool
Caused by: java.net.SocketTimeoutException
The pool could not give the code a working connection in time.
3 causes, each with a fixDatabases
Java cannot log in to MySQL 8: authentication plugin caching_sha2_password not supported
ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
MySQL 8 accounts use the caching_sha2_password plugin by default, and Connector/J releases before 8.0.9 cannot use it.
3 causes, each with a fix