By Vasudev Ram
A new way of writing socket servers has been introduced with the Linux kernel 3.9.
It involves the ability to bind multiple listening sockets to the same port on the same host, by unrelated processes or threads. The socket option is called SO_REUSEPORT.
Article about SO_REUSEPORT.
The above article includes a demo of how to use the feature in Python, using the example of a simple echo server and netcat.
Another article about SO_REUSEPORT on LWN.net by Michael Kerrisk.
Hacker News thread about SO_REUSEPORT.
About Michael Kerrisk. He has worked at DEC (Digital Equipment Corporation) and Google in the past.
Coincidentally, I happened to have come across Michael Kerrisk's Unix and Linux work some months earlier and had emailed him with a few questions about it, which he was kind enough to answer.
Kerrisk is the author of the book The Linux Programming Interface, which seems like an interesting and useful book.
From the web site for the The Linux Programming Interface book:
[ The Linux Programming Interface (published in October 2010, No Starch Press, ISBN 978-1-59327-220-3) is a detailed guide and reference for Linux and UNIX system programming.
With 1552 pages, 115 diagrams, 88 tables, nearly 200 example programs, and over 200 exercises, TLPI is the most comprehensive description of Linux and UNIX system programming available. The author, Michael Kerrisk, is the maintainer of the Linux man-pages project, which documents the Linux kernel and glibc APIs. He has long been active in the documentation, testing, and design review of Linux kernel-userspace interfaces. ]
And if you want to write command-line programs in Linux using C (an area closely related to the topic of the TLPI book), you may wish to check out my article on the subject, written for IBM developerWorks:
Developing a Linux command-line utility.
I have not yet tried out the SO_REUSEPORT option, because I need to get Linux kernel 3.9 first, but it seems like a useful technique for increasing performance of socket servers. Note that there are various other issues involved, so you may not get increased performance just by using this option in your code. As always with performance tuning, you have to profile your code, identify hotspots, and then only work on improving certain parts of it that seem to be the bottlenecks. And in this case, even before all that, you may need to evaluate whether this socket option is relevant to your application at all. So, caveat lector :-)
- Vasudev Ram - Dancing Bison Enterprises