node js techtalksto
DESCRIPTION
James Duncan's March 2nd TechTalksTO presentation on NodeJSTRANSCRIPT
![Page 1: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/1.jpg)
Chief Architect
James A. Duncan
Node
Friday, March 4, 2011
![Page 2: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/3.jpg)
C10K
Friday, March 4, 2011
![Page 4: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/4.jpg)
Friday, March 4, 2011
![Page 5: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/5.jpg)
Friday, March 4, 2011
![Page 6: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/6.jpg)
Between 70-100 microprocessors
in a car!
Friday, March 4, 2011
![Page 7: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/7.jpg)
1 flight, 1 plane, 480 TBs
Friday, March 4, 2011
![Page 8: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/10.jpg)
Computer Scientistsand
Computer Engineers
Friday, March 4, 2011
![Page 11: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/11.jpg)
Friday, March 4, 2011
![Page 12: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/13.jpg)
TwistedPOEEventMachine
Friday, March 4, 2011
![Page 14: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/14.jpg)
TwistedPOE = Pain.EventMachine
Friday, March 4, 2011
![Page 15: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/16.jpg)
Computers can do nothing.Very fast.
Friday, March 4, 2011
![Page 17: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/17.jpg)
L1: 3 cycles
Friday, March 4, 2011
![Page 18: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/18.jpg)
L2: 14 Cycles
Friday, March 4, 2011
![Page 19: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/19.jpg)
RAM: 250 cycles
Friday, March 4, 2011
![Page 20: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/20.jpg)
Disk: 41,000,000 cycles
Friday, March 4, 2011
![Page 21: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/21.jpg)
Network: 240,000,000 cycles
Friday, March 4, 2011
![Page 22: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/24.jpg)
Computer can keep doing things very fast.
Friday, March 4, 2011
![Page 25: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/25.jpg)
Asynchronous IO.
Friday, March 4, 2011
![Page 26: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/26.jpg)
Alternatives
Friday, March 4, 2011
![Page 27: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/27.jpg)
Multiple ProcessesThreadsGreen Threads, Coroutines
Friday, March 4, 2011
![Page 28: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/28.jpg)
Asynchronous IO.Not New.
Friday, March 4, 2011
![Page 29: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/29.jpg)
Cultural Biasputs("Enter your name: ");var name = gets();puts("Name: " + name)
Friday, March 4, 2011
![Page 30: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/30.jpg)
Infrastructural Bias
Friday, March 4, 2011
![Page 31: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/31.jpg)
Callbacks
Friday, March 4, 2011
![Page 32: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/32.jpg)
C will never go away.
Friday, March 4, 2011
![Page 33: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/33.jpg)
Man Pages
Friday, March 4, 2011
![Page 34: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/34.jpg)
Library support
Friday, March 4, 2011
![Page 35: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/35.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/36.jpg)
C will never go away.
Friday, March 4, 2011
![Page 37: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/37.jpg)
C will never go away.Nor will JavaScript.
Friday, March 4, 2011
![Page 38: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/38.jpg)
JavaScript is a cultural fit
38
Friday, March 4, 2011
![Page 39: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/39.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/40.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/41.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/42.jpg)
establish the competition
42
Friday, March 4, 2011
![Page 43: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/43.jpg)
Why is nginx so fast?
Friday, March 4, 2011
![Page 44: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/44.jpg)
plot node.js results
44
Friday, March 4, 2011
![Page 45: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/45.jpg)
what if we change the response size?
45
Friday, March 4, 2011
![Page 46: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/46.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/47.jpg)
JavaScript is only a little bit slower than lovingly hand-crafted optimized C.
Friday, March 4, 2011
![Page 48: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/48.jpg)
JavaScript is only a little bit slower than lovingly hand-crafted optimized C. Wow.
Friday, March 4, 2011
![Page 49: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/49.jpg)
How is this true?
Friday, March 4, 2011
![Page 50: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/50.jpg)
Friday, March 4, 2011
![Page 51: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/51.jpg)
ECMAScript
Friday, March 4, 2011
![Page 52: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/52.jpg)
Friday, March 4, 2011
![Page 53: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/53.jpg)
Friday, March 4, 2011
![Page 54: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/54.jpg)
Speedy
54
Friday, March 4, 2011
![Page 55: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/55.jpg)
Friday, March 4, 2011
![Page 56: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/56.jpg)
Friday, March 4, 2011
![Page 57: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/57.jpg)
Dogfood.
Friday, March 4, 2011
![Page 58: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/58.jpg)
Friday, March 4, 2011
![Page 59: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/59.jpg)
Libraries
Friday, March 4, 2011
![Page 60: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/60.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/61.jpg)
Connect Middleware
Friday, March 4, 2011
![Page 62: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/62.jpg)
Databases
Friday, March 4, 2011
![Page 63: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/63.jpg)
Multi-process
Friday, March 4, 2011
![Page 64: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/64.jpg)
npm
Friday, March 4, 2011
![Page 65: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/65.jpg)
Demo App
Friday, March 4, 2011
![Page 66: Node js techtalksto](https://reader035.vdocuments.net/reader035/viewer/2022081720/54539e85af795904308b542f/html5/thumbnails/66.jpg)
Conclusions& Thanks for Listening!
Friday, March 4, 2011