Measured on JDK 23 with a counting resolver: 30 seconds for answers, 10 for failures, and a stale window after DNS fails. Set the properties for failover.
Two caches between your code and DNS
When Java code calls InetAddress.getByName, or a client library does it for you, the answer can come from three places: the JDK's own name cache, the operating system's resolver and its cache, or a DNS server. The JDK cache sits in front of the others and has its own rules, set by security properties rather than by the TTL in the DNS record. That surprises people in two situations: after a failover that moves a service name to a new address, and when a dependency is briefly missing and every lookup for it fails for longer than the outage lasted.
This guide measures those rules on JDK 23.0.2 with a resolver that counts its calls, and shows how to set them for a service that needs to follow address changes without querying DNS on every connection.
What the properties say
The JDK reads three security properties, documented in the comments of conf/security/java.security:
networkaddress.cache.ttlis the number of seconds to cache a successful lookup. A negative value caches forever and zero disables caching. The shipped file leaves it commented out, and the comment explains the default: forever when a security manager is set, and in this implementation 30 seconds when no security manager is set.networkaddress.cache.negative.ttlis the number of seconds to cache a failed lookup. The shipped file sets it to 10, with a comment explaining that failed lookups in some Windows environments can take about five seconds.networkaddress.cache.stale.ttlis the number of seconds an expired entry may still be used when refreshing it fails. The default is 0, meaning never.
The networking properties page of the JDK 23 API documentation describes the same three properties and adds the point people miss: because they are part of the security policy, they are not set with the -D option or System.setProperty, but as security properties. Set them in a java.security override file passed with -Djava.security.properties=..., or with java.security.Security.setProperty before the first lookup.
The cache ignores the TTL that the DNS server returned. A record published with a 300-second TTL is still cached for 30 seconds by default, and a record with a 5-second TTL is still cached for 30.
A resolver that counts its calls
Since JDK 18, JEP 418 lets an application replace the resolver behind InetAddress with an InetAddressResolverProvider registered through ServiceLoader. That makes the cache observable: put a provider behind it that counts how often it is asked, and every lookup that does not reach the provider was served from the cache.
// Author: Sandeep Belgavi. Excerpt from jvm_dns_cache_probe/src/CountingResolverProvider.java
@Override public InetAddressResolver get(Configuration c) {
InetAddressResolver builtin = c.builtinResolver();
return new InetAddressResolver() {
@Override public Stream<InetAddress> lookupByName(String host, LookupPolicy p) throws UnknownHostException {
if (!host.endsWith(".bookstore.test")) return builtin.lookupByName(host, p);
CALLS.incrementAndGet();
if (failing || host.startsWith("missing")) throw new UnknownHostException(host);
return Stream.of(InetAddress.getByAddress(host, new byte[]{10, 0, 0, (byte) answer}));
}
@Override public String lookupByAddress(byte[] a) throws UnknownHostException { return builtin.lookupByAddress(a); }
};
}The probe registers the provider in META-INF/services/java.net.spi.InetAddressResolverProvider, sets the properties for a scenario, then calls InetAddress.getByName once per second for a synthetic name such as orders.bookstore.test and logs every time the provider is consulted. Names under .bookstore.test never reach real DNS.
What JDK 23.0.2 did
All runs used JDK 23.0.2 on Windows 11 without a security manager, one lookup per second.
| Setting | Lookups | Resolver calls | When the resolver was called |
|---|---|---|---|
| Defaults | 65 | 3 | 0.0, 30.3, 60.5 s |
cache.ttl=5 | 21 | 5 | 0.0, 5.1, 10.1, 15.2, 20.2 s |
cache.ttl=0 | 6 | 6 | every lookup |
| Defaults, name does not exist | 25 | 3 | 0.0, 10.2, 20.2 s |
The default positive cache held answers for 30 seconds, and the default negative cache held failures for 10 seconds, as the file describes. For a failed name that means 25 lookups produced 25 UnknownHostExceptions while the resolver was asked only three times. If the name had started resolving at second 1, the application would still have failed until second 10.
The stale setting needs a failure to show its effect. Two runs used cache.ttl=3, a working resolver for the first five seconds and a failing resolver afterwards:
- With the default
cache.stale.ttl=0, the last success was at 5 seconds. From 6 seconds, when the entry refreshed at 3.1 seconds expired and the refresh at 6.1 seconds failed, every lookup threwUnknownHostException. - With
cache.stale.ttl=10, lookups kept returning the cached address until 15 seconds, while the JDK kept trying to refresh every 3 seconds. From 16 seconds lookups failed, and the next resolver call came at 26.3 seconds: the failure was then held by the negative cache for its 10 seconds.
So the stale window turns a DNS outage shorter than the window into no outage at all for names the application already knew, at the price of using an address that might be out of date.
What this means for failover
Suppose the order service for the book store fails over and its name moves from one address to another. With the defaults, a JVM that resolved the name just before the change keeps using the old address for up to 30 seconds after the record changed, plus however long the operating system resolver cached it. If the client library also keeps connections in a pool, existing connections to the old address can live far longer; the name cache only matters when a new connection is opened.
Three settings follow from that.
For services behind a name that moves, such as a database endpoint that changes on failover or a load balancer whose addresses rotate, set networkaddress.cache.ttl to a value close to the record's TTL, typically 5 to 60 seconds. Do not set it to 0 unless you have measured the lookup cost; with caching disabled the probe called the resolver on every lookup, and in a real service that is a DNS query per new connection.
Consider whether 10 seconds of negative caching suits you. During a deployment that creates a name a moment after its clients start, a 10-second negative entry turns a one-second race into ten seconds of errors. Lowering it to 1 or 2 seconds shortens that, at the cost of more queries for names that really are missing. Setting it to 0 means a missing name is queried on every attempt, which can amplify load on DNS during an incident.
Use networkaddress.cache.stale.ttl when a short DNS outage is more likely than an address change. The property appears in the java.security file of JDK 21 and not in that of JDK 17; check your own JDK's file before relying on it, because a JDK that does not know the property simply ignores it.
Then connect the cache to connection lifetime. If a pool keeps connections open for an hour, the name cache is irrelevant to failover; set a maximum connection lifetime in the pool so connections are re-established, and re-resolved, regularly.
Setting the properties
The least surprising way is a small override file that the launcher passes on the command line:
# Author: Sandeep Belgavi. bookstore-dns.security
networkaddress.cache.ttl=10
networkaddress.cache.negative.ttl=2
networkaddress.cache.stale.ttl=30java -Djava.security.properties=/etc/bookstore/bookstore-dns.security -jar orders-client.jarSetting them in code also works, but only before anything in the process has resolved a name. The probe measured this: in a run that set networkaddress.cache.ttl=0 after the first lookup, six lookups reached the resolver once, so the default 30-second policy that was in force at the first lookup kept applying. In the other runs Security.setProperty is the first statement in main. In a large application, a library may resolve a name during class initialization before your code runs, so the file is the safer choice.
Verifying in your own service
Do not trust a configuration you have not observed. The counting provider in the companion example is a test fixture you can drop into an integration test: register it, set the properties exactly as production does, and assert on the number and timing of resolver calls. For a live service, compare DNS query logs or resolver metrics with connection-open counts; if queries are far fewer than new connections over a period longer than your TTL, something is caching longer than you think.
Limits of this guide
The property semantics come from the comments in JDK 23's java.security file. The timings come from one Windows 11 machine with a one-second lookup interval, so each measured time is accurate to about a second and includes scheduling jitter. The probe exercises only the JDK cache; the operating system resolver and any caching in client libraries are outside it.