Skip to main content
← PROBLEMS / PRODUCTION-DEBUGGING
An end-to-end domain learning journey

Fixing Stale DNS Caching
in Long-Lived Clients.

Java's default infinite/long DNS cache and unrecycled TLS keep-alive pools pin pods to decommissioned AWS ALB IPs; setting networkaddress.cache.ttl=30, connection max-lifetime=55s with jitter, and NodeLocal DNSCache ensures <=60s IP rotation without TLS storms.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When an external partner's AWS ALB (`api, fix both layers: bound JVM DNS cache TTL (<=30s) AND bound HTTP connection max-lifetime (connTTL ~55s + jitter) so persistent TCP/TLS sockets actually re-resolve rotated ALB IPs.

TL;DR30-second simplified summary
▾

Core Mechanics: When an external partner's AWS ALB (api.partner-pay.com, DNS TTL = 60 s) scales or rotates IPs, old IPs are drained and decommissioned 5 minutes later.

Visual & Structural Flow: Depicts ALB IP Rotation: Pinned TLS Socket (504) vs. Bounded connTTL + DNS TTL (Java Pod (100 conns) -> NodeLocal DNS (60s) -> Old ALB IP -> New ALB IP); NodeLocal DNS -> JVM Cache (30s) -> Jittered connTTL (55-75s) (App Threads -> HttpClient Pool -> JVM DNS Cache -> NodeLocal DNS).

Trade-Offs & Production: Setting Connection: close or networkaddress.cache.ttl=0 trades outages for latency and CoreDNS saturation: TLS 1.3 Handshake Tax: Disabling keep-alive adds ~18 ms RTT to every single partner call. Production Java clients combine three coordinated settings:

Failure Modes & Application: Coordinated DNS + connection-pool TTLs are critical whenever talking to elastic cloud endpoints: AWS ALB / NLB & CloudFront Integrations: Following elastic load balancer node scaling without 504s.

Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why stale DNS breaks long-lived HTTP/2 and TLS clients.

Why this matters: The mechanism: understand how Why stale DNS breaks long-lived HTTP/2 and TLS clients works from first principles.

Think of Why stale DNS breaks long-lived HTTP/2 and TLS clients as the intuition layer for Fixing Stale DNS Caching in Long-Lived Clients: The mechanism: understand how Why stale DNS breaks long-lived HTTP/2 and TLS clients works from first principles.

When an external partner's AWS ALB (api.partner-pay.com, DNS TTL = 60 s) scales or rotates IPs, old IPs are drained and decommissioned 5 minutes later. Long-lived Java pods (200 pods × 100 persistent TLS connections) keep sending 100% of traffic to dead IPs—causing ConnectTimeoutException and 504 Gateway Timeout until pods are manually restarted.

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

What happens when DNS TTL and connection pools are unbounded.

Why this matters: The problem: see what breaks without What happens when DNS TTL and connection pools are unbounded under real concurrency or scale.

Two independent caching layers pin traffic to dead ALB nodes:

  1. JVM InetAddress Cache: With a SecurityManager or older defaults, Java caches DNS forever; even with 30 s default, it only applies to new socket connections.
  2. Unbounded Keep-Alive Socket Lifetime: Existing TLS 1.3 sockets stay open indefinitely, never performing a new DNS lookup even after the JVM DNS cache expires.
Two-Layer IP Pinning

ALB IP Rotation: Pinned TLS Socket (504) vs. Bounded connTTL + DNS TTL

Java Pod (100conns)NodeLocal DNS (60s)Old ALB IPNew ALB IP1. AWS rotates ALB -> old IP drains2. Naive: keep-alive pinned -> 504Timeout3. Fix: connTTL=55s+jitter expires socket4. Fresh lookup (JVM TTL=30s) ->New IP5. Graceful TLS 1.3 reconnect ->200 OK

Bounding both JVM DNS TTL (30s) and pooled connection max-lifetime (55s + jitter) rotates traffic within 60s without TLS storms.

Engineering Decision Framework
Problem
How to implement Fixing Stale DNS Caching in Long-Lived Clients without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
When an external partner's AWS ALB (`api, fix both layers: bound JVM DNS cache TTL (<=30s) AND bound HTTP connection max-lifetime (connTTL ~55s + jitter) so persistent TCP/TLS sockets actually re-resolve rotated ALB IPs.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Fixing Stale DNS Caching in Long-Lived Clients experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Naive solution (and why Connection: close or TTL=0 fails).

Why this matters: The problem: see what breaks without Naive solution under real concurrency or scale.

Setting Connection: close or networkaddress.cache.ttl=0 trades outages for latency and CoreDNS saturation:

  • TLS 1.3 Handshake Tax: Disabling keep-alive adds ~18 ms RTT to every single partner call.
  • CoreDNS Flood: 200 pods resolving uncached DNS on every request blows past the < 2,000 QPS CoreDNS budget.
Request & State Flow

Naive solution (and why Connection: close or TTL=0 fails)

CallerService EntryCore ProcessorState Store1. Submit bounded request2. Apply domain invariant3. Persist or read state4. Return verified response
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise solution: JVM DNS TTL=30s + Jittered connTTL + NodeLocal DNS.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Production Java clients combine three coordinated settings:

  • 1. Bounded JVM DNS Cache: Set Security.setProperty('networkaddress.cache.ttl', '30') and networkaddress.cache.negative.ttl=5 (respecting the 60s ALB TTL).
  • 2. Jittered Connection Max-Lifetime (connTTL): Configure Apache HttpClient 5 (TimeValue.ofSeconds(55 + jitter)) or Netty ConnectionProvider.maxLifeTime(55s) so persistent TLS sockets recycle gracefully across 55–75 s—avoiding synchronized TLS handshake spikes.
  • 3. NodeLocal DNSCache + Multi-IP Failover: Absorb 200-pod DNS refreshes on-node (< 1 ms) and enable Happy Eyeballs / retry-on-connect-timeout across multiple returned A-records.
Coordinated DNS & Pool Recycling

NodeLocal DNS -> JVM Cache (30s) -> Jittered connTTL (55-75s)

App ThreadsReuse TLS 1.3 poolHttpClient PoolmaxLifeTime=55s+jitJVM DNS CacheTTL=30s, neg=5sNodeLocal DNSAbsorbs 200-pod burst

1.JVM DNS TTL=30s: Refreshes A-records within half the 60s ALB DNS TTL.

2.Jittered connTTL: Staggers TLS 1.3 reconnects across 55–75s to prevent handshake storms.

3.NodeLocal Cache: Keeps CoreDNS well under the 2,000 QPS external lookup budget.

4.Connect Retry: Safe idempotent retry on ConnectTimeoutException tries next ALB IP.

Coordinating DNS TTL, jittered connection max-lifetime, and NodeLocal DNS achieves <= 60s failover with minimal TLS overhead.

// 1. Bound JVM DNS cache (startup)
java.security.Security.setProperty("networkaddress.cache.ttl", "30");
java.security.Security.setProperty("networkaddress.cache.negative.ttl", "5");

// 2. Apache HttpClient 5 / Netty: bound connection max-lifetime so sockets re-resolve DNS
var connManager = PoolingHttpClientConnectionManagerBuilder.create()
    .setDefaultConnectionConfig(ConnectionConfig.custom()
        .setConnectTimeout(Timeout.ofMilliseconds(500))
        .setTimeToLive(TimeValue.ofSeconds(55 + ThreadLocalRandom.current().nextInt(20))) // jittered
        .build())
    .setMaxConnPerRoute(100)
    .build();
05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where DNS & Connection Lifecycle Alignment applies.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Coordinated DNS + connection-pool TTLs are critical whenever talking to elastic cloud endpoints:

  • AWS ALB / NLB & CloudFront Integrations: Following elastic load balancer node scaling without 504s.
  • Multi-Region DNS Failover: Ensuring Route53 health-checked failover propagates to long-lived microservices in < 60 s.
  • gRPC & HTTP/2 Clients: Combining MAX_CONNECTION_AGE on servers with client-side DNS re-resolution.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why is setting JVM networkaddress.cache.ttl=30 insufficient by itself to rotate traffic away from a decommissioned AWS ALB IP?

Recommended Learning Flow