WebClient DNS caching behavior and TTL inconsistency
0 reputation · 07 Jan 2020, 13:55 UTC
0 reputation · 07 Jan 2020, 13:55 UTC
Spring WebClient delegates network operations to the underlying Reactor Netty HttpClient. While the JVM provides global DNS caching controls via system properties, different HTTP client implementations may handle DNS Time-To-Live (TTL) and record expiration independently of the standard JVM settings.
When deploying in dynamic environments where service IP addresses change frequently, relying on the default JVM sun.net.inetaddr.ttl may not guarantee that the underlying Netty name resolver honors the same caching logic, potentially leading to stale connection attempts.
33775 reputation · 08 Jan 2020, 01:34 UTC
Short answer: No. Spring WebClient (via Reactor Netty) does not honor JVM-wide DNS TTL properties such as sun.net.inetaddr.ttl. The transport uses Netty’s DnsNameResolver, which maintains an independent cache. JVM properties only affect java.net.InetAddress lookups, not the Netty resolver that WebClient uses for outbound connections.
If you set -Dsun.net.inetaddr.ttl=30 and still see stale IPs, the cause is almost certainly that the Netty resolver is caching addresses with its own TTL policy. In current Reactor Netty versions, the default positive-cache TTL is effectively unbounded (often documented as Integer.MAX_VALUE) unless explicitly configured. Negative caching (NXDOMAIN) defaults to zero seconds. These are resolver-level defaults, not JVM-wide settings. Confirm your exact Reactor Netty version, because resolver defaults and configuration APIs can change across releases.
You can scope the cache policy to a single HttpClient without touching JVM system properties.
HttpClient httpClient = HttpClient.create()
.resolver(spec -> spec
.queryTimeout(Duration.ofSeconds(5))
.cache(Duration.ofMinutes(1), Duration.ofSeconds(0)));
WebClient webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient))
.build();
Here cache(maxTtl, minTtl) controls how long successful lookups are retained. The queryTimeout is a separate DNS query timeout, not a cache TTL.
If you are using Spring Boot 3.x (Reactor Netty 1.1+), auto-configured WebClient builders can pick up global DNS cache settings via properties such as spring.reactor.netty.http.client.dns-cache-ttl and spring.reactor.netty.http.client.dns-cache-negative-ttl. These apply to auto-configured clients, not to manually constructed HttpClient instances. Verify the exact property names against your Boot version’s reference documentation.
-Dio.netty.resolver.dns.trace=true to see query and cache activity.HttpClient actually used by WebClient.Are you running Spring Boot 3.x with auto-configured WebClient, or are you constructing HttpClient manually? That determines whether the property shortcut is available or whether you must use the programmatic resolver spec.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.