[plug] How does the "127.0.0.1" address work?

Adrian Chadd adrian at creative.net.au
Sun Oct 28 19:07:15 WST 2007


On Sun, Oct 28, 2007, Fred Janon wrote:
> I am working on an application that needs to be accessed through the
> network and also accessed locally. since I don't want to implement 2
> different modes of access and use the HTTP port for both (using
> 127.0.0.1 for local access) I'd like to know what is the performance
> difference in accessing the application through the 127.0.0.1 network
> address compared to accessing from another computer on the network,
> assuming that the network performance is pretty good (100Mb/sec or
> more).
> 
> I wonder how does the network access through the 127.0.0.1 address
> work. Is is a special path inside the network driver? Is it a loopback
> at some level inside the network stack?

A-yup. Its generally implemented as a software network device. :)

> Does anyone a resource on the Web that describes how the "127.0.0.1"
> address is supposed to be implemented?

Its meant to be implemented as any other network destination.
Your app shouldn't have to know that localhost and non-localhost IPs
are any different.

About the only difference you'll notice is the default packet size (MTU)
on loopback interfaces is quite a bit bigger than default:

violet:~ adrian$ ifconfig lo0
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
        inet 127.0.0.1 netmask 0xff000000 


So if you're doing a lot of loopback network IO you can get better
performance by using larger packet sizes.

127.0.0.1 was implemented so certain "local" things could be implemented
exactly the same was as network services - so you wouldn't have to
implement "network" versus "local" versions.

Now, just to be fair - some applications - eg MySQL - do use local UNIX
sockets instead of 127.0.0.1 connections because there's much less data
handling involved. With a local UNIX socket the data is just copied or
referenced; with 127.0.0.1 it may be wrapped up in an IP datagram and
possibly stuffed inside a TCP session. But unless you're shifting around
very large amounts of data very quickly I doubt it'd matter to you.

HTH,



Adrian




More information about the plug mailing list