Spring Cloud LoadBalancer DNS service discovery lacks a supported TTL refresh default
0 reputation · 13 Jun 2026, 15:37 UTC
0 reputation · 13 Jun 2026, 15:37 UTC
In current Spring Boot and Spring Cloud releases, Spring Cloud LoadBalancer can resolve service instances through DNS, but the framework does not define its own DNS cache TTL or negative caching policy. Instead, resolution is delegated to the JVM’s InetAddress implementation, meaning cache lifetime and refresh behavior are governed by the networkaddress.cache.ttl and networkaddress.cache.negative.ttl system properties or the OS resolver.
Because those settings apply process-wide, an application cannot tune DNS freshness for load-balanced calls without affecting every other outbound connection in the same JVM. It is uncertain whether a supported Spring-level configuration exists, or whether teams must accept JVM-wide DNS caching, supply a custom resolver bean, or rely on an external DNS resolver.
Additionally, the HTTP client stack influences how resolution failures surface: RestTemplate with HttpURLConnection and WebClient with Reactor Netty may behave differently when cached DNS entries become stale, so the active client stack and JVM DNS properties must be verified. Without a documented Spring default, the cache strategy remains an application-level design decision.
What is the supported way to control DNS cache TTL refresh for Spring Cloud LoadBalancer DNS service discovery without altering JVM-wide resolver settings? Does Spring Boot expose an auto-configuration hook to override the resolver or cache policy used by WebClient and RestClient? How should teams reconcile differing DNS caching behavior between RestTemplate and WebClient when stale records cause intermittent failures?
35275 reputation · 13 Jun 2026, 20:24 UTC
InetAddress implementation, the caching behavior is globally globally by the JVM-wide system properties: networkaddress.cache.ttl and networkaddress.cache.negative.ttl.
To control DNS cache refresh without affecting the entire JVM, you must bypass the default JVM resolution mechanism by implementing a custom ServiceInstanceSupplier.
The most effective way to isolate DNS behavior for load balancing is to provide a custom bean that performs its own DNS resolution using an independent local cache (such as Caffeine). This allows you to define a specific TTL for service discovery calls only.
ServiceInstanceSupplier. Instead of relying on the default discovery, use a DNS library or manual lookups wrapped in a cache.@LoadBalancerConfiguration to provide your custom supplier to the context of the specific service name.WebClient (Reactor Netty), ensure the client is configured to use the custom LoadBalancer-aware client rather than resolving the hostname via the OS-level resolver directly.Stale records cause intermittent failures because RestTemplate (via HttpURLConnection) and WebClient (via Reactor Netty) interact with the underlying OS/JVM resolver differently. To reconcile these:
ServiceInstanceSupplier logic, regardless of the HTTP client-stack defaults.WebClient, you can configure the DnsNameProvider on the HttpClient to bypass the JVM default cache entirely, providing a consistent resolution strategy for reactive streams.RetrySpec at the client level. This handles the brief window where a DNS record has expired but the load balancer has not yet completed its refresh cycle.Assumption: This assumes you are using the DiscoveryClientServiceLoadBalancer. If using a specific registry like Eureka or Consul, the TTL issue shifts to the registry's heartbeat/cache interval rather than DNS.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.