Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Node.js Explained

Node.js Explained

Rising from non-existence a few short years ago, Node.js is already attracting the accolades and disdain enjoyed and endured by the Ruby and Rails community just a short time ago. It overtook Rails as the most popular Github repository in 2011 and was selected by InfoWorld for the Technology of the Year Award in 2012. This presentation explains the basic theory and programming model central to Node's approach and will help you understand the resulting benefits and challenges it presents. You can also watch this presentation at http://bit.ly/1362UGA

Jeff Kunkle

March 02, 2012
Tweet

More Decks by Jeff Kunkle

Other Decks in Programming

Transcript

  1. Node.js is a platform built on Chrome’s JavaScript runtime for

    easily building fast, scalable network applications. Node.js uses an event driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. DOWNLOAD DOCS v0.6.11
  2. I/O Latency L1 3 cycles L2 14 cycles RAM 250

    cycles Disk 41,000,000 Network 240,000,000 Source: Ryan Dahl’s 2008.11.08 node.js presentation
  3. I/O Latency L2 RAM Disk Network L1 5 83 13,666,666

    80,000,000 L2 18 2,928,571 17,142,857 RAM 164,000 960,000 Disk 6
  4. Scaling with Threads thread 1 thread 2 thread 3 thread

    4 Context switching overhead Execution stacks take up memory Complicates concurrency Handles up to 4 concurrent requests
  5. Scaling with Processes process 1 process 2 process 3 process

    4 High memory usage Handles up to 4 concurrent requests Process scheduling overhead
  6. Scaling with an Event Loop process 1 network filesystem Thread

    Pool and Async I/O APIs Handles many concurrent requests in one process/thread
  7. Platform node standard library node bindings (http, socket, file system)

    V8 thread pool (libeio) event loop (libev) DNS (c-ares) crypto (OpenSSL)
  8. HTTP Server var http = require('http'); http.createServer(function (request, response) {

    response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello World\n'); }).listen(8124); console.log('Server running at http://127.0.0.1:8124/');
  9. Serve a Static File var http = require('http'); http.createServer(function (request,

    response) { fs.readFile('/etc/passwd', function (err, data) { if (err) { response.writeHead(500, err.message); response.end(); } else { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end(data); } }); }).listen(8124);
  10. Read a File in Chunks var fs = require('fs'); var

    stream = fs.createReadStream('huge.txt'); stream.on('data', function (data) { console.log(data); }); stream.on('end', function () { console.log('done'); }); stream.on('error', function (err) { console.log(err); });
  11. Asynchronous I/O var fs = require('fs'); fs.stat('/etc/passwd', function (err, stats)

    { if (err) return; if (stats.isFile()) { fs.readFile('/etc/passwd', function (err, data) { if (err) throw err; console.log(data); }); } });
  12. Rapidly Changing v0.6.11 2012.02.17 v0.7.5 2012.02.23 v0.7.4 2012.02.14 v0.7.3 2012.02.07

    v0.7.2 2012.02.01 v0.7.1 2012.01.23 v0.7.0 2012.01.16 v0.6.10 2012.02.02 v0.6.9 2012.01.27 v0.6.8 2012.01.19 v0.6.7 2012.01.06 v0.6.12 2012.03.02
  13. Fast Ruby 1.9 used what fraction? times more? Ruby 1.9

    used what fraction? times more? Ruby 1.9 used what fraction? times more? Ruby 1.9 used what fraction? times more? Benchmark Time Memory Code pidigits 1 /16 1/ 2 1/ 3 reverse-complement 1/ 2 1/ 2 1/ 2 k-nucleotide 2× 1/ 2 ± binary-trees 6× 1/ 2 ± fasta 7× 6× ± regex-dna 9× 2× ± n-body 16× 1/ 4 ± spectral-norm 19× 1/ 2 ± fannkuch-redux 51× ± ± Source: http://shootout.alioth.debian.org/u32/benchmark.php
  14. Lua

  15. PHP

  16. C

  17. Small Core 36 JavaScript source files 36 C++ source files

    as of March 1, 2012 at 10:50pm on v0.6 branch
  18. 1 install Node from source 2 write some small programs

    3 read the source 4 re-implement a popular pkg
  19. Check out some Packages express web framework persist ORM framework

    db-migrate database migrations async async helpers socket.io realtime networking dnode RPC vows BDD framework java bridge to Java API