Tornadoweb and Nginx are popular web servers for now and several benchmarkings show they have a much better performance than Apache under certain conditions. So my real question is:

Is 'epoll' probably the most essential reason why make sure they are so quick? And so what can I study from when I wish to write a great socket server?

If you are searching to create a socket server, a great beginning point is Serta Kegel's C10k article from the couple of in the past:

http://world wide web.kegel.com/c10k.html

I additionally found Beej's Help guide to Network Programming to become pretty handy:

http://beej.us/guide/bgnet/

Finally, should you prefer a great reference, there's UNIX Network Programming by W. Richard Stevens et. al.:

http://world wide web.amazon . com.com/Unix-Network-Programming-Electrical sockets-Networking/dp/0131411551/ref=dp_doctor_title_bk

Anyway, to reply to your question, the primary distinction between Apache and Nginx is the fact that Apache uses one thread per client with obstructing I/O, whereas Nginx is single-threaded with non-obstructing I/O. Apache's worker pool does lessen the overhead of beginning and destorying processes, however it still helps make the CPU switch between several threads when serving multiple clients. Nginx, however, handles all demands in a single thread. When one request needs to create a network request (say, to some after sales), Nginx attaches a callback towards the after sales request after which creates another active client request. Used, what this means is it returns towards the event loop (epoll, kqueue, or choose) and requests file descriptors which have something to report. Observe that the machine get in touch with primary event loop is really a obstructing operation, due to there being absolutely nothing to do until among the file descriptors is prepared for reading through or writing.

So this is the primary reason Nginx and Tornado are efficient at serving many synchronised clients: there's only ever one process (thus saving RAM) and just one thread (thus saving CPU from context switches). For epoll, it is simply a far more efficient version of choose. If you will find N open file descriptors (electrical sockets), it allows you choose the ones ready for reading through in O(1) rather than O(N) time. Actually, Nginx may use choose rather than epoll should you compile it using the --with-choose_module option, and that i wager it it's still more effective than Apache. I am less acquainted with Apache internals, but a fast grep shows it will use choose and epoll -- most likely once the server is hearing multiple ports/connects, or maybe it will synchronised after sales demands for any single client.

Incidentally, I acquired began with this particular stuff attempting to write a fundamental socket server and wanted to determine how Nginx am freaking efficient. After poring with the Nginx source code and reading through individuals guides/books I associated with above, I came across it would be simpler to create Nginx modules rather than my very own server. Thus was created the now-semi-legendary Emiller's Help guide to Nginx Module Development:

http://world wide web.evanmiller.org/nginx-modules-guide.html

(Warning: the Guide was written against Nginx .5-.6 and APIs might have transformed.) If you are doing anything with HTTP, I'd say give Nginx a go since it is exercised all of the hairy particulars of coping with stupid clients. For instance, the little socket server which i authored just for fun labored great with all of clients -- except Safari, and that i never determined why. For other methods, Nginx may be the proper way to go the eventing is fairly well abstracted in the methods, and that's why it may proxy HTTP in addition to IMAP. The Nginx code is made of very well-organized and incredibly well-written, with one exception that bears mentioning. I wouldn't follow its lead if this involves hands-moving a protocol parser rather, make use of a parser generator. I have written some stuff about utilizing a parser generator (Ragel) with Nginx here:

http://world wide web.evanmiller.org/nginx-modules-guide-advanced.html#parsing

All this was most likely more details than you desired, but hopefully you will find a lot of it helpful.