Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

How does the JVM cache DNS lookups? Explain networkaddress.cache.ttl and networkaddress.cache.negative.ttl and what happens in containers that call services with changing IPs.

The JVM keeps its own cache of DNS answers inside InetAddress, for both successful and failed lookups. networkaddress.cache.ttl is a security property that sets how many seconds a successful answer is kept, about 30 seconds by default in current JDKs when no security manager is installed, and networkaddress.cache.negative.ttl sets how long a failure is kept, 10 seconds by default. The JVM ignores the TTL of the DNS record itself, so in containers calling services whose IPs change, the cache TTL must be short enough to follow the change.

Why this question is asked

Most of the time we never think about DNS in Java: we pass a host name to an HTTP client or a JDBC URL and it works. Then a load balancer changes its addresses or a database fails over, and one Java service keeps talking to an address that no longer exists while everything else has moved on. Interviewers ask this question because the JVM has its own DNS cache with its own rules, and those rules are different from what the DNS server says. In this answer, we will recap how a name becomes an address in Java, look at the positive and negative caches and their properties, see how to set them correctly, and finish with what this means in containers and cloud environments.

1A Quick Recap: From Host Name to IP Address

Every network connection needs an IP address. When we connect to orders.internal.acme.com, Java calls InetAddress.getByName() or getAllByName(), which asks the operating system's resolver, which asks a DNS server. The DNS answer comes with a TTL, a time to live in seconds, telling clients how long they may reuse the answer before asking again.

The JVM adds a layer on top of this: InetAddress keeps a cache of results in memory for the whole process. The cache saves time, since a lookup can take milliseconds and a busy service makes thousands of connections, but it has one surprising property: the JVM does not use the TTL from the DNS record. It uses its own fixed TTL from configuration. A record published with a 5-second TTL can still be cached for 30 seconds, or forever, depending on how the JVM is set up.

2The Positive Cache: networkaddress.cache.ttl

Successful lookups are kept for networkaddress.cache.ttl seconds. This is a security property, not a system property: it lives in the java.security file in the JDK's conf/security directory, and we change it there, through an override file, or with java.security.Security.setProperty() in code. A value of -1 means cache forever, and 0 means do not cache at all.

The default has a history worth knowing. If a security manager is installed, the default is -1, forever, a choice made long ago to stop DNS spoofing attacks from changing an address after a security check. When no security manager is installed, which is the normal case and the only case since the security manager was permanently disabled in JDK 24, the JDK uses an implementation-specific default of 30 seconds. Old JDK versions and some vendor builds behaved differently, so check your version. The legacy system property sun.net.inetaddr.ttl does the same thing and is used only when the security property is not set.

Java
System.out.println(Security.getProperty("networkaddress.cache.ttl")); // null: built-in default (30 s)
System.out.println(Security.getProperty("networkaddress.cache.negative.ttl")); // 10
// Set it in code, as early as possible, before the first lookup
Security.setProperty("networkaddress.cache.ttl", "10");

Notice the first line prints null. The java.security file ships with networkaddress.cache.ttl commented out, which is how the implementation default applies. Setting it in code works, but only reliably before the first lookup, and code in main() can run after a framework or a library has already resolved names. For that reason a file-based override is the safer route, as we will see.

3The Negative Cache: networkaddress.cache.negative.ttl

Failed lookups are cached too. If a name does not resolve, the JVM remembers the failure for networkaddress.cache.negative.ttl seconds, 10 by default, and any lookup in that window fails immediately with UnknownHostException without asking DNS again.

Java
InetAddress.getByName("new-service.internal"); // UnknownHostException: record not created yet
// the record is created two seconds later
InetAddress.getByName("new-service.internal"); // still UnknownHostException, from the negative cache
// ... after 10 seconds
InetAddress.getByName("new-service.internal"); // 10.4.2.17
Step throughWhat InetAddress.getByName does
  1. Is the name in the JVM cache?
    YesCheck its ageNoAsk the OS resolver
  2. Has the entry expired?
    YesAsk the OS resolverNoReturn cached result
  3. Did the lookup succeed?
    YesCache for networkaddress.cache.ttlNoCache failure for negative.ttl
  4. Return addresses or throw UnknownHostException

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

The DNS record's own TTL plays no part in how long the JVM keeps the answer.

The reason for the negative cache is protection: a broken name should not cause a storm of DNS queries from every thread. The cost is a short delay after a name appears, which matters during deployments when a service starts calling a name that is being created at the same moment. Setting the value to 0 disables it, but a few seconds is usually a good compromise.

