define( 'WPCACHEHOME', '/srv/www/www.ichilton.co.uk/html/blog/wp-content/plugins/wp-super-cache/' ); //Added by WP-Cache Manager Node.js | Ian Chilton

Archive

Posts Tagged ‘Node.js’

Threading / Blocking vs Event Driven Servers (and Node.js)

April 9th, 2011 No comments

I was just reading an old (Nov 2009) article by Simon Willison (of Django, The Guardian and Lanyrd Fame) discussing the emergence of Node.js.

Two of the things in particular I found interesting about the article:

Firstly, he cleverly predicted the importance and future popularity of Node.JS – and boy was he right. A year and a bit later and Node.JS is everywhere. I don’t think a day ever goes by when I don’t see at least one mention and/or article about it.

Secondly, he has a brilliant (and simple) description of threading / blocking servers vs event driven servers (just like Apache vs Nginx).

Event driven servers are a powerful alternative to the threading / blocking mechanism used by most popular server-side programming frameworks. Typical frameworks can only handle a small number of requests simultaneously, dictated by the number of server threads or processes available. Long-running operations can tie up one of those threads—enough long running operations at once and the server runs out of available threads and becomes unresponsive. For large amounts of traffic, each request must be handled as quickly as possible to free the thread up to deal with the next in line.

This makes certain functionality extremely difficult to support. Examples include handling large file uploads, combining resources from multiple backend web APIs (which themselves can take an unpredictable amount of time to respond) or providing comet functionality by holding open the connection until a new event becomes available.

Event driven programming takes advantage of the fact that network servers spend most of their time waiting for I/O operations to complete. Operations against in-memory data are incredibly fast, but anything that involves talking to the filesystem or over a network inevitably involves waiting around for a response.

With Twisted, EventMachine and Node, the solution lies in specifying I/O operations in conjunction with callbacks. A single event loop rapidly switches between a list of tasks, firing off I/O operations and then moving on to service the next request. When the I/O returns, execution of that particular request is picked up again.

You can read the full article here: Node.js is genuinely exciting.

If you are not familiar with Node.js (have you been living under a rock for the past year?? :)), there is a great video by the author, Ryan Dahl here:

Categories: Interesting Tags: