Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

After a database failover that changed the endpoint's IP, one Java service kept connecting to the old address for a long time while others recovered quickly. What would you check?

I would check two layers. First the JVM's DNS cache: a very long or infinite networkaddress.cache.ttl, set in java.security, by an old security manager default or by code, keeps resolving the endpoint to the old IP. Second the connection pool: pooled connections stay attached to the old address until they fail or expire, and without a socket timeout a dead connection can hang for many minutes. The fixes are a short DNS TTL, a pool maximum connection lifetime so connections are recreated and the name re-resolved, driver socket timeouts, a check of any address caching in the driver itself, and regular failover tests that measure recovery time per service.

Why this question is asked

Failover is supposed to be the boring part of running a database: the primary fails, a standby takes over, the endpoint name now points to the new instance, and applications reconnect. When one service does not recover while all the others do, it is a strong hint that the problem is in that service's configuration, not in the database. Interviewers like this question because the answer needs a clear model of where an IP address can be remembered between the application and the database. In this answer, we will walk through what happens during a DNS-based failover, then check each place the old address can hide: the JVM DNS cache, the pool, the TCP connections themselves and the driver, and finish with how to test recovery.

1A Quick Recap: What a DNS-Based Failover Changes

Managed databases usually give us a stable name, such as orders-db.cluster.example.internal, and point it at whichever instance is the current primary. When the primary fails, the platform promotes a standby and changes the DNS record to the standby's IP address, typically with a very short record TTL of a few seconds. The old primary's IP either disappears entirely or comes back later as a read-only replica.

Notice what failover does not do: it does not tell any client. Every client discovers the change on its own, and only when it resolves the name again. So a client recovers only if two things happen: it opens new connections, and when it opens them, it asks DNS instead of reusing a remembered address.

Step throughFailover with a stale client
ServiceJVM DNS cacheDNSOld primaryNew primary
queries on pooled connections
failover: name -> new IP
query on old connection: no reply
new connection: resolve name
old IP (entry not expired)
connect to old IP: fails or read-only
after TTL: resolve again
connect to new primary

8 messages in order. Play them, or step through them one at a time.

The old address survives in the cache and in open connections.

2Check 1: The JVM DNS Cache TTL

The first suspect is the JVM's own DNS cache. InetAddress caches successful lookups for networkaddress.cache.ttl seconds, ignoring the record's TTL. The default is about 30 seconds without a security manager, but this service may be different: an old base image may set it to -1 (forever) in java.security, an old JDK running with a security manager defaults to forever, or someone may have called Security.setProperty in code years ago. We check what the running process actually uses, not what we think it uses.

Java
// Log the effective values at startup, or read them from a running JVM with jshell / an admin endpoint
log.info("dns ttl={} negative={} legacy={}",
Security.getProperty("networkaddress.cache.ttl"), // "-1" here would explain everything
Security.getProperty("networkaddress.cache.negative.ttl"),
System.getProperty("sun.net.inetaddr.ttl"));
# Inspect the image too
grep -n "networkaddress" $JAVA_HOME/conf/security/java.security
echo "$JAVA_TOOL_OPTIONS" # look for -Djava.security.properties or -Dsun.net.inetaddr.ttl

If the value is -1 or very large, every new connection resolves to the old IP for as long as the process lives, which matches the symptom exactly. The fix is a short TTL, for example 10 to 30 seconds, set in a java.security override that the base image applies to every service.

3Check 2: The Connection Pool Keeps Old Connections

Even with a perfect DNS TTL, the pool can keep the old address. A pool such as HikariCP opens connections once and reuses them for a long time. Each connection is a TCP connection to a specific IP address, and DNS is only consulted when a new connection is opened. If the old primary is still reachable as a replica, those connections keep working for reads while writes fail with read-only errors. If the old IP simply vanished, the situation is worse: packets go nowhere, no reset comes back, and a query on such a connection waits until TCP gives up, which on Linux can take around 15 minutes with default retransmission settings, unless the driver has a socket timeout.

The fix is to make connections mortal. A maximum connection lifetime makes the pool retire each connection after a fixed time and open a fresh one, which re-resolves the name. A driver socket timeout makes a query on a dead connection fail in seconds instead of minutes, and the pool then evicts that connection.

YAML
spring:
datasource:
url: jdbc:postgresql://orders-db.cluster.example.internal:5432/orders?connectTimeout=5&socketTimeout=30
hikari:
max-lifetime: 600000 # 10 minutes: every connection is recreated and re-resolves the name
keepalive-time: 60000 # test idle connections every minute
connection-timeout: 3000 # wait at most 3 s for a connection from the pool
validation-timeout: 2000

