Node.js
Upcoming SlideShare
Loading in...5
×
 

Node.js

on

  • 2,505 views

My talk at BarCamp Saigon Summer 2011

My talk at BarCamp Saigon Summer 2011

Statistics

Views

Total Views
2,505
Views on SlideShare
2,331
Embed Views
174

Actions

Likes
3
Downloads
37
Comments
0

3 Embeds 174

http://hotrannam.wordpress.com 169
http://yourails.com 4
http://twitter.com 1

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Node.js Node.js Presentation Transcript

  • node.jsbuilding for high performance & high concurrency
    Nam Ho
    KMS Technology
  • About me
    ALT.NET Saigon (a .NET user group)
    Twitter - @hotrannam
    #altnetsg
  • Agenda
    Server models
    Non-Blocking I/O
    Programming model
    Demo
  • Multi-Threaded server
    Request
    A 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 post
    doAnotherThing();
  • Non-Blocking I/O
    Asynchronous I/O
    Continue processing
    Improve throughputand 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 and
    then 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 have
    tens 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 Saigonn");
    }).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 Saigonn");
    }
    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