I gotta have my orange juice.

Jesu, Juva

Archive for April 2008

A performance comparison of AF_UNIX with loopback on Linux

with 2 comments

[PostgreSQL]On various Linux hosts I use either MySQL or PostgreSQL for my back-end database.  Typically the database is running on the local host, and this raises the question of how to connect to the database.  For both MySQL and PostgreSQL it is possible to connect using a local AF_UNIX connection, or using an AF_INET connection over the loopback socket (address

I had thought that it might actually be more efficient to connect over loopback; for my day job I work on the TCP/IP stack for the z/OS operating system, and I know firsthand that our TCP/IP implementation is heavily optimized, including specific optimizations for local traffic.

I decided to test this hypothesis by comparing both the throughput and latency of AF_UNIX and AF_INET connections.  I was running on a Celeron Dual-core system running Debian 4.0 with kernel version 2.6.18-4-686.  The results disproved my hypothesis, at least for Linux:

  • AF_UNIX:
    • 100GB transferred in 80.575200 s
    • 100 million 8-byte messages:
      • Average latency 14.463910 μs
      • Standard deviation 0.003376 μs
  • AF_INET, loopback address
    • 100GB transferred in 226.717520 s
    • 100 million 8-byte messages:
      • Average latency 1133.444424 μs
      • Standard deviation 0.067419 μs

So, for both throughput and latency, on the Linux platform AF_UNIX is a superior choice to AF_INET.  For local MySQL and PostgreSQL connections you should use a local socket rather than the loopback socket.

Written by Scott Moonen

April 5, 2008 at 6:03 pm