4Setting the Values the Right Way

Because these are security properties, -Dnetworkaddress.cache.ttl=10 on the command line does nothing; it sets a system property with that name, which the cache never reads. The reliable ways are an override file or a change to the JDK's java.security in the image.

Properties
# /opt/acme/net.security
networkaddress.cache.ttl=10
networkaddress.cache.negative.ttl=3
# Start the JVM with the override (appends to and overrides java.security)
java -Djava.security.properties=/opt/acme/net.security -jar app.jar
# Legacy alternative, a real system property, used only if the security property is unset
java -Dsun.net.inetaddr.ttl=10 -jar app.jar

Putting the override in the base image's JAVA_TOOL_OPTIONS makes it apply to every Java process in the container, including tools and agents, and keeps it out of application code. Newer JDKs also added networkaddress.cache.stale.ttl, which lets the JVM keep using an expired answer for a while if a refresh fails; check your version before relying on it.

5What This Means in Containers and the Cloud

In the old world, a database lived on a fixed server and its address never changed, so caching forever was harmless. Today addresses move constantly. Cloud load balancers scale by adding and removing nodes, so the set of IP addresses behind their DNS name changes during normal operation. Managed databases fail over by pointing a DNS name at a new instance. In Kubernetes, a headless service's name resolves to pod IPs that change with every deployment.

So the JVM cache TTL must be short enough to follow these changes. With the 30-second default, a client follows a change within about half a minute, which is usually acceptable. With a cache-forever setting, which still appears in old images and old JDK configurations, a client keeps the old address until it restarts, and cloud providers explicitly recommend a TTL of 60 seconds or less for Java clients. But there is a catch: the DNS cache only affects new lookups. Connections already open in a pool keep their old address regardless of the TTL, so following an IP change also needs connections with a limited lifetime, which is the subject of the debugging question that usually follows this one.

6Follow-up questions

Does the JVM honour the TTL in the DNS record?

No. The built-in resolver caches answers for the configured networkaddress.cache.ttl, whatever the record says. A custom resolver, possible through the InetAddressResolver service provider interface added in JDK 18, could honour record TTLs, and some libraries with their own resolvers, such as Netty-based clients, do, so check the client in use.

Is there any other cache between the JVM and DNS?

Possibly. The operating system may cache through nscd or systemd-resolved, and in Kubernetes the node-local DNS cache and CoreDNS cache answers too. Usually these respect record TTLs, but they are worth checking when an address change takes longer than expected.

Why not set the TTL to 0?

Every connection would then do a DNS lookup, which adds latency and load on the DNS servers, and a DNS outage would immediately break every new connection. A short TTL such as 10 to 30 seconds follows changes quickly without those costs.

7Conclusion

In this answer, we saw that InetAddress caches successful and failed lookups inside the JVM, using its own TTLs rather than the DNS record's. networkaddress.cache.ttl is a security property, set in java.security, an override file or Security.setProperty, with a default of about 30 seconds without a security manager and forever with one, and networkaddress.cache.negative.ttl caches failures for 10 seconds by default. In clouds and containers where IPs change, the TTL must be short enough to follow the changes, and open pooled connections need limited lifetimes as well.

What to say in the interviewAbout 90 seconds aloud

Open with

The JVM has its own DNS cache inside InetAddress. When we resolve a name, the result is stored in memory and reused for all later lookups in that process, and failures are cached too.

Then, if they want more

Two security properties control it. networkaddress.cache.ttl is how many seconds a successful answer is kept; minus one means forever and zero means no caching.

Without a security manager, which is the normal case and the only case since JDK 24, the default is about 30 seconds; with a security manager it used to be forever, so I would check the version and configuration of older services. networkaddress.cache.negative.ttl caches failed lookups, by default for 10 seconds, so a name that has just been created can still fail for a few seconds.

An important point is that the JVM ignores the TTL in the DNS record and uses its own value. Because these are security properties, passing them with -D does not work; I set them in a java.security override file passed with -Djava.security.properties, ideally from the base image, or with Security.setProperty before the first lookup.

In containers and the cloud, load balancer and database addresses change, so I keep the TTL short, at most around 30 to 60 seconds, and also limit connection lifetimes in pools, because the DNS cache does not affect connections that are already open.

About this answer

Last updated
12 November 2026
Versions it names
Java 18, Java 24
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 "How does the JVM cache DNS lookups? Explain networkaddress.cache.ttl and...": https://scrivio-interview-library.netlify.app/java/networking/edc61c4144/