Node.js

21
node.js building for high performance & high concurrency Nam Ho KMS Technology

Transcript of Node.js

node.jsbuilding for high performance & high concurrency

Nam HoKMS Technology

ALT.NET Saigon (a .NET user group)

Twitter - @hotrannam#altnetsg

About me

Agenda

• Server models• Non-Blocking I/O• Programming model• Demo

Multi-Threaded server

RequestA thread is created

in response to the request

Multi-Threaded server

Spawning threads to handle concurrent requests

Multi-Threaded server

Long running operations cause threads to block.Subsequent requests are in queue.

Multi-Threaded server

Disadvantages

• Thread context switching is slow• Threads take up too much memory• Thread-per-connection does not scale

Blocking I/O

• Synchronous I/O• Wait for I/O operations to complete• Idle time

var post = db.query(“select * from posts where id = 1”);

// waiting, waiting, waiting

showPost(post);

// do something unrelated to postdoAnotherThing();

Non-Blocking I/O

• Asynchronous I/O• Continue processing• Improve throughput and latency

var post = db.query(“select * from posts where id = 1”, callback);

doAnotherThing();

callback = function(post){ showPost(post); }

Single-Threaded server

Endless event-loop fires off long running operations andthen moves to next request

Multi-Threaded vs. Single-Threaded

http://blog.webfaction.com/a-little-holiday-present

Multi-Threaded vs. Single-Threaded

http://blog.webfaction.com/a-little-holiday-present

What is Node

• Created by Ryan Dahl• Google’s V8 JavaScript engine• Event loop• Non-blocking I/O• Event binding

Where does Node fit

Node is ideal for apps which havetens of thousands of concurrent connections.

Why JavaScript

• First class function• Closure • V8 is fast

A simple web server

var http = require("http");

http.createServer(function(req, res){ res.writeHead(200, {"Content-Type": "text/plain"}); return res.end("Hello Barcamp Saigon\n");}).listen(8080);

console.log("Server running at port 8080");

Callback

var http = require("http");

var callback = function(req, res){ res.writeHead(200, {"Content-Type": "text/plain"}); return res.end("Hello Barcamp Saigon\n");}

http.createServer(callback).listen(8080);

console.log("Server running at port 8080");

Event binding

var fs = require(“fs");

var readStream = fs.createReadStream(“/etc/sources”);

readStream.addListener(“data”, function(data){ console.log(data);});

readStream.addListener(“end”, function(data){ console.log(“file ended”);});

Parallel vs. Serial

var fs = require("fs");function writeBarcamp(){ return fs.writeFile('./barcamp.txt', 'Hi Barcamp!', function writeFile(err){ fs.readFile('./barcamp.txt', 'UTF-8', readFile); });}function writeNode(){ return fs.writeFile('./node.txt', 'Hi Node!', function writeFile(err){ fs.readFile('./node.txt', 'UTF-8', readFile); });}function readFile(err){}

writeBarcamp();writeNode();

Demo

Thanks you!

@hotrannam