Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the cost of establishing connection using Unix Domain sockets versus TCP sockets?

Oddly I didn't find this info by googling. What is the cost of establishing connection using Unix Domain sockets versus TCP sockets?

Right now I have to do connection pooling with TCP sockets because reconnecting is quite expensive. I wonder if I can simplify my client by simply switching to Unix Domain sockets and getting rid of connection pooling.

like image 495
expert Avatar asked Nov 26 '12 03:11

expert


People also ask

What's the difference between Unix socket and TCP IP socket?

A UNIX socket is an inter-process communication mechanism that allows bidirectional data exchange between processes running on the same machine. IP sockets (especially TCP/IP sockets) are a mechanism allowing communication between processes over the network.

In which situation is it preferred to use UNIX domain sockets over TCP IP sockets?

UNIX domain sockets know that they're executing on the same system, so they can avoid some checks and operations (like routing); which makes them faster and lighter than IP sockets. So if you plan to communicate with processes on the same host, this is a better option than IP sockets.

Are UNIX sockets faster than TCP?

Unix domain sockets are often twice as fast as a TCP socket when both peers are on the same host. The Unix domain protocols are not an actual protocol suite, but a way of performing client/server communication on a single host using the same API that is used for clients and servers on different hosts.

Are UNIX domain sockets reliable?

Valid socket types in the UNIX domain are: SOCK_STREAM, for a stream-oriented socket; SOCK_DGRAM, for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don't reorder datagrams); and (since Linux 2.6.


2 Answers

If you look into the code, you'll see that Unix Domain sockets execute far less code than TCP sockets.

Messages sent through TCP sockets have to go all the way through the networking stack to the loopback interface (which is a virtual network interface device typically called "lo" on Unix-style systems), and then back up to the receiving socket. The networking stack code tacks on TCP and IP headers, makes routing decisions, forwards a packet to itself through "lo", then does more routing and strips the headers back off. Furthermore, because TCP is a networking protocol, the connection establishment part of it has all kinds of added complexity to deal with dropped packets. Most significantly for you, TCP has to send three messages just to establish the connection (SYN, SYN-ACK, and ACK).

Unix Domain sockets simply look at the virtual file system (or the "abstract namespace") to find the destination socket object (in RAM) and queue the message directly. Furthermore, even if you are using the file system to name your destination socket, if that socket has been accessed recently, its file system structures will be cached in RAM, so you won't have to go to to disk. Establishing a connection, for a Unix Domain socket involves creating a new socket object instance in RAM (i.e., the socket that gets returned by accept(), which is something that has to be done for TCP too) and storing a pointer in each of the two connected socket objects (so they each have a pointer to the other socket later when they need to send). That's pretty much it. No extra packets are needed.

By the way, this paper suggests that Unix Domain sockets are actually faster than even Pipes for data transfers:

http://osnet.cs.binghamton.edu/publications/TR-20070820.pdf

Unfortunately, they didn't do specific measurements of connection establishment costs, but as I have said, I've looked at the Linux source code and it's really quite a lot simpler than the TCP connection establishment code.

like image 115
jtchitty Avatar answered Dec 09 '22 19:12

jtchitty


Connecting to a server using TCP sockets may involve network traffic, as well as the TCP three-way handshake.

Local sockets (formerly known as Unix domain sockets) are all local, but need to access a physical file on disk.

If you only do local communication then local sockets might be faster as there is less overhead from the protocol. If your application needs to connect remotely then you can't use local sockets.


By the way, if you're only communicating locally, and not over a network, a pair named pipes (or anonymous if you're forking) might be even better.

like image 38
Some programmer dude Avatar answered Dec 09 '22 20:12

Some programmer dude