IPC performance: Named Pipe vs Socket

125,333

Solution 1

Best results you'll get with Shared Memory solution.

Named pipes are only 16% better than TCP sockets.

Results are get with IPC benchmarking:

  • System: Linux (Linux ubuntu 4.4.0 x86_64 i7-6700K 4.00GHz)
  • Message: 128 bytes
  • Messages count: 1000000

Pipe benchmark:

Message size:       128
Message count:      1000000
Total duration:     27367.454 ms
Average duration:   27.319 us
Minimum duration:   5.888 us
Maximum duration:   15763.712 us
Standard deviation: 26.664 us
Message rate:       36539 msg/s

FIFOs (named pipes) benchmark:

Message size:       128
Message count:      1000000
Total duration:     38100.093 ms
Average duration:   38.025 us
Minimum duration:   6.656 us
Maximum duration:   27415.040 us
Standard deviation: 91.614 us
Message rate:       26246 msg/s

Message Queue benchmark:

Message size:       128
Message count:      1000000
Total duration:     14723.159 ms
Average duration:   14.675 us
Minimum duration:   3.840 us
Maximum duration:   17437.184 us
Standard deviation: 53.615 us
Message rate:       67920 msg/s

Shared Memory benchmark:

Message size:       128
Message count:      1000000
Total duration:     261.650 ms
Average duration:   0.238 us
Minimum duration:   0.000 us
Maximum duration:   10092.032 us
Standard deviation: 22.095 us
Message rate:       3821893 msg/s

TCP sockets benchmark:

Message size:       128
Message count:      1000000
Total duration:     44477.257 ms
Average duration:   44.391 us
Minimum duration:   11.520 us
Maximum duration:   15863.296 us
Standard deviation: 44.905 us
Message rate:       22483 msg/s

Unix domain sockets benchmark:

Message size:       128
Message count:      1000000
Total duration:     24579.846 ms
Average duration:   24.531 us
Minimum duration:   2.560 us
Maximum duration:   15932.928 us
Standard deviation: 37.854 us
Message rate:       40683 msg/s

ZeroMQ benchmark:

Message size:       128
Message count:      1000000
Total duration:     64872.327 ms
Average duration:   64.808 us
Minimum duration:   23.552 us
Maximum duration:   16443.392 us
Standard deviation: 133.483 us
Message rate:       15414 msg/s

Solution 2

I would suggest you take the easy path first, carefully isolating the IPC mechanism so that you can change from socket to pipe, but I would definitely go with socket first. You should be sure IPC performance is a problem before preemptively optimizing.

And if you get in trouble because of IPC speed, I think you should consider switching to shared memory rather than going to pipe.

If you want to do some transfer speed testing, you should try socat, which is a very versatile program that allows you to create almost any kind of tunnel.

Solution 3

I'm going to agree with shodanex, it looks like you're prematurely trying to optimize something that isn't yet problematic. Unless you know sockets are going to be a bottleneck, I'd just use them.

A lot of people who swear by named pipes find a little savings (depending on how well everything else is written), but end up with code that spends more time blocking for an IPC reply than it does doing useful work. Sure, non-blocking schemes help this, but those can be tricky. Spending years bringing old code into the modern age, I can say, the speedup is almost nil in the majority of cases I've seen.

If you really think that sockets are going to slow you down, then go out of the gate using shared memory with careful attention to how you use locks. Again, in all actuality, you might find a small speedup, but notice that you're wasting a portion of it waiting on mutual exclusion locks. I'm not going to advocate a trip to futex hell (well, not quite hell anymore in 2015, depending upon your experience).

Pound for pound, sockets are (almost) always the best way to go for user space IPC under a monolithic kernel .. and (usually) the easiest to debug and maintain.

Solution 4

Keep in mind that sockets does not necessarily mean IP (and TCP or UDP). You can also use UNIX sockets (PF_UNIX), which offer a noticeable performance improvement over connecting to 127.0.0.1

