3

The question is in the title. In another words, if Nginx works as the same event-driven async IO model of node.js, why doesn't it requires writing async style code? I know, Nginx is NOT actually executing any code, rather proxying them to who can. Then why doesn't node do so? Are we missing anything in the current Ngninx way? Or, gaining anything more from node (apart from the pain of writing async codes)?

Ps. To be more specific, how different is Nginx+php-fpm or Nginx+wsgi+python/ruby from node alone regarding performance or utilizing computing resource that node claims? Couldn't node just use existing FastCGI models, be a sync style JavaScript interpreter and let webserver do its async job?

Mehdi
  • 1,075
  • 1
  • 11
  • 24

2 Answers2

5

Cross-posted from NodeJS google groups:

Okay i'll try my best to answer your question:

Nginx is a web server that only proxies requests. Now if you take the example of Nginx+php+fpm or Nginx+wsgi+ruby you are having an asynchronous, evented web server sitting in front of webserver that is executing synchronously. So Nginx will accept() as many connections as possible and all of them would be queued. The requests from Nginx to your backend synchronous server would be asynchronous. But your backend synchronous server which also does accept() is not queuing any connections. It can serve only one request at a time (considering you are single threaded) and multiple requests at a time (prefork/fork(slow)/multithreaded -> has its own drawbacks like thread creation time(can be avoided with thread-pools but PITA to implement), context switches, thread deadlocks, number of connections accept()ed can never be greater than number of threads etc)

Imagine you have 2 routes to your backend server that Nginx is hitting:

/404, /login.

If the /login route is doing a lot of I/O and if another request is made to /404, the rendering of the /404 page will depend on the completion of /login's request (because the process is blocked). So basically the response to any request will depend on the request that takes the longest time to do I/O. So even though Nginx is async and evented its response time for any request will depend entirely on that one request that takes the longest time to finish (culprit: the synchronous backend server).

Now if you take the example of NodeJS, everything is asynchronous and evented. Be it File/Network I/O etc. So nothing blocks the process. So taking the previous example, even if /login route is doing a lot of I/O its all asynchronous and /404 page is rendered immediately.

My explanation is quite rudimentary. But I think it should give you more clarity.

Shripad Krishna
  • 10,463
  • 4
  • 52
  • 65
  • 1
    Thank you! That explains right where the bottleneck is with php-fpm or others despite Nginx being event-driven. – Mehdi Aug 15 '12 at 17:10
3

nginx is a simple static HTTP and proxy server. Node.js is a full-featured application platform.

Why would you not expect the more specialised application to have abstracted away all the internal workings that you don't need to control directly?

Edit:

Your PS is pretty similar to this question, and is concerned specifically with using Node.JS as an HTTP server. Bear in mind that v0.4.12 had just been released when that question was closed - v0.8.5 is the latest stable release at the moment. The key point anyway is it depends what you're trying to achieve.

This blog post describes a Node.JS-based set-up achieving 250k concurrent connections on a single server. A quick google search shows people attempting similar with nginx+php struggling to reach 100k with far more hardware resources available.

Community
  • 1
  • 1
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
  • I know what they are (I wrote, "Nginx is not actually executing any code, rather proxying them to who can"). But this doesn't answer the question. – Mehdi Aug 15 '12 at 08:55
  • 1
    For the same reason a text editor doesn't expose the same interface as a C compiler - they're designed for (and capable of) different things. – OrangeDog Aug 15 '12 at 10:27