Successfully reported this slideshow.
Your SlideShare is downloading. ×

Nodejs Event Driven Concurrency for Web Applications

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Node.js Explained
Node.js Explained
Loading in …3
×

Check these out next

1 of 83 Ad

Nodejs Event Driven Concurrency for Web Applications

We describe the event-driven concurrency model used by Nodejs, a JavaScript server-side scripting platform. An overview of the traditional thread based approach(used by Apache) is also given. We compare both the approaches. An Introduction to Nodejs programming is provided and some useful packages are discussed.

We describe the event-driven concurrency model used by Nodejs, a JavaScript server-side scripting platform. An overview of the traditional thread based approach(used by Apache) is also given. We compare both the approaches. An Introduction to Nodejs programming is provided and some useful packages are discussed.

Advertisement
Advertisement

More Related Content

Slideshows for you (20)

Viewers also liked (20)

Advertisement

Similar to Nodejs Event Driven Concurrency for Web Applications (20)

Nodejs Event Driven Concurrency for Web Applications

  1. 1. Nodejs Event-Driven concurrency for Web Applications Ganesh Iyer @lastlegion http://ganeshiyer.net
  2. 2. What is Node Server-side JavaScript runtime http://ganeshiyer.net
  3. 3. What is Node Server-side JavaScript runtime http://ganeshiyer.net
  4. 4. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine http://ganeshiyer.net
  5. 5. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine http://ganeshiyer.net
  6. 6. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  7. 7. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  8. 8. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  9. 9. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  10. 10. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Uses an event-driven, non-blocking I/O model for building scalable network applications http://ganeshiyer.net
  11. 11. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Uses an event-driven, non-blocking I/O model for building scalable network applications http://ganeshiyer.net
  12. 12. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Uses an event-driven, non-blocking I/O model for building scalable network applications http://ganeshiyer.net
  13. 13. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Uses an event-driven, non-blocking I/O model for building scalable network applications http://ganeshiyer.net
  14. 14. Concurrency in web servers http://ganeshiyer.net
  15. 15. Two main approaches • Threads • Events http://ganeshiyer.net
  16. 16. Thread Based • Creates a new thread( or process) for handling a new connection. http://ganeshiyer.net
  17. 17. Thread Based • Creates a new thread( or process) for handling a new connection. Server Process http://ganeshiyer.net
  18. 18. Thread Based • Creates a new thread( or process) for handling a new connection. New Connection Server Process http://ganeshiyer.net
  19. 19. Thread Based • Creates a new thread( or process) for handling a new connection. Request handler Process New Connection Server Process http://ganeshiyer.net
  20. 20. Thread Based • Creates a new thread( or process) for handling a new connection. Request handler Process Server Process http://ganeshiyer.net
  21. 21. Thread Based • Creates a new thread( or process) for handling a new connection. Request handler Process Server Process http://ganeshiyer.net
  22. 22. Threads used in Apache(MPM) Prefork http://ganeshiyer.net
  23. 23. Threads used in Apache(MPM) Worker http://ganeshiyer.net
  24. 24. Scalability Issues There is a maximum number of threads(T’) that a system can support, beyond which the throughput decreases. http://ganeshiyer.net
  25. 25. Scalability Issues Source: M. Welsh, S. D. Gribble, E. A. Brewer, and D. Culler. A design framework for highly con-current systems. Technical Report UCB/CSD-00-1108, U.C. Berkeley Computer Science Division, April 2000. http://ganeshiyer.net
  26. 26. Scalability Issues Under heavy load a multi-threaded web server consumers large amounts of memory http://ganeshiyer.net
  27. 27. Scalability Issues Under heavy load a multi-threaded web server consumers large amounts of memory Single thread stack for each connection http://ganeshiyer.net
  28. 28. Scalability Issues Under heavy load a multi-threaded web server consumers large amounts of memory Single thread stack for each connection Overhead due to context-switching and scheduling increases drastically with large number of threads http://ganeshiyer.net
  29. 29. Event Based http://ganeshiyer.net
  30. 30. Event Based States Event Event Handler Loop Event Queue http://ganeshiyer.net
  31. 31. Event Based Event Emitters States Event Event Handler Loop Event Queue http://ganeshiyer.net
  32. 32. Event Based Event Emitters States Event Event Handler Loop Event Queue http://ganeshiyer.net
  33. 33. Event Based Use an event loop http://ganeshiyer.net
  34. 34. Event Based Use an event loop Multiple connections are mapped to a single thread. http://ganeshiyer.net
  35. 35. Event Based Use an event loop Multiple connections are mapped to a single thread. This Thread handles all occurring events from I/O operations of these connections and requests. http://ganeshiyer.net
  36. 36. Improved scalability Source: M. Welsh, S. D. Gribble, E. A. Brewer, and D. Culler. A design framework for highly con-current systems. Technical Report UCB/CSD-00-1108, U.C. Berkeley Computer Science Division, April 2000. http://ganeshiyer.net
  37. 37. Improved scalability The event loop has a queue of event handlers that it executes in order. The overhead when switching from one event handler to the next time is much lower. http://ganeshiyer.net
  38. 38. Nodejs http://ganeshiyer.net
  39. 39. Why Server-side JavaScript? Unified language for both front end an backend http://ganeshiyer.net
  40. 40. Why Server-side JavaScript? Unified language for both front end an backend Increased programmer productivity http://ganeshiyer.net
  41. 41. Why Server-side JavaScript? Unified language for both front end an backend Increased programmer productivity Code reuse http://ganeshiyer.net
  42. 42. Why Server-side JavaScript? Unified language for both front end an backend Increased programmer productivity Code reuse exchange of data using JSON http://ganeshiyer.net
  43. 43. Why Server-side JavaScript? Unified language for both front end an backend Increased programmer productivity Code reuse exchange of data using JSON Speed! http://ganeshiyer.net
  44. 44. Why Server-side JavaScript? Unified language for both front end an backend Increased programmer productivity Code reuse exchange of data using JSON Speed! Contrary to popular belief, Javascript with the V8 engine performs is faster than PHP, Ruby and Python http://ganeshiyer.net
  45. 45. Nodejs Architecture http://ganeshiyer.net
  46. 46. Architecture JavaScript C++ http://ganeshiyer.net
  47. 47. Architecture JavaScript C++ JavaScript Engine http://ganeshiyer.net
  48. 48. Architecture JavaScript C++ Event-based fully asynchronous I/O library. http://ganeshiyer.net
  49. 49. Architecture JavaScript C++ The event loop http://ganeshiyer.net
  50. 50. Architecture JavaScript C++ Exposes OS features like file handling, sockets etc. Functionality for HTTP, DNS etc http://ganeshiyer.net
  51. 51. The EventDriven model http://ganeshiyer.net
  52. 52. Nodejs http://ganeshiyer.net
  53. 53. Nodejs: Hello World http://ganeshiyer.net
  54. 54. Nodejs: Hello World Output $ Hello World http://ganeshiyer.net
  55. 55. Hello World revisited VS Synchronous Asynchronous http://ganeshiyer.net
  56. 56. Hello World revisited VS Output $ hello http://ganeshiyer.net
  57. 57. Hello World revisited VS Output $ hello 2 seconds later http://ganeshiyer.net
  58. 58. Hello World revisited VS Output $ hello 2 seconds later $ world http://ganeshiyer.net
  59. 59. Hello World revisited VS Output New request to Node $ hello During those two seconds http://ganeshiyer.net
  60. 60. Hello World revisited VS Output New requests to Node $ hello During those two seconds http://ganeshiyer.net
  61. 61. Hello World revisited VS Since it is non-blocking Output $ hello 2 seconds later $ hello 2 seconds later $ hello 2 seconds later $ world $ world $ world Response Response Response http://ganeshiyer.net
  62. 62. Creating an HTTP server http://ganeshiyer.net
  63. 63. Creating an HTTP server Require http module http://ganeshiyer.net
  64. 64. Creating an HTTP server Callback executed on each request http://ganeshiyer.net
  65. 65. Static file serving web server http://ganeshiyer.net
  66. 66. Static file serving web server Parsing the url for pathname http://ganeshiyer.net
  67. 67. Node provides low-level functionality For buliding web-applications we require higher functionality Node has a lot of packages that help! http://ganeshiyer.net
  68. 68. Node Ecosystem http://ganeshiyer.net
  69. 69. Package management with npm Predominant package manager for node To install ‘express’ package http://ganeshiyer.net
  70. 70. Connect.js : Middleware for nodejs Essential middleware required for developing web applications Implements logging, caching, cookies, sessions etc. http://ganeshiyer.net
  71. 71. Connect.js : Middleware for nodejs Essential middleware required for developing web applications • logger request logger with custom format support • csrf Cross-site request forgery protection • basicAuth basic http authentication • bodyParser extensible request body parser • json application/json parser • urlencoded application/x-www-form-urlencoded parser • cookieParser cookie parser • session session management support with bundled MemoryStore • cookieSession cookie-based session support • staticCache memory cache layer for the static() middleware http://ganeshiyer.net
  72. 72. Connect.js : Middleware for nodejs http://ganeshiyer.net
  73. 73. Express.js : A MVC framework for web Applications http://ganeshiyer.net
  74. 74. Express.js : A MVC framework for web Applications Model View Controller http://ganeshiyer.net
  75. 75. Express.js : A MVC framework for web Applications Model View Controller Performs RESTful routing http://ganeshiyer.net
  76. 76. Express.js : A MVC framework for web Applications Model View Controller Performs RESTful routing Uses Templating for rendering html http://ganeshiyer.net
  77. 77. Scalability across multiple cores/servers http://ganeshiyer.net
  78. 78. Scalability across multiple cores/servers Node is single threaded! http://ganeshiyer.net
  79. 79. Scalability across multiple cores/servers Node is single threaded! How is going to scale on my multi-core server!? http://ganeshiyer.net
  80. 80. Scalability across multiple cores/servers Start child processes on multiple cores/servers. One process manages flow of events and other cores for doing computation. http://ganeshiyer.net
  81. 81. Scalability across multiple cores/servers Cluster is an extensible multi-core server manager for node.js. http://ganeshiyer.net
  82. 82. Questions? http://ganeshiyer.net
  83. 83. Thank You http://ganeshiyer.net

×