Node js techtalksto

Preview:

DESCRIPTION

James Duncan's March 2nd TechTalksTO presentation on NodeJS

Citation preview

Chief Architect

james@joyent.com

James A. Duncan

Node

Friday, March 4, 2011

Chief Architect @ Joyent

Spend a lot of my time thinking about how things should join together and why they shouldnʼt.

Grew up in Hawkesbury, Ontario

Live in the mountains to the north of Montreal in Quebec, Canada.

Formerly lived in London & was a CIO of a subsidiary of Canon Europe.

Open Source guy for 15 years.

Gardener, skier, and technologist.

James A. Duncan

2

Friday, March 4, 2011

C10K

Friday, March 4, 2011

Friday, March 4, 2011

Friday, March 4, 2011

Between 70-100 microprocessors

in a car!

Friday, March 4, 2011

1 flight, 1 plane, 480 TBs

Friday, March 4, 2011

Intel shipped 3.3 billion CPUs in Q1 2010

1.5ZBs of storage sold in 2010

Friday, March 4, 2011

Scale Up

From 1990 to 2010, “RAM” in computers increased 1,000,000x in the

same power footprint

Friday, March 4, 2011

Computer Scientistsand

Computer Engineers

Friday, March 4, 2011

Friday, March 4, 2011

the node.js project

To provide a purely evented, non-blocking infrastructure to script highly

concurrent programs.

12

Friday, March 4, 2011

TwistedPOEEventMachine

Friday, March 4, 2011

TwistedPOE = Pain.EventMachine

Friday, March 4, 2011

a snippet of code

15

...rows = db.select("col,umns FROM aTable WHERE aRow=‘foo’");do_something( rows );...

Friday, March 4, 2011

Computers can do nothing.Very fast.

Friday, March 4, 2011

L1: 3 cycles

Friday, March 4, 2011

L2: 14 Cycles

Friday, March 4, 2011

RAM: 250 cycles

Friday, March 4, 2011

Disk: 41,000,000 cycles

Friday, March 4, 2011

Network: 240,000,000 cycles

Friday, March 4, 2011

Anything that waits for a response from the disk, or the network can be considered “blocking” IO

Friday, March 4, 2011

a better snippet of code

23

...sql = "col,umns FROM aTable WHERE aRow=‘foo’";db.select(sql, do_something);...

Friday, March 4, 2011

Computer can keep doing things very fast.

Friday, March 4, 2011

Asynchronous IO.

Friday, March 4, 2011

Alternatives

Friday, March 4, 2011

Multiple ProcessesThreadsGreen Threads, Coroutines

Friday, March 4, 2011

Asynchronous IO.Not New.

Friday, March 4, 2011

Cultural Biasputs("Enter your name: ");var name = gets();puts("Name: " + name)

Friday, March 4, 2011

Infrastructural Bias

Friday, March 4, 2011

Callbacks

Friday, March 4, 2011

C will never go away.

Friday, March 4, 2011

Man Pages

Friday, March 4, 2011

Library support

Friday, March 4, 2011

Async File IO

• BSD:

• poll/select

• Linux:

• epoll

• FreeBSD

• kqueue

• Solaris

• /dev/poll

• libevent, libev

35

Friday, March 4, 2011

C will never go away.

Friday, March 4, 2011

C will never go away.Nor will JavaScript.

Friday, March 4, 2011

JavaScript is a cultural fit

38

Friday, March 4, 2011

the node.js project

• Evented server-side javascript

• All IO is non-blocking

• Good at handling lots of different IO at the same time

• Achieves this by making all IO non-blocking

• Primary author, and benevolent dictator for life is Ryan Dahl

• Owned by us (Joyent), but MIT/BSD licensed

• seems to be pretty popular, which is making us happy

• running our http://no.de service - lets you get started using node quickly

• running on HPʼs webOS as the underlying service mechanism (replaced Java)

39

Friday, March 4, 2011

a pleasing example

40

var http = require("http");var net = require("net");var c = 0;

http.createServer( function( req, res ) { c++; res.writeHead(200); res.end("Hello World");}).listen(8000);

net.createServer( function( socket ) { socket.write("connections: " + c); socket.end();}).listen(8001);

Friday, March 4, 2011

speed is obviously important

• Benchmark:

• nginx v0.7.65

• node v0.1.91

• tornado v0.2 (python 2.6.4)

• thin v1.2.7 (ruby 1.9.1-p376)

• Linux 2.53, using Intel Core 2 Duo & 4GB RAM

• Standard hello world, with a 100 byte response

41

Friday, March 4, 2011

establish the competition

42

Friday, March 4, 2011

Why is nginx so fast?

Friday, March 4, 2011

plot node.js results

44

Friday, March 4, 2011

what if we change the response size?

45

Friday, March 4, 2011

Implementation

46

buffer = new Buffer(16*1024);for (i = 0; i < buffer.length; i++) { buffer[i] = 100;}

http.createServer(function(req, res){ res.writeHead(200); res.end(buffer);});

Friday, March 4, 2011

JavaScript is only a little bit slower than lovingly hand-crafted optimized C.

Friday, March 4, 2011

JavaScript is only a little bit slower than lovingly hand-crafted optimized C. Wow.

Friday, March 4, 2011

How is this true?

Friday, March 4, 2011

Friday, March 4, 2011

ECMAScript

Friday, March 4, 2011

Friday, March 4, 2011

Friday, March 4, 2011

Speedy

54

Friday, March 4, 2011

Friday, March 4, 2011

Friday, March 4, 2011

Dogfood.

Friday, March 4, 2011

Friday, March 4, 2011

Libraries

Friday, March 4, 2011

var express = require('express');var app = express.createServer();

app.get("/", function(req, res, next) { res.send("Hello from the URL " + req.url);});

app.get(“/:name”, function(req, res, next) { res.send(“Hello “ + req.params.name);});app.listen(3000);

Web Framework - Express

60

Friday, March 4, 2011

Connect Middleware

Friday, March 4, 2011

Databases

Friday, March 4, 2011

Multi-process

Friday, March 4, 2011

npm

Friday, March 4, 2011

Demo App

Friday, March 4, 2011

Conclusions& Thanks for Listening!

Friday, March 4, 2011