• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

URL: Use domain name or IP?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple URL connection is:



Is it faster to use an IP when it will always be the same, like 'http://1.2.3.4' rather than 'http://example.com' when making the call?

Can the DNS lookup (and possibly slow down depending on DNS server) be avoided by initially providing the actual IP address?

 
Ranch Hand
Posts: 116
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran this performance test on my website:



Result:

Time 1: 17963
Time 2: 18079

No meaningful difference between the times for opening a connection, so I would go with the more readable one.
 
Rancher
Posts: 144
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It only would make any difference if you take out any DNS lookup at all by using InetAddress.getByAddress(byte[]) - any other way of creating a socket will perform at least some sort of DNS look up.
Also be aware of TLS - when you want to use something like SNI you have to do it on your own - wich quickly end up in a 20-line mess. Just using URL does it all for you.
TLDR: Unless you do it all low-level by yourself - nope - it doesn't make any significant difference.
 
Marshal
Posts: 67939
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kristina Hansen wrote:. . . low-level by yourself . . .

It is usually a bad idea to do anything at a low level. Except that beginners are often made to do things at a low level so they can learn the algorithm involved however.

Moving to a different forum as not a “beginning” topic.
 
Saloon Keeper
Posts: 21710
148
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DNS services are usually cached. After all, when you make a "web page request", what you are often doing is making a cascading series of requests, since a separate request is required for the primary page, requests for each image on the page, requests for JavaScript, CSS stylesheets, and so forth. So many requests are made, in fact, that most browsers actually run multiple requests in parallel to speed up the fetch process.

So using a direct IP address will not give all that much of a performance gain. A cached DNS lookup may take only a few microseconds, and as I pointed out to a colleague years ago, you cannot save microseconds in a jar for later use - the use is going to spend countless microseconds drooling at the screen trying to make sense of what just came back.

A more important reason not to use direct IP addresses is that many sites these days host multiple web applications at the same IP address. That includes my own local server farm. So in order to get to the proper web application, you'll probably have to supply a FQDN in the URL request anyway.
 
machines help you to do more, but experience less. Experience this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!