170

Working on an Android and iOS based application which require communication with a server running in the same device. Currently using TCP loopback connection for communicating with App and Server (App written in user layer, server written in C++ using Android NDK)

I was wondering if replacing inter communication with Unix Domain socket would improve the performance?

Or in-general is there any evidence/theory that proves that Unix Domain socket would give better performance then TCP loopback connection?

7
  • 3
    Remember that local sockets (UNIX domain sockets) need a file in the filesystem. Using the TCP loopback address keeps it all in memory. And if you have to use remote TCP sockets, it might be easier to integrate another TCP socket instead of fiddling with a new socket and address family. Commented Feb 20, 2013 at 6:56
  • 3
    @JoachimPileborg When developing only for Linux (Android) there is the option to use abstract UNIX domain socket addreses, which do not need a file in the filesystem.
    – thuovila
    Commented Feb 20, 2013 at 7:50
  • 13
    @Someprogrammerdude They need a file in the filesystem, but that doesn't mean everything goes to disk and back.
    – user207421
    Commented Jan 17, 2018 at 4:21
  • 12
    @Someprogrammerdude Only the filename, ownership, and permissions info ever gets stored in the filesystem. All the actual data transfer happens entirely in memory.
    – Jesin
    Commented Feb 26, 2018 at 14:38
  • 1
    @kensai Not only you can, but you normally will. Socket files are usually placed in /run (or /var/run), which is a tmpfs. Commented Aug 24, 2022 at 10:56

5 Answers 5

140

Yes, local interprocess communication by unix domain sockets should be faster than communication by loopback localhost connections because you have less TCP overhead, see here.

10
  • 12
    the first link is citing the second link, which is from 2005 (old). and it only covers FreeBSD Commented Jan 31, 2014 at 16:34
  • 9
    This answer is wrong, when tested loopback tcp on modern linux is as fast and sometimes faster than UDS. can provide benchmark if required
    – easytiger
    Commented Jun 18, 2014 at 13:11
  • 17
    This answer is absolutely correct. Loopback interface is still TCP, meaning that you still have the overhead of TCP (congestion control, flow control, stream management (IP packet ordering, retransmission, etc) ). Unix domain sockets do not do any of the above because it was designed from the ground up to be ran locally, meaning no congestion issues, no speed differences between server/client requiring flow control, no dropped packets, etc. Google this if in doubt, not a new thing.
    – JSON
    Commented Oct 17, 2014 at 20:45
  • 4
    What about local UDP? Commented Nov 18, 2014 at 11:10
  • 8
    given that the first link is dead (HTTP 404)... this is why stackoverflow best-practice is to at least provide a short/concise relevant quote from the source URL at the time of the answer writing (then when the link goes down the short summary is still available). Commented Jan 25, 2019 at 16:27
134

This benchmark: https://github.com/rigtorp/ipc-bench provides latency and throughput tests for TCP sockets, Unix Domain Sockets (UDS), and PIPEs.

Here you have the results on a single CPU 3.3GHz Linux machine :

TCP average latency: 6 us

UDS average latency: 2 us

PIPE average latency: 2 us

TCP average throughput: 0.253702 million msg/s

UDS average throughput: 1.733874 million msg/s

PIPE average throughput: 1.682796 million msg/s

66% latency reduction and almost 7X more throughput explain why most performance-critical software has their own IPC custom protocol.

5
  • 7
    Sounds to me like their product is an answer to the problem! Maybe that's why they're replying to those questions; because they know an answer. Commented Feb 13, 2016 at 3:05
  • This is a great answer because it has some numbers. Throughput from TCP to UNIX is 350% better, UNIX to PIPE 40% on an i5. Commented Sep 19, 2016 at 13:42
  • 13
    @GreenReaper The answer is indeed relevant, but the line our Torusware Speedus product ... come with 2 versions, Speedus Lite and Speedus Extreme Performance (EP) is not, and it makes the whole thing sound like a cheap ad. Commented Dec 5, 2016 at 17:27
  • 3
    Spam. And no, his product isn't relevant in a comparison between TCP and Unix sockets. There's plenty of common sense alternatives to sockets- each outside what the OP is asking
    – JSON
    Commented Mar 6, 2017 at 19:51
  • 1
    The usage of that tool is not sufficiently explained. Is there somehow a page explaining how to call client and server?
    – falkb
    Commented Feb 11, 2019 at 15:18
59

Redis benchmark shows unix domain socket can be significant faster than TCP loopback.

When the server and client benchmark programs run on the same box, both the TCP/IP loopback and unix domain sockets can be used. Depending on the platform, unix domain sockets can achieve around 50% more throughput than the TCP/IP loopback (on Linux for instance). The default behavior of redis-benchmark is to use the TCP/IP loopback.

However, this difference only matters when throughput is high.

Throughput per data size

14

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. The Unix domain protocols are an alternative to the interprocess communication (IPC) methods.

5

Unix domain sockets are indeed faster than TCP as most of the other answers suggested here. I would also add though that it is always a good idea to benchmark and get some real sense of the performance numbers as there might be discrepancies from platform to platform. Here is a good collection of benchmarks that cover various ways to do IPC: https://github.com/goldsborough/ipc-bench.

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