sockets - unix socket vs tcp socket performance tuning
TCP loopback connection vs Unix Domain Socket performance (3)
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?
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.
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: 253702 msg/s UDS average throughput: 1733874 msg/s PIPE average throughput: 1682796 msg/s
66% latency reduction and almost 7X more throughput explain why most performance-critical software has their own IPC custom protocol.