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

Oddly enough, I did not find this information for the search query. How much does it cost to establish a connection using Unix Domain sockets and TCP sockets?

Currently I have to make a connection pool 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 the connection pool.

+6
source share
2 answers

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.

+6
source

Connecting to a server using TCP sockets can include network traffic, as well as TCP three-way handshake .

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

If you only perform local communication, then local sockets can be faster, because there are less costs from the protocol. If your application needs to connect remotely, you will not be able to use local sockets.


By the way, if you only communicate locally and not through a network, a pair called pipe (or anonymously if you deploy) might be even better.

+3
source

All Articles