66
Chief Architect [email protected] James A. Duncan Node Friday, March 4, 2011

Node js techtalksto

Embed Size (px)

DESCRIPTION

James Duncan's March 2nd TechTalksTO presentation on NodeJS

Citation preview

Page 1: Node js techtalksto

Chief Architect

[email protected]

James A. Duncan

Node

Friday, March 4, 2011

Page 2: Node js techtalksto

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

Page 3: Node js techtalksto

C10K

Friday, March 4, 2011

Page 4: Node js techtalksto

Friday, March 4, 2011

Page 5: Node js techtalksto

Friday, March 4, 2011

Page 6: Node js techtalksto

Between 70-100 microprocessors

in a car!

Friday, March 4, 2011

Page 7: Node js techtalksto

1 flight, 1 plane, 480 TBs

Friday, March 4, 2011

Page 8: Node js techtalksto

Intel shipped 3.3 billion CPUs in Q1 2010

1.5ZBs of storage sold in 2010

Friday, March 4, 2011

Page 9: Node js techtalksto

Scale Up

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

same power footprint

Friday, March 4, 2011

Page 10: Node js techtalksto

Computer Scientistsand

Computer Engineers

Friday, March 4, 2011

Page 11: Node js techtalksto

Friday, March 4, 2011

Page 12: Node js techtalksto

the node.js project

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

concurrent programs.

12

Friday, March 4, 2011

Page 13: Node js techtalksto

TwistedPOEEventMachine

Friday, March 4, 2011

Page 14: Node js techtalksto

TwistedPOE = Pain.EventMachine

Friday, March 4, 2011

Page 15: Node js techtalksto

a snippet of code

15

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

Friday, March 4, 2011

Page 16: Node js techtalksto

Computers can do nothing.Very fast.

Friday, March 4, 2011

Page 17: Node js techtalksto

L1: 3 cycles

Friday, March 4, 2011

Page 18: Node js techtalksto

L2: 14 Cycles

Friday, March 4, 2011

Page 19: Node js techtalksto

RAM: 250 cycles

Friday, March 4, 2011

Page 20: Node js techtalksto

Disk: 41,000,000 cycles

Friday, March 4, 2011

Page 21: Node js techtalksto

Network: 240,000,000 cycles

Friday, March 4, 2011

Page 22: Node js techtalksto

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

Friday, March 4, 2011

Page 23: Node js techtalksto

a better snippet of code

23

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

Friday, March 4, 2011

Page 24: Node js techtalksto

Computer can keep doing things very fast.

Friday, March 4, 2011

Page 25: Node js techtalksto

Asynchronous IO.

Friday, March 4, 2011

Page 26: Node js techtalksto

Alternatives

Friday, March 4, 2011

Page 27: Node js techtalksto

Multiple ProcessesThreadsGreen Threads, Coroutines

Friday, March 4, 2011

Page 28: Node js techtalksto

Asynchronous IO.Not New.

Friday, March 4, 2011

Page 29: Node js techtalksto

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

Friday, March 4, 2011

Page 30: Node js techtalksto

Infrastructural Bias

Friday, March 4, 2011

Page 31: Node js techtalksto

Callbacks

Friday, March 4, 2011

Page 32: Node js techtalksto

C will never go away.

Friday, March 4, 2011

Page 33: Node js techtalksto

Man Pages

Friday, March 4, 2011

Page 34: Node js techtalksto

Library support

Friday, March 4, 2011

Page 35: Node js techtalksto

Async File IO

• BSD:

• poll/select

• Linux:

• epoll

• FreeBSD

• kqueue

• Solaris

• /dev/poll

• libevent, libev

35

Friday, March 4, 2011

Page 36: Node js techtalksto

C will never go away.

Friday, March 4, 2011

Page 37: Node js techtalksto

C will never go away.Nor will JavaScript.

Friday, March 4, 2011

Page 38: Node js techtalksto

JavaScript is a cultural fit

38

Friday, March 4, 2011

Page 39: Node js techtalksto

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

Page 40: Node js techtalksto

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

Page 41: Node js techtalksto

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

Page 42: Node js techtalksto

establish the competition

42

Friday, March 4, 2011

Page 43: Node js techtalksto

Why is nginx so fast?

Friday, March 4, 2011

Page 44: Node js techtalksto

plot node.js results

44

Friday, March 4, 2011

Page 45: Node js techtalksto

what if we change the response size?

45

Friday, March 4, 2011

Page 46: Node js techtalksto

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

Page 47: Node js techtalksto

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

Friday, March 4, 2011

Page 48: Node js techtalksto

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

Friday, March 4, 2011

Page 49: Node js techtalksto

How is this true?

Friday, March 4, 2011

Page 50: Node js techtalksto

Friday, March 4, 2011

Page 51: Node js techtalksto

ECMAScript

Friday, March 4, 2011

Page 52: Node js techtalksto

Friday, March 4, 2011

Page 53: Node js techtalksto

Friday, March 4, 2011

Page 54: Node js techtalksto

Speedy

54

Friday, March 4, 2011

Page 55: Node js techtalksto

Friday, March 4, 2011

Page 56: Node js techtalksto

Friday, March 4, 2011

Page 57: Node js techtalksto

Dogfood.

Friday, March 4, 2011

Page 58: Node js techtalksto

Friday, March 4, 2011

Page 59: Node js techtalksto

Libraries

Friday, March 4, 2011

Page 60: Node js techtalksto

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

Page 61: Node js techtalksto

Connect Middleware

Friday, March 4, 2011

Page 62: Node js techtalksto

Databases

Friday, March 4, 2011

Page 63: Node js techtalksto

Multi-process

Friday, March 4, 2011

Page 64: Node js techtalksto

npm

Friday, March 4, 2011

Page 65: Node js techtalksto

Demo App

Friday, March 4, 2011

Page 66: Node js techtalksto

Conclusions& Thanks for Listening!

Friday, March 4, 2011