Your SlideShare is downloading. ×
0
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
Nodejs Event Driven Concurrency for Web Applications
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

Nodejs Event Driven Concurrency for Web Applications

4,017

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 …

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
18 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
4,017
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
1
Likes
18
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. NodejsEvent-Driven concurrency for Web Applications Ganesh Iyer @lastlegion http://ganeshiyer.net
  • 2. What is NodeServer-side JavaScript runtime http://ganeshiyer.net
  • 3. What is NodeServer-side JavaScript runtime http://ganeshiyer.net
  • 4. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine http://ganeshiyer.net
  • 5. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine http://ganeshiyer.net
  • 6. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  • 7. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  • 8. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  • 9. What is NodeServer-side JavaScript runtimeBuilt on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  • 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. 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. 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. 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. Concurrency in web servers http://ganeshiyer.net
  • 15. Two main approaches• Threads• Events http://ganeshiyer.net
  • 16. Thread Based• Creates a new thread( or process) for handling a new connection. http://ganeshiyer.net
  • 17. Thread Based• Creates a new thread( or process) for handling a new connection. Server Process http://ganeshiyer.net
  • 18. Thread Based • Creates a new thread( or process) for handling a new connection.NewConnection Server Process http://ganeshiyer.net
  • 19. Thread Based • Creates a new thread( or process) for handling a new connection. Request handler ProcessNewConnection Server Process http://ganeshiyer.net
  • 20. Thread Based• Creates a new thread( or process) for handling a new connection. Request handler Process Server Process http://ganeshiyer.net
  • 21. Thread Based• Creates a new thread( or process) for handling a new connection. Request handler Process Server Process http://ganeshiyer.net
  • 22. Threads used in Apache(MPM) Prefork http://ganeshiyer.net
  • 23. Threads used in Apache(MPM) Worker http://ganeshiyer.net
  • 24. Scalability IssuesThere is a maximum number of threads(T’) that asystem can support, beyond which thethroughput decreases. http://ganeshiyer.net
  • 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. Scalability IssuesUnder heavy load a multi-threaded webserver consumers large amounts of memory http://ganeshiyer.net
  • 27. Scalability IssuesUnder heavy load a multi-threaded webserver consumers large amounts of memory Single thread stack for each connection http://ganeshiyer.net
  • 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. Event Based http://ganeshiyer.net
  • 30. Event Based States Event Event Handler LoopEvent Queue http://ganeshiyer.net
  • 31. Event Based Event Emitters States Event Event Handler LoopEvent Queue http://ganeshiyer.net
  • 32. Event Based Event Emitters States Event Event Handler LoopEvent Queue http://ganeshiyer.net
  • 33. Event BasedUse an event loop http://ganeshiyer.net
  • 34. Event BasedUse an event loopMultiple connections are mapped to a singlethread. http://ganeshiyer.net
  • 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. 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. 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. Nodejs http://ganeshiyer.net
  • 39. Why Server-side JavaScript?Unified language for both front end an backend http://ganeshiyer.net
  • 40. Why Server-side JavaScript?Unified language for both front end an backend Increased programmer productivity http://ganeshiyer.net
  • 41. Why Server-side JavaScript?Unified language for both front end an backend Increased programmer productivity Code reuse http://ganeshiyer.net
  • 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. 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. 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. Nodejs Architecture http://ganeshiyer.net
  • 46. ArchitectureJavaScriptC++ http://ganeshiyer.net
  • 47. Architecture JavaScript C++JavaScript Engine http://ganeshiyer.net
  • 48. Architecture JavaScript C++Event-based fullyasynchronous I/O library. http://ganeshiyer.net
  • 49. Architecture JavaScript C++The event loop http://ganeshiyer.net
  • 50. Architecture JavaScript C++Exposes OS features likefile handling, socketsetc. Functionality forHTTP, DNS etc http://ganeshiyer.net
  • 51. The EventDriven model http://ganeshiyer.net
  • 52. Nodejshttp://ganeshiyer.net
  • 53. Nodejs: Hello World http://ganeshiyer.net
  • 54. Nodejs: Hello WorldOutput$ Hello World http://ganeshiyer.net
  • 55. Hello World revisited VSSynchronous Asynchronous http://ganeshiyer.net
  • 56. Hello World revisited VSOutput $ hello http://ganeshiyer.net
  • 57. Hello World revisited VSOutput $ hello 2 seconds later http://ganeshiyer.net
  • 58. Hello World revisited VSOutput $ hello 2 seconds later $ world http://ganeshiyer.net
  • 59. Hello World revisited VSOutput New request to Node $ hello During those two seconds http://ganeshiyer.net
  • 60. Hello World revisited VSOutput New requests to Node $ hello During those two seconds http://ganeshiyer.net
  • 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. Creating an HTTP server http://ganeshiyer.net
  • 63. Creating an HTTP server Require http module http://ganeshiyer.net
  • 64. Creating an HTTP server Callback executed on each request http://ganeshiyer.net
  • 65. Static file serving web server http://ganeshiyer.net
  • 66. Static file serving web serverParsingthe url forpathname http://ganeshiyer.net
  • 67. Node provides low-level functionalityFor buliding web-applications we require higher functionalityNode has a lot of packages that help! http://ganeshiyer.net
  • 68. Node Ecosystem http://ganeshiyer.net
  • 69. Package management with npmPredominant package manager for nodeTo install ‘express’ package http://ganeshiyer.net
  • 70. Connect.js : Middleware for nodejsEssential middleware required for developing web applicationsImplements logging, caching, cookies, sessions etc. http://ganeshiyer.net
  • 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. Connect.js : Middleware for nodejs http://ganeshiyer.net
  • 73. Express.js : A MVC framework for web Applications http://ganeshiyer.net
  • 74. Express.js : A MVC framework for web ApplicationsModel View Controller http://ganeshiyer.net
  • 75. Express.js : A MVC framework for web ApplicationsModel View ControllerPerforms RESTful routing http://ganeshiyer.net
  • 76. Express.js : A MVC framework for web ApplicationsModel View ControllerPerforms RESTful routingUses Templating for rendering html http://ganeshiyer.net
  • 77. Scalability across multiple cores/servers http://ganeshiyer.net
  • 78. Scalability across multiple cores/serversNode is single threaded! http://ganeshiyer.net
  • 79. Scalability across multiple cores/serversNode is single threaded!How is going to scale on my multi-core server!? http://ganeshiyer.net
  • 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. Scalability across multiple cores/serversCluster is an extensible multi-core servermanager for node.js. http://ganeshiyer.net
  • 82. Questions? http://ganeshiyer.net
  • 83. Thank You http://ganeshiyer.net

×