Uploaded on

My talk at BarCamp Saigon Summer 2011

My talk at BarCamp Saigon Summer 2011

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,135
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
40
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. node.jsbuilding for high performance & high concurrency
    Nam Ho
    KMS Technology
  • 2. About me
    ALT.NET Saigon (a .NET user group)
    Twitter - @hotrannam
    #altnetsg
  • 3. Agenda
    Server models
    Non-Blocking I/O
    Programming model
    Demo
  • 4. Multi-Threaded server
    Request
    A thread is created
    in response to the request
  • 5. Multi-Threaded server
    Spawning threads to handle concurrent requests
  • 6. Multi-Threaded server
    Long running operations cause threads to block.
    Subsequent requests are in queue.
  • 7. Multi-Threaded server
    Disadvantages
    Thread context switching is slow
    Threads take up too much memory
    Thread-per-connection does not scale
  • 8. 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();
  • 9. 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);
    }
  • 10. Single-Threaded server
    Endless event-loop fires off long running operations and
    then moves to next request
  • 11. Multi-Threaded vs. Single-Threaded
    http://blog.webfaction.com/a-little-holiday-present
  • 12. Multi-Threaded vs. Single-Threaded
    http://blog.webfaction.com/a-little-holiday-present
  • 13. What is Node
    Created by Ryan Dahl
    Google’s V8 JavaScript engine
    Event loop
    Non-blocking I/O
    Event binding
  • 14. Where does Node fit
    Node is ideal for apps which have
    tens of thousands of
    concurrent connections.
  • 15. Why JavaScript
    First class function
    Closure
    V8 is fast
  • 16. 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");
  • 17. 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");
  • 18. 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”);
    });
  • 19. 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();
  • 20. Demo
  • 21. Thanks you!
    @hotrannam