node.js

12
Danilo Sousa [email protected] Node.js

Upload: danilo-sousa

Post on 15-Jan-2015

253 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Node.js

Danilo [email protected]

Node.js

Page 2: Node.js

Introduction

"Node's goal is to provide an easy way to build scalable network programs."

Ryan Dahl

● Server-side Javascript

● Built on Google’s V8

● CommonJS module system

● Evented, non-blocking I/O. Similar to EventMachine or Twisted.

Page 3: Node.js

I/O needs to be done differently.

Page 4: Node.js

I/O Costs

●L1: 3 cycles

●L2: 14 cycles

●RAM: 250 cycles

●DISK: 41,000,000 cycles

●NETWORK: 240,000,000 cycles

http://nodejs.org/jsconf.pdf

Page 5: Node.js

Traditional way

var db = require('db');

row = db.query(' SELECT id, name FROM aaa');

row2 = db.query('SELECT id, name WHERE id = '+row.id );

// do your stuff hereconsole.log( row2.name );

Page 6: Node.js

I/O Costs

●L1: 3 cycles

●L2: 14 cycles

●RAM: 250 cycles

●DISK: 41,000,000 cycles

●NETWORK: 240,000,000 cycles

http://nodejs.org/jsconf.pdf

Page 7: Node.js

Node's first attempt

var db = require('db');

db.query(' SELECT id, name FROM table', function(row){ db.query('SELECT .... WHERE id = '+row.id, function(row2){

// do your stuff here console.log( row2.name ); });});

Page 8: Node.js

Better wayvar db = require('db');

frunction get(cb){ db.query('SELECT id, name FROM table', row1);}

function row1(row, cb){ db.query('SELECT .... WHERE id = '+ row.id, function(row){ cb( row ); });}

get(function(row){ console.log( row.name );});

Page 9: Node.js

speed

Page 10: Node.js

speed

var http = require(’http’);var b = new Buffer(1024*1024);

http.createServer(function(req, res){ res.writeHead(200); res.end(b);}).listen(8000);

by Ryan Dahl

Page 11: Node.js

speed

100 concurrent clients1 megabyte response

req/sec

node ~800nginx ~700

thin 85mongrel 4

(bigger is better)by Ryan Dahl

Page 12: Node.js

Questions?