Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

During a deployment, callers get connection refused errors for about 30 to 90 seconds when instances of a Eureka-registered service shut down. Why, and how do you fix it?

The errors come from stale registry data: callers cache the Eureka registry, the server and the load balancer cache it too, and an instance is only removed after it deregisters or its lease expires, so for 30 to 90 seconds callers keep sending requests to instances that have stopped. The fix is to shut down in order (deregister first, wait for clients to refresh, then stop accepting requests), enable Spring Boot's graceful shutdown so in-flight requests finish, retry idempotent requests on another instance in the load balancer, and, carefully, shorten fetch and lease intervals at the cost of more load.

Why this question is asked

Connection refused errors during every deployment are a classic Eureka problem, and they are annoying precisely because nothing is broken: every component behaves as designed, but the combination leaves a gap. Interviewers ask this debugging question because solving it requires knowing how registration, caching and shutdown interact in time. In this answer, we will reproduce the timeline of a shutdown, see where the 30 to 90 seconds come from, and then apply the fixes in order of value: an orderly shutdown, graceful request draining, retries and tuning.

1A Quick Recap of How Callers Find Instances

Each instance of the service registers with the Eureka server and renews its lease with a heartbeat every 30 seconds by default. Callers do not ask Eureka on every request. Their Eureka client fetches the registry every 30 seconds by default and keeps a local copy, and Spring Cloud LoadBalancer, which picks an instance for each call, keeps its own cache of instances on top of that (its time to live is about 35 seconds by default; check your version). The Eureka server also caches the responses it gives to clients.

So information about an instance travels through several caches before it reaches the code that makes the call. That is fine for stable systems and exactly what causes trouble when instances disappear.

2What Happens When an Instance Shuts Down

Let us follow one instance during a rolling deployment. If the process is killed abruptly, it never deregisters, and Eureka keeps it in the registry until the lease expires, 90 seconds by default, plus the time for the eviction task to run. Even when the instance deregisters cleanly on shutdown, callers learn about it only when their next registry fetch happens, the server's response cache has been refreshed and the load balancer's cache expires. Meanwhile, the process has already closed its port, so every request sent there fails with connection refused.

Step throughWhy callers hit a stopped instance
CallerLB cacheEureka serverInstance B
deregister at t = 0 s
port closed at t = 1 s
pick instance at t = 5 s
B, from a cache refreshed 20 s ago
GET /orders -> connection refused
registry fetch at t = 28 s
list may still contain B (server cache)
cache expires at t = 60 s, B gone

8 messages in order. Play them, or step through them one at a time.

The exact times depend on when each cache was last refreshed; together they explain 30 to 90 seconds.

Notice that the problem has two parts. The registry is slow to say the instance is gone, and the instance stops serving immediately. Close either gap and most errors disappear; close both and they go away.

3Fix 1: Deregister First, Wait, Then Stop

The most effective fix is to reverse the order: the instance should leave the registry, keep serving for long enough that every caller's caches have refreshed, and only then stop. One way is to set the instance's status to OUT_OF_SERVICE through the service registry actuator endpoint, wait, and then shut down. On Kubernetes, the usual way is a preStop hook that sleeps for a while, combined with deregistration, because Kubernetes sends SIGTERM only after the hook finishes.

Java
# Kubernetes pod spec (excerpt)
lifecycle:
preStop:
exec:
# mark out of service, then keep serving while caches refresh
command: ["sh", "-c", "curl -s -X POST localhost:8080/actuator/serviceregistry -H 'Content-Type: application/json' -d '{\"status\":\"OUT_OF_SERVICE\"}'; sleep 60"]
terminationGracePeriodSeconds: 90 # must exceed preStop sleep + drain time
# without a deploy platform: same idea in the deployment script
# 1. set status OUT_OF_SERVICE 2. wait 60 s 3. send SIGTERM
Step throughOrderly shutdown of one instance
  1. Mark OUT_OF_SERVICE in Eurekaregistry stops offering this instance
  2. Keep serving and waitabout 60 s, longer than all cache refreshes
  3. Receive SIGTERMSpring Boot begins graceful shutdown
  4. Finish in-flight requestsserver.shutdown=graceful, timeout per phase
  5. Process exitsno caller is still sending traffic

5 steps in order. Play them, or step through them one at a time.

