Video in progress

Strong Junior 🏗️ System design

A server's IP address changed, but some users are still going to the old server. How does the DNS TTL relate to this?

A DNS answer is cached by resolvers, the OS and the browser for the length of the TTL. With a TTL of 1 day, some users keep going to the old IP for up to a day after the move, so you need to lower the TTL ahead of time, before the migration.

📖 Answer

A DNS query doesn't go to your nameserver every time. The ISP's resolver, the operating system, the browser, even Java itself all cache the answer. The TTL on the A record decides for how long.

A common mistake: changing the A record on the day of the move to the new server, while the TTL is 86400 (1 day). The result is that for a whole day traffic is split between two places, and if you shut down the old server, the site is "gone" for some users.

The right order:

  1. A day or two before the move, lower the TTL to 300 seconds.
  2. After the old TTL has expired, change the A record to the new IP.
  3. Keep the old server running for a while and wait until traffic in its access log drops to zero.
  4. When everything is fine, raise the TTL again.

Pay attention on the Java side too: the JVM caches DNS results according to networkaddress.cache.ttl. Without a security manager the default is about 30 seconds, but in old configurations it is sometimes set to "forever" — then the service keeps connecting to the old IP until it is restarted.

// at application startup, if needed
java.security.Security.setProperty("networkaddress.cache.ttl", "60");

You can also do simple load balancing through DNS (several IPs under one name), but it won't remove an unhealthy server by itself — it's no substitute for an LB with health checks.