135
votes

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?

4
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.Some programmer dude
@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
refer stackoverflow.com/questions/14643571/… for android connection.Rohit
@Someprogrammerdude They need a file in the filesystem, but that doesn't mean everything goes to disk and back.user207421
@Someprogrammerdude Only the filename, ownership, and permissions info ever gets stored in the filesystem. All the actual data transfer happens entirely in memory.Jesin

4 Answers

114
votes

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.

96
votes

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.

47
votes

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

10
votes

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.