0

I have noticed that InetAddress is very slow for some hosts.

Below is the relevant code

final long startTime = System.currentTimeMillis();

try {
    System.out.println("start");
    InetAddress address = InetAddress.getByName("smartface.com");
    System.out.println(address);
} catch (UnknownHostException e) {
    e.printStackTrace();
}

final long endTime = System.currentTimeMillis();
System.out.println("Total execution time: " + (endTime - startTime));

For the host "smartface.com" it takes about 20000 miliseconds resulting in a "UnknownHostException"

start
java.net.UnknownHostException: smartface.com: Name or service not known
...
...
Total execution time: 20024
end

But when running the same code for host "smartfacesmart.com" it takes about 280 milliseconds.

start
java.net.UnknownHostException: smartfacesmart.com: Name or service not known
...
...
Total execution time: 284
end

I don't understand why there is such a big difference between the two hosts. Both result in the same exception.

Is there anything I can to make the process faster for the slower hosts?

Arya
  • 8,473
  • 27
  • 105
  • 175
  • that is how DNS caches work: "positive" (existing records) answers get cached, "negative" (non exisiting records) - do not. If record is not found in cache your DNS server is trying to get authoritative answer from the chain of DNS servers serving particular domain/zone, that takes some time. – Andrey B. Panfilov May 15 '23 at 03:33
  • Try using a command line DNS query tool and verify that the delays are coming from the DNS servers rather than your program. – aled May 15 '23 at 11:31
  • @AndreyB.Panfilov DNS servers and local DNS clients usually also cache negative answers, but usually with a shorter TTL than positive answers. – Mark Rotteveel May 15 '23 at 13:38
  • well, in this particular case that would correct to say the delay is caused by unreachability of authoritative name servers – Andrey B. Panfilov May 15 '23 at 14:04
  • @aled When running `dig smartface.com` it prints `communications error to 127.0.0.53#53: timed out`. But for `dig smartfacesmart.com` there is no timout. – Arya May 15 '23 at 16:04
  • @AndreyB.Panfilov Is there anyway to set a lower timeout for this particular case? – Arya May 15 '23 at 16:11

0 Answers0