Both answers so far have been great.  If you're programming an
extension for nginx, yes it is async.  It is fairly difficult because
async in C is a lot harder than async in javascript.  But if you're
writing your application logic in some blocking system written in
ruby, php, python or even javascript then of course your app logic
doesn't have to be async.  At the sime time your app logic is blocking
and won't benefit much from the async code in the nginx proxy.

On Wed, Aug 15, 2012 at 8:36 AM, Shripad K <assortmentofso...@gmail.com> wrote:
> 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 K.
>
>
> On Wed, Aug 15, 2012 at 4:39 PM, Krishna Guda <kris...@syntactice.com>
> wrote:
>>
>> think I could clarify a few things here. Some of the
>> observations/assertions made are incorrect. Anyways, my 2 cents (corrections
>> & response).
>>
>> 1. nginx executes code.
>>
>> 2. In nginx you write async code all the time for the network and I/O. You
>> are writing them in C whereas in nodejs u write in js. Its generally
>> difficult to write async code in C. Whereas in js, functions are first-class
>> so aync code appears more elegant and needs lesser boilerplate. Btw, you can
>> only write code for modules or when implementing new network I/O protocols
>> in nginx. Most of the times you are just using the conf file and
>> occasionally you may have used some if-else construct which doesn't mean
>> that u write sync code nginx.
>>
>> 3. There could be some overlap in the objectives of nginx and nodejs but
>> they need not be compared. The good news is you can use nginx to serve
>> static content of nodejs web project. The stacks u mentioned are ok too.
>>
>> // Krishna
>> Sent from my BlackBerry
>> ________________________________
>> From: MHK <mha...@omicronlab.com>
>> Sender: nodejs@googlegroups.com
>> Date: Wed, 15 Aug 2012 03:24:57 -0700 (PDT)
>> To: <nodejs@googlegroups.com>
>> ReplyTo: nodejs@googlegroups.com
>> Subject: [nodejs] Being both event-driven servers, why node.js needs async
>> code where Nginx doesn't?
>>
>> Being both event-driven servers, why node.js needs async code where Nginx
>> doesn't? In another words, if Nginx works as the same event-driven async IO
>> model of node, 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)?
>>
>> 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?
>>
>> PS. This question is cross-posted in stackoverflow.
>>
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to nodejs@googlegroups.com
>> To unsubscribe from this group, send email to
>> nodejs+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to nodejs@googlegroups.com
>> To unsubscribe from this group, send email to
>> nodejs+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to