Solution 5

As often, numbers says more than feeling, here are some data: Pipe vs Unix Socket Performance (opendmx.net).

This benchmark shows a difference of about 12 to 15% faster speed for pipes.

Share:
125,333
user19745
Author by

user19745

Updated on July 08, 2022

Comments

  • user19745
    user19745 5 months

    Everyone seems to say named pipes are faster than sockets IPC. How much faster are they? I would prefer to use sockets because they can do two-way communication and are very flexible but will choose speed over flexibility if it is by considerable amount.

    • pilcrow
      pilcrow over 13 years
      Your mileage will vary. :) Profile typical use for your intended application, and pick the better of the two. Then profile anonymous pipes, sockets of other domains and families, semaphores and shared memory or message queues (SysV and POSIX), realtime signals with a word of data, or whatever. pipe(2) (er, mkfifo(3)?) may be the winner, but you won't know until you try.
    • Tom Anderson
      Tom Anderson about 12 years
      SysV message queues FTW! I have no idea if they're fast, i just have a soft spot for them.
    • Ian Ni-Lewis
      Ian Ni-Lewis over 6 years
      What is "speed" in this case? Overall data transfer rate? Or latency (how quickly the first byte gets to the receiver)? If you want fast local data transfer, then it's hard to beat shared memory. If latency is an issue, though, then the question gets more interesting...
  • Koshinae over 6 years
    "So test it" <-- words to live by.
  • Pacerier
    Pacerier almost 6 years
    What about Windows?
  • Pacerier
    Pacerier almost 6 years
    Well if you open 2 pipes, then you get bidi behavior too.
  • user3666197
    user3666197 over 5 years
    You might like, guess Amit, Martin SUSTRIK's next artwork -- POSIX compliant nanomsg. Anyway, welcome & enjoy this great place & become it's actively Contributing Member.
  • EntangledLoops
    EntangledLoops over 5 years
    @Pacerier Sadly, you can't create local sockets on Windows in the same way as the abstract namespace on UNIX. I have found PF_UNIX sockets to be substantially faster (>10%) than most other methods described on this page.
  • Gukki5
    Gukki5 over 4 years
    maybe some day in a distant utopian future we'll have a completely new, modular, modern kernel that implicitly offers all the (interprocess and others) abilities we currently walk over broken glass to accomplish... but hey.. one can dream
  • ovunccetin
    ovunccetin about 3 years
    Thanks for the detailed benchmarking. Do you mean "multiprocessing.Queue" with "Message Queue"?
  • chronoxor
    chronoxor about 3 years
    Message Queue is a system XSI message queue (man7.org/linux/man-pages/man0/sys_msg.h.0p.html)
  • eri0o
    eri0o almost 3 years
    devblogs.microsoft.com/commandline/af_unix-comes-to-windows update, Unix sockets are available in Windows 10 now.
  • Axel Rietschin
    Axel Rietschin over 1 year
    "only 16 %" :-) 16% is huge if you have a million servers and you are the one paying the electricity bill. Also, 128 bytes is unrealistically small.
  • Michael Quad
    Michael Quad over 1 year
    how much would it be compared named pipe to simple process start & argument pass?
  • John
    John 10 months
    "You should be sure IPC performance is a problem before preemptively optimizing." Could you please explain that in more detail?
  • elMariachi
    elMariachi 10 months
    If an API is more convenient for you, because it allows you to write clear code or less code, then you should use it first. Once you have a working program, with a realistic data usage, then you can evaluate the performance of your program. By evaluating it, tracing it, you can get information on where the bottleneck is. If your bottleneck is IPC speed, then you can switch to a more complicated but faster API. Given a tradeoff between speed and readability, you should pick readability first, then measure. If IPC speed is still an issue, then you can make an informed choice.
  • elMariachi
    elMariachi 10 months
    @john, also see Tim Post answer