node.jsbuilding for high performance & high concurrency<br />Nam Ho<br />KMS Technology<br />
About me<br />ALT.NET Saigon (a .NET user group)<br />Twitter - @hotrannam<br />#altnetsg<br />
Agenda<br />Server models<br />Non-Blocking I/O<br />Programming model<br />Demo<br />
Multi-Threaded server<br />Request<br />A thread is created <br />in response to the request<br />
Multi-Threaded server<br />Spawning threads to handle concurrent requests <br />
Multi-Threaded server<br />Long running operations cause threads to block.<br />Subsequent requests are in queue.<br />
Multi-Threaded server<br />Disadvantages<br />Thread context switching is slow<br />Threads take up too much memory<br />T...
Blocking I/O<br />Synchronous I/O<br />Wait for I/O operations to complete<br />Idle time<br />var post = db.query(“select...
Non-Blocking I/O<br />Asynchronous I/O<br />Continue processing<br />Improve throughputand latency<br />var post = db.quer...
Single-Threaded server<br />Endless event-loop fires off long running operations and<br />then moves to next request<br />
Multi-Threaded vs. Single-Threaded<br />http://blog.webfaction.com/a-little-holiday-present<br />
Multi-Threaded vs. Single-Threaded<br />http://blog.webfaction.com/a-little-holiday-present<br />
What is Node<br />Created by Ryan Dahl<br />Google’s V8 JavaScript engine<br />Event loop<br />Non-blocking I/O<br />Event...
Where does Node fit<br />Node is ideal for apps which have<br />tens of thousands of <br />concurrent connections.<br />
Why JavaScript<br />First class function<br />Closure <br />V8 is fast<br />
A simple web server<br />var http = require("http");<br />http.createServer(function(req, res){<br />   res.writeHead(200,...
Callback<br />var http = require("http");<br />var callback = function(req, res){<br />   res.writeHead(200, {"Content-Typ...
Event binding<br />var fs = require(“fs");<br />var readStream = fs.createReadStream(“/etc/sources”);<br />readStream.addL...
Parallel vs. Serial<br />var fs = require("fs");<br />function writeBarcamp(){<br />   return fs.writeFile('./barcamp.txt'...
Demo<br />
Thanks you!<br />@hotrannam<br />
Upcoming SlideShare
Loading in...5
×

Node.js

2,375

Published on

My talk at BarCamp Saigon Summer 2011

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,375
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
46
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Node.js

  1. 1. node.jsbuilding for high performance & high concurrency<br />Nam Ho<br />KMS Technology<br />
  2. 2. About me<br />ALT.NET Saigon (a .NET user group)<br />Twitter - @hotrannam<br />#altnetsg<br />
  3. 3. Agenda<br />Server models<br />Non-Blocking I/O<br />Programming model<br />Demo<br />
  4. 4. Multi-Threaded server<br />Request<br />A thread is created <br />in response to the request<br />
  5. 5. Multi-Threaded server<br />Spawning threads to handle concurrent requests <br />
  6. 6. Multi-Threaded server<br />Long running operations cause threads to block.<br />Subsequent requests are in queue.<br />
  7. 7. Multi-Threaded server<br />Disadvantages<br />Thread context switching is slow<br />Threads take up too much memory<br />Thread-per-connection does not scale<br />
  8. 8. Blocking I/O<br />Synchronous I/O<br />Wait for I/O operations to complete<br />Idle time<br />var post = db.query(“select * from posts where id = 1”);<br />// waiting, waiting, waiting<br />showPost(post);<br />// do something unrelated to post<br />doAnotherThing();<br />
  9. 9. Non-Blocking I/O<br />Asynchronous I/O<br />Continue processing<br />Improve throughputand latency<br />var post = db.query(“select * from posts where id = 1”, callback);<br />doAnotherThing();<br />callback = function(post){<br />showPost(post); <br />}<br />
  10. 10. Single-Threaded server<br />Endless event-loop fires off long running operations and<br />then moves to next request<br />
  11. 11. Multi-Threaded vs. Single-Threaded<br />http://blog.webfaction.com/a-little-holiday-present<br />
  12. 12. Multi-Threaded vs. Single-Threaded<br />http://blog.webfaction.com/a-little-holiday-present<br />
  13. 13. What is Node<br />Created by Ryan Dahl<br />Google’s V8 JavaScript engine<br />Event loop<br />Non-blocking I/O<br />Event binding<br />
  14. 14. Where does Node fit<br />Node is ideal for apps which have<br />tens of thousands of <br />concurrent connections.<br />
  15. 15. Why JavaScript<br />First class function<br />Closure <br />V8 is fast<br />
  16. 16. A simple web server<br />var http = require("http");<br />http.createServer(function(req, res){<br /> res.writeHead(200, {"Content-Type": "text/plain"});<br /> return res.end("Hello Barcamp Saigonn");<br />}).listen(8080);<br />console.log("Server running at port 8080");<br />
  17. 17. Callback<br />var http = require("http");<br />var callback = function(req, res){<br /> res.writeHead(200, {"Content-Type": "text/plain"});<br /> return res.end("Hello Barcamp Saigonn");<br />}<br />http.createServer(callback).listen(8080);<br />console.log("Server running at port 8080");<br />
  18. 18. Event binding<br />var fs = require(“fs");<br />var readStream = fs.createReadStream(“/etc/sources”);<br />readStream.addListener(“data”, function(data){<br /> console.log(data);<br />});<br />readStream.addListener(“end”, function(data){<br />console.log(“file ended”);<br />});<br />
  19. 19. Parallel vs. Serial<br />var fs = require("fs");<br />function writeBarcamp(){<br /> return fs.writeFile('./barcamp.txt', 'Hi Barcamp!', <br />function writeFile(err){<br /> fs.readFile('./barcamp.txt', 'UTF-8', readFile);<br /> });<br />}<br />function writeNode(){<br /> return fs.writeFile('./node.txt', 'Hi Node!', <br />function writeFile(err){<br /> fs.readFile('./node.txt', 'UTF-8', readFile);<br /> });<br />}<br />function readFile(err){}<br />writeBarcamp();<br />writeNode();<br />
  20. 20. Demo<br />
  21. 21. Thanks you!<br />@hotrannam<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×