If you look at the code, you will see that Unix Domain sockets execute much less code than TCP sockets.
Messages sent over TCP sockets must go all the way through the network stack to the loopback interface (which is a virtual network interface device, usually called βloβ on Unix-style systems), and then back to the receiving socket, the network code of the stack on the TCP headers and IP, makes routing decisions, passes the packet to itself through "lo", then does more routing and removes the headers. In addition, since TCP is a network protocol, its part of establishing a connection has all kinds of additional complexity for handling dropped packets. Most importantly for you, TCP should send three messages only to establish a connection (SYN, SYN-ACK and ACK).
Unix domain sockets simply look at the virtual file system (or "abstract namespace") to find the socket target (in RAM) and send the message to the queue directly. In addition, even if you use the file system to indicate your destination socket, if this socket is connected to it, its file systems will be cached in RAM, so you do not have to go to disk. Establishing a connection for a Unix domain socket involves creating a new instance of the socket object in RAM (i.e., the socket returned by accept (), which also needs to be done for TCP) and storing a pointer in each of the two connected socket objects (therefore, each one of them has a pointer to another socket later when they need to send). This is pretty much the case. No additional packages are required.
By the way, this document suggests that Unix Domain sockets are actually faster than even Pipes for data transfer:
http://osnet.cs.binghamton.edu/publications/TR-20070820.pdf
Unfortunately, they did not take specific measurements of connection costs, but, as I said, I looked at the Linux source code, and it is really much simpler than the TCP connection setup code.
source share