TCP loopback connection vs Unix Domain Socket performance

SocketsUnixTcpLoopbackUnix Socket

Sockets Problem Overview


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?

Sockets Solutions


Solution 1 - Sockets

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.

Solution 2 - Sockets

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.

Solution 3 - Sockets

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

Solution 4 - Sockets

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.

Solution 5 - Sockets

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRohitView Question on Stackoverflow
Solution 1 - Sockets0x4a6f4672View Answer on Stackoverflow
Solution 2 - SocketsGuillermo LopezView Answer on Stackoverflow
Solution 3 - SocketswoodingsView Answer on Stackoverflow
Solution 4 - SocketspeterDriscollView Answer on Stackoverflow
Solution 5 - SocketsAnastasios AndronidisView Answer on Stackoverflow