What is node.js?
●   Server-side Javascript
●   Built on Google’s V8
●   Evented, non-blocking I/O.
●   EventMachine or Twisted.
●   CommonJS module system.
●   8000 lines of C/C++, 2000 lines of Javascript, 14
      contributors.
Main Goal



      To provide a purely evented,
      non-blocking infrastructure to
   script highly concurrent programs.
Why Evented?
Apache vs NGINX
 concurrency ×
   reqs/sec




                 http://blog.webfaction.com/a-little-holiday-present
Apache vs NGINX
 concurrency ×
    memory




                 http://blog.webfaction.com/a-little-holiday-present
Apache vs NGINX

The difference?

Apache uses one thread per
Connection.

NGINX doesn’t use threads. It uses
an event loop.

For massive concurrency, cannot
use an OS thread for each
connection.
Javascript designed specifically to be used with an
  event loop:
   ●   Anonymous functions, closures.
   ●   Only one callback at a time.
   ●   I/O through DOM event callbacks.


The culture of Javascript is already geared towards
 evented programming.
Why non-blocking?
Design Goals
Design Goals
●   No function should direct perform I/O.
●   To receive info from disk, network, or
    another process there must be a callback.
Code like this




either blocks the entire process or
 implies multiple execution stacks.
But a line of code like this




allows the program to return to the
event loop immediately.

No machinery required.

This is how I/O should be done.
Design Goals
●   Low-level.
●   Stream everything; never force the buffering of
      data.
●   Have built-in support for the most important
     protocols:
       ●   TCP
       ●   DNS
       ●   HTTP
Design Goals
●   Support many HTTP features.
       ●   Chunked requests and responses.
       ●   Keep-alive.
       ●   Hang requests for comet applications.
Design Goals
●   The API should be both familiar to client-side JS
     programmers and old school UNIX hackers.
●   Be platform independent.
Speed
Benchmarks
●   nginx v0.7.65
●   node v0.1.91
●   tornado v0.2 (python 2.6.4)
●   thin v1.2.7 (ruby 1.9.1-p376)


    In Linux using a Intel Core 2 Due 2.53, 4 GB
      memory
The standard ‘hello world’ concurrency benchmark.


(Approximately 100 byte response for each.)
How do the servers perform when
the concurrency is fixed at 300, but
  serve different response sizes.



This is what the node version looks
         like, approximately
Wow. Node sucks at serving large files.



Well over 3 second responses for 256 kilobyte
files at 300 concurrent connections.
What’s happening:



V8 has a generational garbage collector. Moves
objects around randomly.

Node can’t get a pointer to raw string
data to write to socket.
Using Node’s new Buffer object,
      the results change.
Installation




 https://github.com/joyent/node/wiki/Installation

Node.js Presentation

  • 2.
    What is node.js? ● Server-side Javascript ● Built on Google’s V8 ● Evented, non-blocking I/O. ● EventMachine or Twisted. ● CommonJS module system. ● 8000 lines of C/C++, 2000 lines of Javascript, 14 contributors.
  • 3.
    Main Goal To provide a purely evented, non-blocking infrastructure to script highly concurrent programs.
  • 4.
  • 5.
    Apache vs NGINX concurrency × reqs/sec http://blog.webfaction.com/a-little-holiday-present
  • 6.
    Apache vs NGINX concurrency × memory http://blog.webfaction.com/a-little-holiday-present
  • 7.
    Apache vs NGINX Thedifference? Apache uses one thread per Connection. NGINX doesn’t use threads. It uses an event loop. For massive concurrency, cannot use an OS thread for each connection.
  • 8.
    Javascript designed specificallyto be used with an event loop: ● Anonymous functions, closures. ● Only one callback at a time. ● I/O through DOM event callbacks. The culture of Javascript is already geared towards evented programming.
  • 9.
  • 11.
  • 12.
    Design Goals ● No function should direct perform I/O. ● To receive info from disk, network, or another process there must be a callback.
  • 13.
    Code like this eitherblocks the entire process or implies multiple execution stacks.
  • 14.
    But a lineof code like this allows the program to return to the event loop immediately. No machinery required. This is how I/O should be done.
  • 15.
    Design Goals ● Low-level. ● Stream everything; never force the buffering of data. ● Have built-in support for the most important protocols: ● TCP ● DNS ● HTTP
  • 16.
    Design Goals ● Support many HTTP features. ● Chunked requests and responses. ● Keep-alive. ● Hang requests for comet applications.
  • 17.
    Design Goals ● The API should be both familiar to client-side JS programmers and old school UNIX hackers. ● Be platform independent.
  • 18.
  • 19.
    Benchmarks ● nginx v0.7.65 ● node v0.1.91 ● tornado v0.2 (python 2.6.4) ● thin v1.2.7 (ruby 1.9.1-p376) In Linux using a Intel Core 2 Due 2.53, 4 GB memory
  • 20.
    The standard ‘helloworld’ concurrency benchmark. (Approximately 100 byte response for each.)
  • 22.
    How do theservers perform when the concurrency is fixed at 300, but serve different response sizes. This is what the node version looks like, approximately
  • 25.
    Wow. Node sucksat serving large files. Well over 3 second responses for 256 kilobyte files at 300 concurrent connections.
  • 26.
    What’s happening: V8 hasa generational garbage collector. Moves objects around randomly. Node can’t get a pointer to raw string data to write to socket.
  • 27.
    Using Node’s newBuffer object, the results change.
  • 30.