Your SlideShare is downloading. ×
0
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Node.js
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Node.js

2,323

Published on

My talk at BarCamp Saigon Summer 2011

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,323
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
45
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 &amp; high concurrency<br />Nam Ho<br />KMS Technology<br />
  • 2. About me<br />ALT.NET Saigon (a .NET user group)<br />Twitter - @hotrannam<br />#altnetsg<br />
  • 3. Agenda<br />Server models<br />Non-Blocking I/O<br />Programming model<br />Demo<br />
  • 4. Multi-Threaded server<br />Request<br />A thread is created <br />in response to the request<br />
  • 5. Multi-Threaded server<br />Spawning threads to handle concurrent requests <br />
  • 6. Multi-Threaded server<br />Long running operations cause threads to block.<br />Subsequent requests are in queue.<br />
  • 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. 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. 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. Single-Threaded server<br />Endless event-loop fires off long running operations and<br />then moves to next request<br />
  • 11. Multi-Threaded vs. Single-Threaded<br />http://blog.webfaction.com/a-little-holiday-present<br />
  • 12. Multi-Threaded vs. Single-Threaded<br />http://blog.webfaction.com/a-little-holiday-present<br />
  • 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. Where does Node fit<br />Node is ideal for apps which have<br />tens of thousands of <br />concurrent connections.<br />
  • 15. Why JavaScript<br />First class function<br />Closure <br />V8 is fast<br />
  • 16. A simple web server<br />var http = require(&quot;http&quot;);<br />http.createServer(function(req, res){<br /> res.writeHead(200, {&quot;Content-Type&quot;: &quot;text/plain&quot;});<br /> return res.end(&quot;Hello Barcamp Saigonn&quot;);<br />}).listen(8080);<br />console.log(&quot;Server running at port 8080&quot;);<br />
  • 17. Callback<br />var http = require(&quot;http&quot;);<br />var callback = function(req, res){<br /> res.writeHead(200, {&quot;Content-Type&quot;: &quot;text/plain&quot;});<br /> return res.end(&quot;Hello Barcamp Saigonn&quot;);<br />}<br />http.createServer(callback).listen(8080);<br />console.log(&quot;Server running at port 8080&quot;);<br />
  • 18. Event binding<br />var fs = require(“fs&quot;);<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. Parallel vs. Serial<br />var fs = require(&quot;fs&quot;);<br />function writeBarcamp(){<br /> return fs.writeFile(&apos;./barcamp.txt&apos;, &apos;Hi Barcamp!&apos;, <br />function writeFile(err){<br /> fs.readFile(&apos;./barcamp.txt&apos;, &apos;UTF-8&apos;, readFile);<br /> });<br />}<br />function writeNode(){<br /> return fs.writeFile(&apos;./node.txt&apos;, &apos;Hi Node!&apos;, <br />function writeFile(err){<br /> fs.readFile(&apos;./node.txt&apos;, &apos;UTF-8&apos;, readFile);<br /> });<br />}<br />function readFile(err){}<br />writeBarcamp();<br />writeNode();<br />
  • 20. Demo<br />
  • 21. Thanks you!<br />@hotrannam<br />

×