I gotta have my orange juice.

Jesu, Juva

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 127.0.0.1).

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

2 Responses

Subscribe to comments with RSS.

  1. Good article. Sorry about commenting an article that was posted some time ago! But I found this when I was looking for something else.

    I do have one question, with AF_INET it is possible to have concurrent servers because a “connection” is defined as a 4 tuple (src_addr, src_port, dest_addr, dest_port), so it is possible to have multiple connections to localhost. Is a similar thing possible with AF_UNIX? Thank you!

    Raghu Reddy

    September 24, 2011 at 9:47 am

  2. Hi Raghu, yes, it is typical to allow multiple connections using AF_UNIX sockets.

    Scott Moonen

    September 24, 2011 at 9:53 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: