News for hackers. Stuff that matters.

Ask HN: Make makes nginx so fast ?

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 ?
( 11 comments | 10 points | on HN )
Re:Ask HN: Make makes nginx so fast ? by RoboTeddy on Wednesday February 24, 2010 @04:43AM (#1147648)
Re:Ask HN: Make makes nginx so fast ? by chmike on Wednesday February 24, 2010 @05:08AM (#1147672)
Excellent reference. State Threads looks very attractive.
Re:Ask HN: Make makes nginx so fast ? by davidw on Wednesday February 24, 2010 @04:28AM (#1147625)
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.

Re:Ask HN: Make makes nginx so fast ? by al_james on Wednesday February 24, 2010 @05:04AM (#1147668)
I would also recommend looking at node.js (http://nodejs.org/). Its an event based javascript framework that lends itself to the creation of scalable servers very easily. Its still early days, so might not be mature enough for your project, but worth a look.
Re:Ask HN: Make makes nginx so fast ? by chmike on Wednesday February 24, 2010 @05:18AM (#1147678)
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.
Re:Ask HN: Make makes nginx so fast ? by davidw on Wednesday February 24, 2010 @05:23AM (#1147683)
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.

Re:Ask HN: Make makes nginx so fast ? by chmike on Wednesday February 24, 2010 @05:41AM (#1147699)
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.
Re:Ask HN: Make makes nginx so fast ? by davidw on Wednesday February 24, 2010 @05:47AM (#1147706)
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.
Re:Ask HN: Make makes nginx so fast ? by ionrock on Wednesday February 24, 2010 @04:58PM (#1149301)
One of big benefits of the async model that ngninx uses i that it can keep many open connections. If you don't have many long running open connections (I/O on the socket), then going that route doesn't really help as much.
Re:Ask HN: Make makes nginx so fast ? by zepolen on Wednesday February 24, 2010 @06:56AM (#1147777)
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.
Re:Ask HN: Make makes nginx so fast ? by lukatmyshu on Wednesday February 24, 2010 @07:54AM (#1147846)
(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.