Node.js For Dummies

15 commentsWritten on December 18th, 2011 by
Categories: JavaScript, node.js

I'm sure you've all heard of Node.js by now. Its popularity is increasing rapidly, which means it's a good idea to be aware of what Node.js is and especially how it differs from more traditional technology stacks. In this post, I'll try to give an easy-to-understand overview of what makes Node.js different and make it clear that it's more than just server-side JavaScript. Note that this overview is highly simplified and only meant to help people understand how Node.js works. This is definitely not a completely accurate description of the lower-level details of Node.js.

Evented/Asynchronous I/O

In most technology stacks, API calls for I/O operations are synchronous. As in, the thread that executes the operation is blocked for the duration of the I/O operation until that operation has completed. Once completed, execution of your code proceeds. Of course, a lot of technology stacks have asynchronous variants of those operations available as well, but generally speaking, they aren't used as often as the synchronous variants. In Node.js, it's the other way around. All I/O operations are asynchronous and there are only a few synchronous implementations available (and you're generally discouraged from using them).

This means that whenever you do an I/O operation (file manipulation, network requests, database operations, etc…), Node.js initiates the I/O operation through a lower-level C/C++ layer which will perform the operation asynchronously. Once the operation has completed, Node.js will execute your callback function that you passed as a parameter to the I/O operation's function call. The important thing here is that while the I/O operation is being executed, Node.js doesn't have to wait for the operation to complete, and is able to focus entirely on processing other events. And those events can be anything: incoming network requests, executing callbacks from other operations that have completed, or invoking whatever function that is assigned to a particular event.

Eventloop

The Node.js eventloop is what makes Node.js so interesting and powerful. Node.js basically just keeps reading from an event queue until that queue is empty. As it loops through the events to be processed, it invokes the JavaScript functions that have been assigned for those events. If any of those functions performs an I/O operation, Node.js will initiate the operation and then immediately move to the next event in the event queue. Once the I/O operation has completed, an event will be added to the event queue with a reference to your original callback. Once all preceding events have been processed, Node.js will get to the newly added event and invoke your callback. Because all I/O operations are asynchronous, this enables Node.js to maximize its efficiency as it processes events because it doesn't need to wait for slow I/O operations to complete.

Single-threaded

One thing that people don't always realize is that the Node.js eventloop is single-threaded. This has some nice benefits but there's a huge drawback as well. The biggest benefit is that you don't need to worry about concurrent access to shared state. After all, there is never more than 1 thread executing your JavaScript functions. This means you don't have to write any locking code to protect shared state. The drawback to the single-threaded eventloop is that you need to be careful not to block the event loop. If you're planning on doing heavy synchronous processing in your JavaScript code, you need to realize that no other events can be processed by Node.js until that synchronous block of code has completed. Obviously, since there's only one thread going through the eventloop, any delay you cause in your code can be very costly to overall throughput and performance. For now, it's best to execute synchronous processing routines as a child process, possibly even in a language that is more suitable for this than JavaScript. But it seems that future Node.js versions will provide a more integrated way to deal with this.

Why JavaScript?

JavaScript's support of closures and it treating functions as first class objects means it's ideally suited for the evented programming model that Node.js offers. Many people still think of JavaScript as a joke or a toy language, but it's a lot more powerful than many people think it is. Yes, it certainly has problems as well, but it's definitely worth learning. I do hope that this post has made it clear that there's a lot more to Node.js than simply being server-side JavaScript. What makes Node.js so interesting are the principles that I've tried to explain in this post. Those principles can be implemented with other languages as well, and could be made to work just as great as, or perhaps even greater than Node.js itself. But you'd be hard-pressed to find a language that's so ubiquitous, yet completely devoid of a pre-existing synchronous I/O infrastructure.

Anyways, I hope I succeeded at making it somewhat clear how Node.js works and why it's so different from most other technology stacks.

  • Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1005

  • Will Rogers

    Thanks. Good 1000-foot overview.

  • http://sanspace.in/ San

  • http://sanspace.in/ San

    JS is a great thing and it’s sad some part of the crowd doesn’t realize it yet. Yes, it got problems. But, you can work around a bit. Crackford’s JS good parts would be a good start.

  • Sms22

    just boys started to hear about node.js 

  • http://www.geekswithblogs.net/jboyer Justin

    Awesome overview. I’ve been curious about Node.js since it seems to be growing in popularity. Are there any good books that discuss how to use it or do you have to rely on online blogs and documentation?

    • http://pulse.yahoo.com/_XHEJMHH2ULDS22G37EYOLMKHYQ Florin Jurcovici

      Idunno about you, but IMO it’s been quite a while since the really useful and fresh information is available first and mostly on blogs and forums, rather than in books.

    • http://davybrion.com Davy Brion

      there aren’t a lot of books available yet, but Hands-On Node.js is pretty good, though pretty short: http://www.amazon.com/Hands-on-Node-js-ebook/dp/B004ZURMXY/ref=sr_1_3?ie=UTF8&qid=1324465490&sr=8-3

  • Kay

    It doesn’t block the current thread of execution. It either blocks the I/O spooler if it spools, or it log jams the CPU with new threads (which is worse, btw). “Non-blocking” is a myth until we create machines that can manifest more cores out of nothing.

  • http://twitter.com/tommie_open tommie

    Hi, I’m making an avatar chat Hanpen3 with Node.js.
    Please check it:  http://tommie.dip.jp
    Thanx.

  • Pingback: Node.js For Dummies « Visar's Blog

  • Joao Rodrigues

    The title of this post is almost there but not quite. It should be:
    Node.js IS ONLY for dummies!

    • http://davybrion.com Davy Brion

      What, not even a single logical argument to make your case?
      You can’t really expect people to take you seriously with such a lame comment…

  • Pingback: NodeJS « Jim's Journal

  • Swaroop

    Came here looking for exactly this. A five minute summary of node.js
    Good work. This makes me want a 5 minute technical summary of all programming concepts. (better than wikipedia but eadier than a textbook)