The wait should cover the longest cache chain, roughly the client fetch interval plus the load balancer cache time plus a margin, so 60 seconds is a common choice with default settings. The serviceregistry endpoint must be exposed in management.endpoints.web.exposure.include for this to work, and only on the management port, never publicly.

4Fix 2: Graceful Shutdown for In-Flight Requests

When the process does receive SIGTERM, requests may still be in progress. Spring Boot's graceful shutdown stops the web server from accepting new requests and gives existing ones time to complete before the application context closes. It is enabled with server.shutdown=graceful, which has been the default since Spring Boot 3.4 (earlier versions default to immediate, so check your version), and the waiting time is set with spring.lifecycle.timeout-per-shutdown-phase, 30 seconds by default.

Graceful shutdown alone does not fix the connection refused errors, because new requests arriving after the server stops accepting are still refused. It complements Fix 1: the wait handles new requests, graceful shutdown handles the ones already running.

YAML
server:
shutdown: graceful # default from Boot 3.4
spring:
lifecycle:
timeout-per-shutdown-phase: 20s # time for in-flight requests to finish

5Fix 3: Retry on Another Instance, and Tune Carefully

Some failures will remain, for example when an instance crashes. Spring Cloud LoadBalancer can retry a failed request on a different instance when Spring Retry is on the classpath; by default it retries only GET requests, and spring.cloud.loadbalancer.retry.max-retries-on-next-service-instance controls how many other instances it tries. Retries should only cover idempotent requests, ones that are safe to repeat, or requests carrying an idempotency key.

Finally, we can shorten the intervals: eureka.client.registry-fetch-interval-seconds on callers, eureka.instance.lease-renewal-interval-in-seconds and lease-expiration-duration-in-seconds on instances, the server's response cache interval, and the load balancer cache time to live. Halving them roughly halves the window, but roughly doubles the heartbeat and fetch load on the Eureka server, which with hundreds of instances can become significant. Tuning is a supplement to an orderly shutdown, not a replacement for it.

6Follow-up questions

Why does the problem last up to 90 seconds rather than 30?

Because the caches add up and an abruptly killed instance is only removed when its lease expires, 90 seconds by default with the eviction task running on its own schedule. A clean deregistration shortens it to the time the caller-side caches need to refresh, which is roughly 30 to 60 seconds with defaults.

Would moving to Kubernetes discovery remove the problem?

It shrinks it, because Kubernetes removes a terminating pod from the Service's endpoints quickly, but propagation to every node still takes a moment, so a short preStop sleep and graceful shutdown are still recommended there.

How do you verify the fix?

Run a rolling deployment under steady test traffic and count connection errors on the callers, comparing before and after. Tracing or load balancer metrics showing requests per instance should show traffic to the old instance falling to zero before it exits.

7Conclusion

In this answer, we saw that connection refused errors during deployments come from several layers of caching in Eureka, the client and Spring Cloud LoadBalancer, combined with instances that stop serving before callers learn they are gone. We fixed it by deregistering first and waiting before stopping, enabling graceful shutdown for in-flight requests, retrying idempotent requests on another instance, and tuning fetch and lease intervals carefully, trading registry load for freshness.

What to say in the interviewAbout 80 seconds aloud

Open with

The errors happen because callers do not see the instance disappear in time. The Eureka client on each caller fetches the registry every 30 seconds by default, the Eureka server caches its responses, and Spring Cloud LoadBalancer caches the instance list too.

Then, if they want more

An instance is removed only when it deregisters or its 90 second lease expires. Meanwhile the instance closes its port immediately, so for 30 to 90 seconds callers send requests to a stopped process and get connection refused.

My main fix is to reverse the order of shutdown: first mark the instance out of service or deregister it, then keep serving for about a minute while every caller's caches refresh, and only then stop. On Kubernetes I would do this with a preStop hook and a long enough termination grace period.

Second, I would make sure Spring Boot graceful shutdown is on, server.shutdown=graceful, which is the default in recent versions but worth checking, so in-flight requests finish. Third, I would enable load balancer retries on another instance for idempotent requests.

If needed I would also shorten the registry fetch, lease and cache intervals, but carefully, because that increases load on the Eureka server. Then I would verify with a rolling deploy under test traffic.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.4
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "During a deployment, callers get connection refused errors for about 30 to 90 seconds...": https://scrivio-interview-library.netlify.app/spring-boot/spring-cloud/d1af88a26f/