Nodejs Event Driven Concurrency for Web Applications

5,883 views
5,582 views

Published on

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.

1 Comment
26 Likes
Statistics
Notes
  • It is good. Try to share more presentations about your knowledge on programming..
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
5,883
On SlideShare
0
From Embeds
0
Number of Embeds
47
Actions
Shares
0
Downloads
0
Comments
1
Likes
26
Embeds 0
No embeds

No notes for slide

Nodejs Event Driven Concurrency for Web Applications

  1. 1. NodejsEvent-Driven concurrency for Web Applications Ganesh Iyer @lastlegion http://ganeshiyer.net
  2. 2. What is NodeServer-side JavaScript runtime http://ganeshiyer.net
  3. 3. What is NodeServer-side JavaScript runtime http://ganeshiyer.net
  4. 4. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine http://ganeshiyer.net
  5. 5. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine http://ganeshiyer.net
  6. 6. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  7. 7. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  8. 8. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  9. 9. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  10. 10. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 EngineUses an event-driven, non-blocking I/O modelfor building scalable network applications http://ganeshiyer.net
  11. 11. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 EngineUses an event-driven, non-blocking I/O modelfor building scalable network applications http://ganeshiyer.net
  12. 12. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 EngineUses an event-driven, non-blocking I/O modelfor building scalable network applications http://ganeshiyer.net
  13. 13. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 EngineUses an event-driven, non-blocking I/O modelfor 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.NewConnection Server Process http://ganeshiyer.net
  19. 19. Thread Based • Creates a new thread( or process) for handling a new connection. Request handler ProcessNewConnection 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 IssuesThere is a maximum number of threads(T’) that asystem can support, beyond which thethroughput decreases. http://ganeshiyer.net
  25. 25. Scalability IssuesSource: 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 IssuesUnder heavy load a multi-threaded webserver consumers large amounts of memory http://ganeshiyer.net
  27. 27. Scalability IssuesUnder heavy load a multi-threaded webserver consumers large amounts of memory Single thread stack for each connection http://ganeshiyer.net
  28. 28. Scalability IssuesUnder heavy load a multi-threaded webserver consumers large amounts of memory Single thread stack for each connectionOverhead due to context-switching andscheduling increases drastically with largenumber of threads http://ganeshiyer.net
  29. 29. Event Based http://ganeshiyer.net
  30. 30. Event Based States Event Event Handler LoopEvent Queue http://ganeshiyer.net
  31. 31. Event Based Event Emitters States Event Event Handler LoopEvent Queue http://ganeshiyer.net
  32. 32. Event Based Event Emitters States Event Event Handler LoopEvent Queue http://ganeshiyer.net
  33. 33. Event BasedUse an event loop http://ganeshiyer.net
  34. 34. Event BasedUse an event loopMultiple connections are mapped to a singlethread. http://ganeshiyer.net
  35. 35. Event BasedUse an event loopMultiple connections are mapped to a singlethread.This Thread handles all occurring events fromI/O operations of these connections andrequests. http://ganeshiyer.net
  36. 36. Improved scalabilitySource: 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 scalabilityThe event loop has a queue of event handlersthat it executes in order.The overhead when switching from one eventhandler 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 JSONSpeed! 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 JSONSpeed! 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. ArchitectureJavaScriptC++ http://ganeshiyer.net
  47. 47. Architecture JavaScript C++JavaScript Engine http://ganeshiyer.net
  48. 48. Architecture JavaScript C++Event-based fullyasynchronous 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 likefile handling, socketsetc. Functionality forHTTP, DNS etc http://ganeshiyer.net
  51. 51. The EventDriven model http://ganeshiyer.net
  52. 52. Nodejshttp://ganeshiyer.net
  53. 53. Nodejs: Hello World http://ganeshiyer.net
  54. 54. Nodejs: Hello WorldOutput$ Hello World http://ganeshiyer.net
  55. 55. Hello World revisited VSSynchronous Asynchronous http://ganeshiyer.net
  56. 56. Hello World revisited VSOutput $ hello http://ganeshiyer.net
  57. 57. Hello World revisited VSOutput $ hello 2 seconds later http://ganeshiyer.net
  58. 58. Hello World revisited VSOutput $ hello 2 seconds later $ world http://ganeshiyer.net
  59. 59. Hello World revisited VSOutput New request to Node $ hello During those two seconds http://ganeshiyer.net
  60. 60. Hello World revisited VSOutput New requests to Node $ hello During those two seconds http://ganeshiyer.net
  61. 61. Hello World revisited VS Since it is non-blockingOutput$ 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 serverParsingthe url forpathname http://ganeshiyer.net
  67. 67. Node provides low-level functionalityFor buliding web-applications we require higher functionalityNode has a lot of packages that help! http://ganeshiyer.net
  68. 68. Node Ecosystem http://ganeshiyer.net
  69. 69. Package management with npmPredominant package manager for nodeTo install ‘express’ package http://ganeshiyer.net
  70. 70. Connect.js : Middleware for nodejsEssential middleware required for developing web applicationsImplements logging, caching, cookies, sessions etc. http://ganeshiyer.net
  71. 71. Connect.js : Middleware for nodejsEssential 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 ApplicationsModel View Controller http://ganeshiyer.net
  75. 75. Express.js : A MVC framework for web ApplicationsModel View ControllerPerforms RESTful routing http://ganeshiyer.net
  76. 76. Express.js : A MVC framework for web ApplicationsModel View ControllerPerforms RESTful routingUses Templating for rendering html http://ganeshiyer.net
  77. 77. Scalability across multiple cores/servers http://ganeshiyer.net
  78. 78. Scalability across multiple cores/serversNode is single threaded! http://ganeshiyer.net
  79. 79. Scalability across multiple cores/serversNode is single threaded!How is going to scale on my multi-core server!? http://ganeshiyer.net
  80. 80. Scalability across multiple cores/serversStart child processes on multiplecores/servers.One process manages flow of events andother cores for doing computation. http://ganeshiyer.net
  81. 81. Scalability across multiple cores/serversCluster is an extensible multi-core servermanager for node.js. http://ganeshiyer.net
  82. 82. Questions? http://ganeshiyer.net
  83. 83. Thank You http://ganeshiyer.net

×