Opscraft
DNS TTL & caching · Lesson 7 of 8 · 3 min read

Application-level DNS caching

The JVM

Java caches successful lookups according to networkaddress.cache.ttl (in java.security or set programmatically):

  • Default: 30 seconds in modern JDKs when no security manager is installed.
  • -1 = cache forever (until the JVM restarts) — historically the default when a security manager was installed, and sometimes set explicitly by old app servers.
  • Negative lookups: networkaddress.cache.negative.ttl (default 10 s).

AWS recommends a TTL of 60 seconds or less for Java apps talking to AWS endpoints.

java.security.Security.setProperty("networkaddress.cache.ttl", "60");
# or in $JAVA_HOME/conf/security/java.security
networkaddress.cache.ttl=60

Classic incident: "A Java service fails after the ALB scaled" — it pinned old ELB IPs forever. Fix the cache setting and restart.

Enlarge diagramJVM resolvesthe ALBgets 3.6.10.20cache.ttl = -1keeps it foreverALB scalesits IPs changeCalls failold IP is not theALB any moreFixcache TTL 60 s,restart, recyclepooledconnections12345
Figure 1. A classic incident: a Java service caches DNS forever, the ALB replaces its nodes, and the service keeps calling IPs that are no longer the ALB's.
Swipe sideways to see all of it, or tap to open it full screen.

Other runtimes

  • Node.js — dns.lookup (used by http) calls the OS resolver every time; no built-in cache (use cacheable-lookup or an agent if you need one). Without an OS cache in a container, every request triggers a DNS query — significant load on CoreDNS.
  • Go — the pure-Go resolver doesn't cache; relies on the OS/cluster DNS.
  • Python — no cache by default (requests each getaddrinfo).
  • .NET — ServicePointManager.DnsRefreshTimeout (older), SocketsHttpHandler.PooledConnectionLifetime in modern .NET.

Connection pools pin IPs regardless of DNS

Even with perfect DNS caching, a long-lived connection keeps talking to the IP it connected to. HTTP keep-alive pools, database pools and gRPC channels may never re-resolve. Set a maximum connection lifetime (e.g. 5–10 minutes) so connections are periodically recreated and pick up new DNS answers — important for RDS failover (the endpoint's CNAME flips to the new primary) and for load balancers whose IPs change.

Mobile and IoT clients

Apps that cache resolved IPs for hours — or hardcode them — ignore TTL entirely. During migrations keep the old endpoint alive (or redirect/proxy from it) until old app versions age out, and design clients to re-resolve on failure.