Node.js Explained

2,241 views

Published on

Rising from non-existence a few short years ago, Node.js is already attracting the accolades and disdain enjoyed and endured by the Ruby and Rails community just a short time ago. It overtook Rails as the most popular Github repository in 2011 and was selected by InfoWorld for the Technology of the Year Award in 2012. This presentation explains the basic theory and programming model central to Node's approach and will help you understand the resulting benefits and challenges it presents. You can also watch this presentation at http://bit.ly/1362UGA

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,241
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
63
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Node.js Explained

  1. 1. explained March 2, 2012Jeff Kunkle
  2. 2. Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. DOWNLOAD DOCS v0.6.11
  3. 3. 1 Theory2 Challenges3 Benefits
  4. 4. Theory
  5. 5. I/O Latency L1 3 cycles L2 14 cycles RAM 250 cycles Disk 41,000,000 Network 240,000,000 Source: Ryan Dahl’s 2008.11.08 node.js presentation
  6. 6. CPU Cycles300,000,000 240,000,000225,000,000150,000,000 75,000,000 41,000,000 3 14 250 0 L1 L2 RAM Disk Network
  7. 7. I/O Latency L2 RAM Disk Network L1 5 83 13,666,666 80,000,000 L2 18 2,928,571 17,142,857 RAM 164,000 960,000 Disk 6
  8. 8. Waiting... route process format request results response query db or write to web service log file
  9. 9. Scaling with Threads Handles up to 4 concurrent requests thread 1 thread 2 thread 3 thread 4 Context switching overhead Execution stacks take up memory Complicates concurrency
  10. 10. Scaling with Processes Handles up to 4 concurrent requests process 1 process 2 process 3 process 4 High memory usage Process scheduling overhead
  11. 11. Scaling with an Event Loop Handles many concurrent requests in one process/thread process 1 network filesystem Thread Pool and Async I/O APIs
  12. 12. Event Loop Thread Pool Event filesystem Queue network Event Loop process other
  13. 13. Platform node standard library node bindings (http, socket, file system) thread pool event loop crypto DNS V8 (libeio) (libev) (OpenSSL) (c-ares)
  14. 14. ExamplessetTimeout(function () { console.log(This will still run.);}, 500);
  15. 15. HTTP Servervar http = require(http);http.createServer(function (request, response) { response.writeHead(200, {Content-Type: text/plain}); response.end(Hello Worldn);}).listen(8124);console.log(Server running at http://127.0.0.1:8124/);
  16. 16. Serve a Static Filevar http = require(http);http.createServer(function (request, response) { fs.readFile(/etc/passwd, function (err, data) { if (err) { response.writeHead(500, err.message); response.end(); } else { response.writeHead(200, {Content-Type: text/plain}); response.end(data); } });}).listen(8124);
  17. 17. Read a File in Chunksvar fs = require(fs);var stream = fs.createReadStream(huge.txt);stream.on(data, function (data) { console.log(data);});stream.on(end, function () { console.log(done);});stream.on(error, function (err) { console.log(err);});
  18. 18. Challenges
  19. 19. Asynchronous I/Ovar fs = require(fs);fs.stat(/etc/passwd, function (err, stats) { if (err) return; if (stats.isFile()) { fs.readFile(/etc/passwd, function (err, data) { if (err) throw err; console.log(data); }); }});
  20. 20. Debugging
  21. 21. CPU-intensive Tasks 1 thread be careful!
  22. 22. Rapidly Changing v0.6.12 2012.03.02 v0.7.5 2012.02.23 v0.6.11 2012.02.17 v0.7.4 2012.02.14 v0.6.10 2012.02.02 v0.7.3 2012.02.07 v0.6.9 2012.01.27 v0.7.2 2012.02.01 v0.6.8 2012.01.19 v0.7.1 2012.01.23 v0.6.7 2012.01.06 v0.7.0 2012.01.16
  23. 23. Benefits
  24. 24. Multi-platform
  25. 25. Lightweight Single thread
  26. 26. Dead-lock Free Single thread simplifies concurrency
  27. 27. Monoglot Programming JavaScript on the client JavaScript on the server
  28. 28. Popular
  29. 29. Fast Ruby 1.9 used what fraction? times more? Benchmark Time Memory Code pidigits 1 /16 1/ 2 1/ 3 reverse-complement 1/ 2 1/ 2 1/ 2 k-nucleotide 2× 1/ 2 ± binary-trees 6× 1/ 2 ± fasta 7× 6× ± regex-dna 9× 2× ± n-body 16× 1/ 4 ± spectral-norm 19× 1/ 2 ± fannkuch-redux 51× ± ± Source: http://shootout.alioth.debian.org/u32/benchmark.php
  30. 30. Lua
  31. 31. Perl
  32. 32. PHP
  33. 33. Python 3
  34. 34. JRuby
  35. 35. Clojure
  36. 36. Java
  37. 37. C
  38. 38. Vibrant Community 7,661 packages as of 3:00pm EDT 2012.03.02
  39. 39. Conceptually Simple Thread Pool Event filesystem Queue network Event Loop process other
  40. 40. Small Core 36 JavaScript source files 36 C++ source files as of March 1, 2012 at 10:50pm on v0.6 branch
  41. 41. Short Learning Curve Understand Node.js Theory Understand JavaScript Understand Event-based Programming You just need to learn new APIs
  42. 42. Getting Smart
  43. 43. 1 install Node from source2 write some small programs3 read the source4 re-implement a popular pkg
  44. 44. Check out some Packages express web framework async helpers async persist socket.io realtime networking ORM framework db-migrate vowsframework BDD database migrations dnode RPC java to Java API bridge

×