Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Spring Boot cannot connect to Redis and shows the wrong host

Errorio.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379

Short 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.

  • Applies to Spring Boot 3.4, Spring Data Redis 3.4, Lettuce 6, Java 21
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/null
  2. 2Match what you see, then jump to its fix

    1. 1The error names localhost:6379 although your file sets another hostThe config still uses the old spring.redis.* keysFix
    2. 2Host and port are set correctly, but the connection goes to a different addressA url property overrides host and portFix

What does the error look like?

  • io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
  • org.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:run

Confirmed by github.com

Did this fix it?

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.yml

2Fix

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=0

3Verify

Restart and confirm the app connects to the address in the url.

./mvnw spring-boot:run

Confirmed by docs.spring.io

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.