Hikari's default maximum lifetime is 30 minutes, which bounds the problem but is long for a failover. Ten minutes or less, kept a little shorter than any idle timeout in the network path, is a common choice. The PostgreSQL JDBC driver's socketTimeout is in seconds and should be longer than the slowest legitimate query, which is why some teams set it per data source rather than globally. With these settings, connections to a vanished primary fail within the socket timeout, and every remaining old connection is gone within ten minutes even if nothing fails.

4Check 3: Address Caching in the Driver or Client

Some drivers and clients keep addresses themselves, independently of InetAddress. Cluster-aware drivers often discover the topology once and connect to member addresses directly; Netty-based clients may use their own DNS resolver with its own cache; Redis, MongoDB and Kafka clients all maintain their own view of the cluster with their own refresh rules. A JDBC URL listing several hosts with a target server type, as the PostgreSQL driver supports, changes behaviour too. So the third check is the documentation of the exact driver and version, looking for words like topology refresh, DNS cache or address resolution, and the matching settings.

5Why Only This Service? Comparing With the Others

Because the other services recovered, comparing configurations is the fastest way to find the culprit. We put the effective values side by side: JDK version and DNS TTL, pool maximum lifetime, driver version and socket timeout. The odd one out is usually obvious, an older base image with an infinite TTL, a pool configured with no maximum lifetime, or a different driver version.

Comparing the stuck service with healthy ones
SettingHealthy servicesStuck service
JVM DNS cache TTL30 s (default)-1 (forever) in old base image
Pool max lifetime10 min0 (never retire)
Driver socket timeout30 snot set (wait for TCP)
Driver versioncurrentthree years old
Recovery after failoverunder 1 minabout 40 min
Illustrative values: each row is a place an old address can survive.

6Test Failover Regularly and Measure Recovery

The real lesson of this incident is that recovery was never tested. Managed databases let us trigger a failover on demand, so we should do it regularly in staging, and occasionally in production during a quiet period. For each service we measure the recovery time: from the moment the failover completes to the moment its error rate returns to normal. A dashboard of recovery time per service turns a hidden misconfiguration into a visible outlier, and a target, for example every service recovers within two minutes, gives teams something concrete to fix.

7Follow-up questions

Would restarting the service have been the right immediate action?

Yes, as mitigation. A restart clears the DNS cache and the pool, so it recovers immediately. But it must be followed by fixing the configuration, otherwise the next failover needs another manual restart.

Does setting the DNS TTL alone solve it?

No. The TTL only affects new lookups. Without a maximum connection lifetime and a socket timeout, existing connections can still point at the old address for a long time. Both layers need fixing.

What about TCP keepalive?

TCP keepalive can detect a dead peer on an idle connection, but Linux defaults wait two hours before the first probe. Some drivers expose a tcpKeepAlive option and the kernel settings can be shortened, but the pool's lifetime and validation settings and a socket timeout are easier to control from the application.

8Conclusion

In this answer, we followed a DNS-based failover and checked every place the old address could survive. The JVM DNS cache may hold it for a long or infinite networkaddress.cache.ttl; the pool keeps open connections to it until they fail or expire, and without a socket timeout a dead connection can hang for minutes; and some drivers cache addresses themselves. We fixed it with a short TTL, a pool maximum lifetime, driver socket timeouts and driver-specific settings, and made failover tests with recovery time per service a regular practice.

What to say in the interviewAbout 90 seconds aloud

Open with

Since the other services recovered, I would look for what is different about this one, and there are three places the old IP can live. First the JVM's DNS cache.

Then, if they want more

InetAddress caches lookups for networkaddress.cache.ttl seconds and ignores the record TTL. The default is about 30 seconds, but an old base image, an old JDK with a security manager, or code calling Security.setProperty can make it infinite, so new connections keep resolving to the old address.

I would log the effective security property in the running process and check java.security and JAVA_TOOL_OPTIONS. Second the connection pool. Existing connections are bound to the old IP, and if that IP vanished without sending resets, a query can hang until TCP gives up, which can be around 15 minutes on Linux.

So I would set a pool maximum lifetime, say ten minutes in Hikari, so connections are recreated and re-resolve the name, plus a driver socket timeout so dead connections fail fast and get evicted. Third the driver or client itself, since some keep their own topology or DNS cache, so I would read its docs for that version.

Finally I would make failover a regular test in staging and measure recovery time per service, so an outlier like this shows up before a real incident.

About this answer

Last updated
12 November 2026
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 "After a database failover that changed the endpoint's IP, one Java service kept...": https://scrivio-interview-library.netlify.app/java/networking/f158434c71/