Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Could someone please describe to me the pros and cons of using a Unix socket file vs a tcp/ip localhost:port when setting up services on a server (Ubuntu, FWIW)?

In this particular instance it's for a Python WSGI server (uWSGI) but I'm just interested in general (eg, I know you can set up MySQL in both ways).

I realise that using tcp/ip means that the services can be exposed to other machines, but I'm just interested in whether there are any performance tradeoffs when accessing services locally.

Cheers.

share|improve this question
1  
Both brilliant answers and really useful - thank you! :) –  Ludo Oct 28 '10 at 13:29

2 Answers 2

up vote 18 down vote accepted

Unix sockets are a little bit faster as you don't have the tcp-overhead. If you realize this performance loss is a question of server load. If you don't have very high server load you won't recognize it.

If you use Jails (FreeBSD) or some other virtualisation technology to separate the e.g. MySQL-Server from the Webserver, you often use the tcp/ip setup instead of sockets. The firewall rules need to restrict the access though.

You need to find out if your system is under heavy load so that a socket is a must or you can focus on a nice system design (separating services), then a tcp/ip solution would be better.

So make a long answer short:

Yes, there is a performance difference, sockets are faster. If you are not suffering high server load, just choose what fits better to your system's design.

share|improve this answer
2  
Re: sockets are faster...aren't they both sockets? –  Bart Silverstrim Oct 27 '10 at 13:49
2  
@Bart Silverstrim: no, sockets are sockets; TCP has a socket-like API –  Javier Oct 27 '10 at 14:13
5  
I believe they're called "Unix sockets" and "Internet sockets". (socket(AF_INET, SOCK_STREAM, ...)) –  grawity Oct 27 '10 at 16:44
    
i just tested a few mysql-queries with php-mysql (unix vs tcp-socket, both localhost). for example "select SQL_NO_CACHE 1" to eliminate non-transport factors. there was no difference measurable. BOTH had an average of 0.25 ms, best time for BOTH was 0.19 ms. –  jens Aug 27 '13 at 18:54

It's basically a tradeoff between performance and flexibility. Unix domain sockets will give you a bit better performance, while a socket connected to localhost gives you a bit better portability. You can easily move the server app to another OS by just changing the IP address from localhost to a different hostname.

A Unix domain socket uses the local file system to create an IPC mechanism between the server and client processes. You will see a file in /var somewhere when the Unix domain socket is connected.

If you are looking for purely the ultimate performance solution you may want to explore a shared memory IPC. But, that's a little bit more complex.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.