

Ask HN: Make makes nginx so fast ? - chmike

I'm currently developping a network service and would like to know what makes nginx so fast ?
Is there a document (whitepaper) describing its working principle ?
======
RoboTeddy
<http://www.kegel.com/c10k.html>

~~~
chmike
Excellent reference. State Threads looks very attractive.

------
davidw
It's an "event based" server written in C, meaning that it uses select or poll
or some other 'event based' mechanism to do its work.

Erlang might be an interesting choice for your network service, as it allows
you to write this sort of code very naturally. It depends on what you're
doing, though... Python has some frameworks that work this way, although none
so smoothly as what Erlang gives you. Tcl has also had this capability in its
core for years, although, like Python, you have to be careful about writing
'blocking' code, something that you don't have to worry about so much in
Erlang.

~~~
chmike
The network service I'm developing is CORBA/ICE/RMI/... like. The difficulty
is the request processing, not the I/O. I assume nginx may benefit from the
fact that the different requests it has to process are predefined and limited
in number.

~~~
davidw
Yeah, being fast is really easy if all you do is spit out files. When you
actually have to do processing, things get a bit more difficult, and a system
like Erlang becomes more compelling.

It all depends on what sort of compromises you want to make in terms of speed,
ease of programming, and the expected amount of traffic, though.

~~~
chmike
I'm not sure about Erlang. I don't know this language and the protocol I
designed uses binary encoding. Support of JS and python is planned, especially
on the client side, but come with a performance overhead which is not very
attractive when it comes to service performance.

~~~
davidw
Using a language you know is probably a good idea: better to get something up
and running and worry about making it blazingly fast when you know you need
to. Erlang handles binary data quite well, but if you don't know it, you don't
know it.

------
zepolen
Yes, being event based helps a lot, but the reason nginx is faster than other
event based webservers is that it tries very very hard to add as little
overhead as possible; For example not copying memory around unless absolutely
neccessary.

~~~
lukatmyshu
(expanding on this) nginx uses a buffer-chain architecture internally. This
means that if your data comes in chunks when you read it in (remember, nginx
is event based) then it's stored internally in fragments that correspond to
those reads. Almost all the operations on it work on those chains so often
times the data never has to be copied until it's written (I believe using
iovectors) back out. Memory is managed in request pools which minimize memory
fragmentation, and are just faster/safer than calling malloc/free directly.

