node.js explained

44
explained Jeff Kunkle March 2, 2012

Upload: jeff-kunkle

Post on 19-Jan-2015

869 views

Category:

Technology


5 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Node.js Explained

explained

Jeff Kunkle

March 2, 2012

Page 2: Node.js Explained

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable networkapplications. 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

Page 3: Node.js Explained

Theory

Benefits

Challenges

123

Page 4: Node.js Explained

Theory

Page 5: Node.js Explained

I/O LatencyL1 3 cycles

L2 14 cycles

RAM 250 cycles

Disk 41,000,000

Network 240,000,000Source: Ryan Dahl’s 2008.11.08 node.js presentation

Page 6: Node.js Explained

0

75,000,000

150,000,000

225,000,000

300,000,000

L1 L2 RAM Disk Network

240,000,000

41,000,000

250143

CPU Cycles

Page 7: Node.js Explained

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

Page 8: Node.js Explained

Waiting...route

request

query db orweb service

processresults

formatresponse

write tolog file

Page 9: Node.js Explained

Scaling with Threads

thread 1

thread 2

thread 3

thread 4

Context switching overheadExecution stacks take up memoryComplicates concurrency

Handles up to 4 concurrent requests

Page 10: Node.js Explained

Scaling with Processes

process 1

process 2

process 3

process 4

High memory usage

Handles up to 4 concurrent requests

Process scheduling overhead

Page 11: Node.js Explained

Scaling with an Event Loop

process 1

network filesystem

Thread Pool and Async I/O APIs

Handles many concurrent requests in one process/thread

Page 12: Node.js Explained

Event Loop

filesystem

network

process

other

EventQueue

Thread Pool

EventLoop

Page 13: Node.js Explained

Platformnode standard library

node bindings(http, socket, file system)

V8 thread pool(libeio)

event loop(libev)

DNS(c-ares)

crypto(OpenSSL)

Page 14: Node.js Explained

Examples

setTimeout(function () { console.log('This will still run.');}, 500);

Page 15: Node.js Explained

HTTP Servervar 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/');

Page 16: Node.js Explained

Serve a Static Filevar 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);

Page 17: Node.js Explained

Read a File in Chunksvar 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);});

Page 18: Node.js Explained

Challenges

Page 19: Node.js Explained

Asynchronous I/Ovar 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); }); }});

Page 20: Node.js Explained

Debugging

Page 21: Node.js Explained

CPU-intensive Tasks

1thread

be careful!

Page 22: Node.js Explained

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

Page 23: Node.js Explained

Benefits

Page 24: Node.js Explained

Multi-platform

Page 25: Node.js Explained

Lightweight

Single thread

Page 26: Node.js Explained

Dead-lock Free

Single thread simplifies concurrency

Page 27: Node.js Explained

Monoglot Programming

JavaScript on the client

JavaScript on the server

Page 28: Node.js Explained

Popular

Page 29: Node.js Explained

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

Page 30: Node.js Explained

Lua

Page 31: Node.js Explained

Perl

Page 32: Node.js Explained

PHP

Page 33: Node.js Explained

Python 3

Page 34: Node.js Explained

JRuby

Page 35: Node.js Explained

Clojure

Page 36: Node.js Explained

Java

Page 37: Node.js Explained

C

Page 38: Node.js Explained

Vibrant Community

7,661 packagesas of 3:00pm EDT 2012.03.02

Page 39: Node.js Explained

Conceptually Simple

filesystem

network

process

other

EventQueue

Thread Pool

EventLoop

Page 40: Node.js Explained

Small Core

36 JavaScript source files

36 C++ source files

as of March 1, 2012 at 10:50pm on v0.6 branch

Page 41: Node.js Explained

Short Learning Curve

Understand JavaScript

Understand Event-based Programming

You just need to learn new APIs

Understand Node.js Theory

Page 42: Node.js Explained

Getting Smart

Page 43: Node.js Explained

1 install Node from source

2 write some small programs

3 read the source

4 re-implement a popular pkg

Page 44: Node.js Explained

Check out some Packages

expressweb framework

persistORM framework

db-migratedatabase migrations

asyncasync helpers

socket.iorealtime networking

dnodeRPC

vowsBDD framework

javabridge to Java API