2

Lets suppose that I need to send data from one services to other on the same host. For this I can use localhost:port or real.domain.name:port (lets think that it cached or can be resolved by local dns). I am sure that better to use localhost:port because of less delays on lo interface. Is it correct ?

1
  • If I personally use ::1 since it is shorter to write.
    – kasperd
    Dec 2, 2015 at 18:12

4 Answers 4

3

Generally, no. In linux this could be correct only if the the specific software detects this condition and switches to unix sockets (like mysql do, afaik).

Otherwise, these packets will go through exactly same path. The lo address (127.0.0.1) and other local addresses are added to 'local' routing table (you can view it with 'ip route view table local'). They are all similar in this respect.

To illustrate my words, let's try simple program (in tcl, that is simplest language to write tcp servers). (To try by yourself, save them into files and run with 'tclsh filename.tcl')

Server:

proc accept {chan addr port} {           ;# Make a proc to accept connections
    #puts "$addr:$port says [gets $chan]" ;# Receive a string
    puts $chan goodbye                   ;# Send a string
    close $chan                          ;# Close the socket (automatically flushes)
}                                        ;#
socket -server accept 12345              ;# Create a server socket
vwait forever

Client:

set start [clock seconds]
puts $start
for {set i 0} {$i < 1000000} {incr i} {
    set chan [socket 192.168.7.121 12345]         ;# Open the connection
    puts $chan hello                         ;# Send a string
    flush $chan                              ;# Flush the output buffer
    #puts "127.0.0.1:12345 says [gets $chan]"  ;# Receive a string
    close $chan                              ;# Close the socket
}
set ends [clock seconds]
puts $ends
puts [expr $ends-$start]

Benchmark: when I run with 127.0.0.1, 1000000 cycles took 35 seconds.

when I run with 192.168.7.121 (address of my laptop wlan interface, I specify it in 'socket address port' clause): took 37 seconds. This difference is really nothing more than a margin.

So, no, you would not gain anything if using tcp to localhost in comparison with using fqdn and other IP address. To really benefit from locality you have to use unix domain sockets (AF_UNIX family), it is really much faster that tcp.

There is a case when you have to use real address, not localhost. That case is when you configure Kerberos. This isn't really about speed, I only stated this to give an example where you can't use localhost.

2

If your question is the performance of 127.0.0.1 versus, say, 192.168.0.5 (or some other local IP address that your server hosts), the performance will be largely identical because the network stack will check its ARP table to figure out where to send the packet, notice that it's a local address and deliver it locally. It will never arrive at the NIC card driver and cause any CPU cycles to be used by the NIC card driver. The only performance impact at all would be any iptables rules that apply only to the other local address and not to 127.0.0.1.

If you really want to use real.domain.name and point it at 127.0.0.1, modify your localhost's /etc/hosts file:

127.0.0.1 localhost localhost.localdomain real.domain.name

Then there's no DNS lookup involved (checking /etc/hosts is very fast). This also allows, if the real.domain.name moves to another host, to remove this from /etc/hosts and use that (now remote) host without changing your application's configuration in any way.

0

If you are connecting to a local service on the same host, always use a localhost address and not the IP Address, hostname, or FQDN. These three can change within your environment and not always guarantee you are able to connect to the service, and also have additional network dependencies such and DNS resolution.

2
  • I understand about benefits. But I am curious what is more productive.
    – ipeacocks
    Dec 2, 2015 at 20:02
  • @ipeacocks - It depends on the measure of more productive then. If you are looking for more stability of the system, localhost is better than using the domain name. If you are looking for more performance, the answer by Nikita is the way to go using unix sockets that don't even touch the TCP stack. Dec 2, 2015 at 20:28
0

Assuming you have a local DNS cache, I would use the domain name, not localhost.

That way if you move your service to another host, it would still work, whereas localhost will always be stucked on your local machine.

Having a DNS ttl long enough for this entry will keep you away from the DNS lag (which may not be that important, depending on your environment).

Using a local IP will be fast, no matter if it is located on lo interface or not.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .