SlideShare ist ein Scribd-Unternehmen logo
1 von 83
Nodejs
Event-Driven concurrency for Web
           Applications


           Ganesh Iyer
           @lastlegion
      http://ganeshiyer.net
What is Node
Server-side JavaScript runtime




                    http://ganeshiyer.net
What is Node
Server-side JavaScript runtime




                   http://ganeshiyer.net
What is Node
Server-side JavaScript runtime
Built on Google’s V8 Engine




                   http://ganeshiyer.net
What is Node
Server-side JavaScript runtime
Built on Google’s V8 Engine




                   http://ganeshiyer.net
What is Node
Server-side JavaScript runtime
Built on Google’s V8 Engine

  Why another server side technology?




                   http://ganeshiyer.net
What is Node
Server-side JavaScript runtime
Built on Google’s V8 Engine

  Why another server side technology?




                   http://ganeshiyer.net
What is Node
Server-side JavaScript runtime
Built on Google’s V8 Engine

  Why another server side technology?




                   http://ganeshiyer.net
What is Node
Server-side JavaScript runtime
Built on Google’s V8 Engine

  Why another server side technology?




                   http://ganeshiyer.net
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
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
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
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
Concurrency in web
     servers


      http://ganeshiyer.net
Two main approaches


• Threads

• Events




                 http://ganeshiyer.net
Thread Based
• Creates a new thread( or process) for handling
  a new connection.




                    http://ganeshiyer.net
Thread Based
• Creates a new thread( or process) for handling
  a new connection.




           Server Process
                    http://ganeshiyer.net
Thread Based
  • Creates a new thread( or process) for handling
    a new connection.


New
Connection




             Server Process
                      http://ganeshiyer.net
Thread Based
  • Creates a new thread( or process) for handling
    a new connection.
                                              Request
                                              handler
                                              Process
New
Connection




             Server Process
                      http://ganeshiyer.net
Thread Based
• Creates a new thread( or process) for handling
  a new connection.
                                            Request
                                            handler
                                            Process




           Server Process
                    http://ganeshiyer.net
Thread Based
• Creates a new thread( or process) for handling
  a new connection.
                                            Request
                                            handler
                                            Process




           Server Process
                    http://ganeshiyer.net
Threads used in Apache(MPM)




           Prefork

          http://ganeshiyer.net
Threads used in Apache(MPM)




           Worker
          http://ganeshiyer.net
Scalability Issues


There is a maximum number of threads(T’) that a
system can support, beyond which the
throughput decreases.




                 http://ganeshiyer.net
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
Scalability Issues

Under heavy load a multi-threaded web
server consumers large amounts of memory




                http://ganeshiyer.net
Scalability Issues

Under heavy load a multi-threaded web
server consumers large amounts of memory
   Single thread stack for each connection




                 http://ganeshiyer.net
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
Event Based




  http://ganeshiyer.net
Event Based

                                                                 States




                                         Event   Event Handler
                                         Loop


Event Queue




                 http://ganeshiyer.net
Event Based
      Event Emitters

                                                                       States




                                               Event   Event Handler
                                               Loop


Event Queue




                       http://ganeshiyer.net
Event Based
      Event Emitters

                                                                       States




                                               Event   Event Handler
                                               Loop


Event Queue




                       http://ganeshiyer.net
Event Based
Use an event loop




                    http://ganeshiyer.net
Event Based
Use an event loop
Multiple connections are mapped to a single
thread.




                 http://ganeshiyer.net
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
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
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
Nodejs



 http://ganeshiyer.net
Why Server-side JavaScript?
Unified language for both front end an backend




                   http://ganeshiyer.net
Why Server-side JavaScript?
Unified language for both front end an backend
 Increased programmer productivity




                   http://ganeshiyer.net
Why Server-side JavaScript?
Unified language for both front end an backend
 Increased programmer productivity
 Code reuse




                   http://ganeshiyer.net
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
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
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
Nodejs Architecture




      http://ganeshiyer.net
Architecture

JavaScript



C++




                http://ganeshiyer.net
Architecture

     JavaScript



     C++



JavaScript Engine
                       http://ganeshiyer.net
Architecture

         JavaScript



          C++

Event-based fully
asynchronous I/O library.


                            http://ganeshiyer.net
Architecture

      JavaScript



      C++

The event loop



                      http://ganeshiyer.net
Architecture

         JavaScript



          C++

Exposes OS features like
file handling, sockets
etc. Functionality for
HTTP, DNS etc              http://ganeshiyer.net
The EventDriven model




       http://ganeshiyer.net
Nodejs




http://ganeshiyer.net
Nodejs: Hello World




      http://ganeshiyer.net
Nodejs: Hello World



Output
$ Hello World




                http://ganeshiyer.net
Hello World revisited


              VS


Synchronous                  Asynchronous




               http://ganeshiyer.net
Hello World revisited


               VS


Output
     $ hello




                http://ganeshiyer.net
Hello World revisited


               VS


Output
     $ hello   2 seconds later




                http://ganeshiyer.net
Hello World revisited


               VS


Output
     $ hello   2 seconds later
     $ world




                http://ganeshiyer.net
Hello World revisited


               VS


Output                                    New request to
                                          Node

     $ hello   During those two seconds




                http://ganeshiyer.net
Hello World revisited


               VS


Output                                    New requests
                                          to Node

     $ hello   During those two seconds




                http://ganeshiyer.net
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
Creating an HTTP server




        http://ganeshiyer.net
Creating an HTTP server
                                Require http
                                module




        http://ganeshiyer.net
Creating an HTTP server
                                Callback executed on
                                each request




        http://ganeshiyer.net
Static file serving web server




           http://ganeshiyer.net
Static file serving web server




Parsing
the url for
pathname




                     http://ganeshiyer.net
Node provides low-level functionality
For buliding web-applications we require higher
  functionality
Node has a lot of packages that help!




                   http://ganeshiyer.net
Node Ecosystem




    http://ganeshiyer.net
Package management with npm
Predominant package manager for node

To install ‘express’ package




                    http://ganeshiyer.net
Connect.js : Middleware for nodejs
Essential middleware required for developing
  web applications

Implements logging, caching, cookies, sessions
  etc.




                    http://ganeshiyer.net
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
Connect.js : Middleware for nodejs




              http://ganeshiyer.net
Express.js : A MVC framework for web
              Applications




              http://ganeshiyer.net
Express.js : A MVC framework for web
              Applications
Model View Controller




                  http://ganeshiyer.net
Express.js : A MVC framework for web
              Applications
Model View Controller

Performs RESTful routing




                   http://ganeshiyer.net
Express.js : A MVC framework for web
              Applications
Model View Controller

Performs RESTful routing
Uses Templating for rendering html




                   http://ganeshiyer.net
Scalability across multiple
      cores/servers




          http://ganeshiyer.net
Scalability across multiple
              cores/servers
Node is single threaded!




                    http://ganeshiyer.net
Scalability across multiple
              cores/servers
Node is single threaded!
How is going to scale on my multi-core server!?




                    http://ganeshiyer.net
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
Scalability across multiple
            cores/servers



Cluster is an extensible multi-core server
manager for node.js.




                  http://ganeshiyer.net
Questions?




  http://ganeshiyer.net
Thank You




 http://ganeshiyer.net

Weitere ähnliche Inhalte

Was ist angesagt?

Communication in Node.js
Communication in Node.jsCommunication in Node.js
Communication in Node.jsEdureka!
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node jsAkshay Mathur
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.jsDinesh U
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsVikash Singh
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backendDavid Padbury
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginnersEnoch Joshua
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming Tom Croucher
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleTom Croucher
 
Philly Tech Week Introduction to NodeJS
Philly Tech Week Introduction to NodeJSPhilly Tech Week Introduction to NodeJS
Philly Tech Week Introduction to NodeJSRoss Kukulinski
 
RESTful API In Node Js using Express
RESTful API In Node Js using Express RESTful API In Node Js using Express
RESTful API In Node Js using Express Jeetendra singh
 
Getting started with developing Nodejs
Getting started with developing NodejsGetting started with developing Nodejs
Getting started with developing NodejsPhil Hawksworth
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.jsConFoo
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
 

Was ist angesagt? (20)

Nodejs vatsal shah
Nodejs vatsal shahNodejs vatsal shah
Nodejs vatsal shah
 
Communication in Node.js
Communication in Node.jsCommunication in Node.js
Communication in Node.js
 
Introduction Node.js
Introduction Node.jsIntroduction Node.js
Introduction Node.js
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
NodeJS
NodeJSNodeJS
NodeJS
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginners
 
Node.js
Node.jsNode.js
Node.js
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJS
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
 
Philly Tech Week Introduction to NodeJS
Philly Tech Week Introduction to NodeJSPhilly Tech Week Introduction to NodeJS
Philly Tech Week Introduction to NodeJS
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
RESTful API In Node Js using Express
RESTful API In Node Js using Express RESTful API In Node Js using Express
RESTful API In Node Js using Express
 
Getting started with developing Nodejs
Getting started with developing NodejsGetting started with developing Nodejs
Getting started with developing Nodejs
 
Building servers with Node.js
Building servers with Node.jsBuilding servers with Node.js
Building servers with Node.js
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 

Andere mochten auch

Anatomy of a Modern Node.js Application Architecture
Anatomy of a Modern Node.js Application Architecture Anatomy of a Modern Node.js Application Architecture
Anatomy of a Modern Node.js Application Architecture AppDynamics
 
Node.js and The Internet of Things
Node.js and The Internet of ThingsNode.js and The Internet of Things
Node.js and The Internet of ThingsLosant
 
NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)
NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)
NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)Chris Richardson
 
NodeJS: the good parts? A skeptic’s view (jax jax2013)
NodeJS: the good parts? A skeptic’s view (jax jax2013)NodeJS: the good parts? A skeptic’s view (jax jax2013)
NodeJS: the good parts? A skeptic’s view (jax jax2013)Chris Richardson
 
Complete MVC on NodeJS
Complete MVC on NodeJSComplete MVC on NodeJS
Complete MVC on NodeJSHüseyin BABAL
 
Create Rest API in Nodejs
Create Rest API in Nodejs Create Rest API in Nodejs
Create Rest API in Nodejs Irfan Maulana
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Visual Engineering
 
Architecting large Node.js applications
Architecting large Node.js applicationsArchitecting large Node.js applications
Architecting large Node.js applicationsSergi Mansilla
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with ExamplesGabriele Lana
 
The Enterprise Case for Node.js
The Enterprise Case for Node.jsThe Enterprise Case for Node.js
The Enterprise Case for Node.jsNodejsFoundation
 
NodeJS: the good parts? A skeptic’s view (devnexus2014)
NodeJS: the good parts? A skeptic’s view (devnexus2014)NodeJS: the good parts? A skeptic’s view (devnexus2014)
NodeJS: the good parts? A skeptic’s view (devnexus2014)Chris Richardson
 
NodeJS for Novices - 28/Oct/13 - Winnipeg, MB
NodeJS for Novices - 28/Oct/13 - Winnipeg, MBNodeJS for Novices - 28/Oct/13 - Winnipeg, MB
NodeJS for Novices - 28/Oct/13 - Winnipeg, MBDavid Wesst
 
Les règles de passage
Les règles de passageLes règles de passage
Les règles de passagemarwa baich
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJSClaudio Cicali
 
LUA를 이용한 스마트한 웹서버 만들기 (Ray. Lee)
LUA를 이용한 스마트한 웹서버 만들기 (Ray. Lee)LUA를 이용한 스마트한 웹서버 만들기 (Ray. Lee)
LUA를 이용한 스마트한 웹서버 만들기 (Ray. Lee)삵 (sarc.io)
 
Isomorphic web application
Isomorphic web applicationIsomorphic web application
Isomorphic web applicationOliver N
 
Introdução AngularJs
Introdução AngularJsIntrodução AngularJs
Introdução AngularJsGDGFoz
 

Andere mochten auch (20)

Anatomy of a Modern Node.js Application Architecture
Anatomy of a Modern Node.js Application Architecture Anatomy of a Modern Node.js Application Architecture
Anatomy of a Modern Node.js Application Architecture
 
Node.js and The Internet of Things
Node.js and The Internet of ThingsNode.js and The Internet of Things
Node.js and The Internet of Things
 
(C)NodeJS
(C)NodeJS(C)NodeJS
(C)NodeJS
 
NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)
NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)
NodeJS: the good parts? A skeptic’s view (oredev, oredev2013)
 
NodeJS: the good parts? A skeptic’s view (jax jax2013)
NodeJS: the good parts? A skeptic’s view (jax jax2013)NodeJS: the good parts? A skeptic’s view (jax jax2013)
NodeJS: the good parts? A skeptic’s view (jax jax2013)
 
Complete MVC on NodeJS
Complete MVC on NodeJSComplete MVC on NodeJS
Complete MVC on NodeJS
 
Create Rest API in Nodejs
Create Rest API in Nodejs Create Rest API in Nodejs
Create Rest API in Nodejs
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.
 
Architecting large Node.js applications
Architecting large Node.js applicationsArchitecting large Node.js applications
Architecting large Node.js applications
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
The Enterprise Case for Node.js
The Enterprise Case for Node.jsThe Enterprise Case for Node.js
The Enterprise Case for Node.js
 
Node.js - Best practices
Node.js  - Best practicesNode.js  - Best practices
Node.js - Best practices
 
NodeJS: the good parts? A skeptic’s view (devnexus2014)
NodeJS: the good parts? A skeptic’s view (devnexus2014)NodeJS: the good parts? A skeptic’s view (devnexus2014)
NodeJS: the good parts? A skeptic’s view (devnexus2014)
 
NodeJS for Novices - 28/Oct/13 - Winnipeg, MB
NodeJS for Novices - 28/Oct/13 - Winnipeg, MBNodeJS for Novices - 28/Oct/13 - Winnipeg, MB
NodeJS for Novices - 28/Oct/13 - Winnipeg, MB
 
hbase lab
hbase labhbase lab
hbase lab
 
Les règles de passage
Les règles de passageLes règles de passage
Les règles de passage
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJS
 
LUA를 이용한 스마트한 웹서버 만들기 (Ray. Lee)
LUA를 이용한 스마트한 웹서버 만들기 (Ray. Lee)LUA를 이용한 스마트한 웹서버 만들기 (Ray. Lee)
LUA를 이용한 스마트한 웹서버 만들기 (Ray. Lee)
 
Isomorphic web application
Isomorphic web applicationIsomorphic web application
Isomorphic web application
 
Introdução AngularJs
Introdução AngularJsIntrodução AngularJs
Introdução AngularJs
 

Ähnlich wie Nodejs Event Driven Concurrency for Web Applications

05.m3 cms list-ofwebserver
05.m3 cms list-ofwebserver05.m3 cms list-ofwebserver
05.m3 cms list-ofwebservertarensi
 
Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet backdoor
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSPGary Yeh
 
Building Modern Distributed Applications in Go with Service Weaver
Building Modern Distributed Applications in Go with Service WeaverBuilding Modern Distributed Applications in Go with Service Weaver
Building Modern Distributed Applications in Go with Service WeaverShiju Varghese
 
Event Driven Architecture Concepts in Web Technologies - Part 2
Event Driven Architecture Concepts in Web Technologies - Part 2Event Driven Architecture Concepts in Web Technologies - Part 2
Event Driven Architecture Concepts in Web Technologies - Part 2Hamidreza Soleimani
 
GlobalsDB: Its significance for Node.js Developers
GlobalsDB: Its significance for Node.js DevelopersGlobalsDB: Its significance for Node.js Developers
GlobalsDB: Its significance for Node.js DevelopersRob Tweed
 
AJAX for Scalability
AJAX for ScalabilityAJAX for Scalability
AJAX for ScalabilityTuenti
 
Ajax For Scalability
Ajax For ScalabilityAjax For Scalability
Ajax For Scalabilityerikschultink
 
WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxkarthiksmart21
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivitypkaviya
 
Delivering High Performance Websites with NGINX
Delivering High Performance Websites with NGINXDelivering High Performance Websites with NGINX
Delivering High Performance Websites with NGINXNGINX, Inc.
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...WebStackAcademy
 
Ring: Web Apps in Idiomatic Clojure
Ring: Web Apps in Idiomatic ClojureRing: Web Apps in Idiomatic Clojure
Ring: Web Apps in Idiomatic ClojureMark McGranaghan
 
Everyday tools and tricks for scaling Node.js
Everyday tools and tricks for scaling Node.jsEveryday tools and tricks for scaling Node.js
Everyday tools and tricks for scaling Node.jsNikolay Stoitsev
 

Ähnlich wie Nodejs Event Driven Concurrency for Web Applications (20)

Web server
Web serverWeb server
Web server
 
05.m3 cms list-ofwebserver
05.m3 cms list-ofwebserver05.m3 cms list-ofwebserver
05.m3 cms list-ofwebserver
 
Ecom 1
Ecom 1Ecom 1
Ecom 1
 
Programming Server side with Sevlet
 Programming Server side with Sevlet  Programming Server side with Sevlet
Programming Server side with Sevlet
 
Servlet and JSP
Servlet and JSPServlet and JSP
Servlet and JSP
 
slides (PPT)
slides (PPT)slides (PPT)
slides (PPT)
 
Building Modern Distributed Applications in Go with Service Weaver
Building Modern Distributed Applications in Go with Service WeaverBuilding Modern Distributed Applications in Go with Service Weaver
Building Modern Distributed Applications in Go with Service Weaver
 
Event Driven Architecture Concepts in Web Technologies - Part 2
Event Driven Architecture Concepts in Web Technologies - Part 2Event Driven Architecture Concepts in Web Technologies - Part 2
Event Driven Architecture Concepts in Web Technologies - Part 2
 
GlobalsDB: Its significance for Node.js Developers
GlobalsDB: Its significance for Node.js DevelopersGlobalsDB: Its significance for Node.js Developers
GlobalsDB: Its significance for Node.js Developers
 
Building SPA’s (Single Page App) with Backbone.js
Building SPA’s (Single Page App) with Backbone.jsBuilding SPA’s (Single Page App) with Backbone.js
Building SPA’s (Single Page App) with Backbone.js
 
Real time web
Real time webReal time web
Real time web
 
AJAX for Scalability
AJAX for ScalabilityAJAX for Scalability
AJAX for Scalability
 
Ajax For Scalability
Ajax For ScalabilityAjax For Scalability
Ajax For Scalability
 
WEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptxWEB TECHNOLOGY Unit-3.pptx
WEB TECHNOLOGY Unit-3.pptx
 
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database ConnectivityIT2255 Web Essentials - Unit V Servlets and Database Connectivity
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
 
Delivering High Performance Websites with NGINX
Delivering High Performance Websites with NGINXDelivering High Performance Websites with NGINX
Delivering High Performance Websites with NGINX
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
 
WSO2 Application Server
WSO2 Application ServerWSO2 Application Server
WSO2 Application Server
 
Ring: Web Apps in Idiomatic Clojure
Ring: Web Apps in Idiomatic ClojureRing: Web Apps in Idiomatic Clojure
Ring: Web Apps in Idiomatic Clojure
 
Everyday tools and tricks for scaling Node.js
Everyday tools and tricks for scaling Node.jsEveryday tools and tricks for scaling Node.js
Everyday tools and tricks for scaling Node.js
 

Nodejs Event Driven Concurrency for Web Applications

  • 1. Nodejs Event-Driven concurrency for Web Applications Ganesh Iyer @lastlegion http://ganeshiyer.net
  • 2. What is Node Server-side JavaScript runtime http://ganeshiyer.net
  • 3. What is Node Server-side JavaScript runtime http://ganeshiyer.net
  • 4. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine http://ganeshiyer.net
  • 5. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine http://ganeshiyer.net
  • 6. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  • 7. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  • 8. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  • 9. What is Node Server-side JavaScript runtime Built on Google’s V8 Engine Why another server side technology? http://ganeshiyer.net
  • 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. 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. 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. 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. 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. New Connection Server Process http://ganeshiyer.net
  • 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. 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 Issues There is a maximum number of threads(T’) that a system can support, beyond which the throughput decreases. http://ganeshiyer.net
  • 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. Scalability Issues Under heavy load a multi-threaded web server consumers large amounts of memory http://ganeshiyer.net
  • 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. 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. Event Based http://ganeshiyer.net
  • 30. Event Based States Event Event Handler Loop Event Queue http://ganeshiyer.net
  • 31. Event Based Event Emitters States Event Event Handler Loop Event Queue http://ganeshiyer.net
  • 32. Event Based Event Emitters States Event Event Handler Loop Event Queue http://ganeshiyer.net
  • 33. Event Based Use an event loop http://ganeshiyer.net
  • 34. Event Based Use an event loop Multiple connections are mapped to a single thread. http://ganeshiyer.net
  • 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. 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. 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
  • 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 JSON Speed! 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 JSON Speed! 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. Architecture JavaScript C++ http://ganeshiyer.net
  • 47. Architecture JavaScript C++ JavaScript Engine http://ganeshiyer.net
  • 48. Architecture JavaScript C++ Event-based fully asynchronous I/O library. http://ganeshiyer.net
  • 49. Architecture JavaScript C++ The event loop http://ganeshiyer.net
  • 50. Architecture JavaScript C++ Exposes OS features like file handling, sockets etc. Functionality for HTTP, DNS etc http://ganeshiyer.net
  • 51. The EventDriven model http://ganeshiyer.net
  • 53. Nodejs: Hello World http://ganeshiyer.net
  • 54. Nodejs: Hello World Output $ Hello World http://ganeshiyer.net
  • 55. Hello World revisited VS Synchronous Asynchronous http://ganeshiyer.net
  • 56. Hello World revisited VS Output $ hello http://ganeshiyer.net
  • 57. Hello World revisited VS Output $ hello 2 seconds later http://ganeshiyer.net
  • 58. Hello World revisited VS Output $ hello 2 seconds later $ world http://ganeshiyer.net
  • 59. Hello World revisited VS Output New request to Node $ hello During those two seconds http://ganeshiyer.net
  • 60. Hello World revisited VS Output New requests to Node $ hello During those two seconds http://ganeshiyer.net
  • 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. 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 server Parsing the url for pathname http://ganeshiyer.net
  • 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. Node Ecosystem http://ganeshiyer.net
  • 69. Package management with npm Predominant package manager for node To install ‘express’ package http://ganeshiyer.net
  • 70. Connect.js : Middleware for nodejs Essential middleware required for developing web applications Implements logging, caching, cookies, sessions etc. http://ganeshiyer.net
  • 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. 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 Applications Model View Controller http://ganeshiyer.net
  • 75. Express.js : A MVC framework for web Applications Model View Controller Performs RESTful routing http://ganeshiyer.net
  • 76. Express.js : A MVC framework for web Applications Model View Controller Performs RESTful routing Uses Templating for rendering html http://ganeshiyer.net
  • 77. Scalability across multiple cores/servers http://ganeshiyer.net
  • 78. Scalability across multiple cores/servers Node is single threaded! http://ganeshiyer.net
  • 79. Scalability across multiple cores/servers Node is single threaded! How is going to scale on my multi-core server!? http://ganeshiyer.net
  • 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. Scalability across multiple cores/servers Cluster is an extensible multi-core server manager for node.js. http://ganeshiyer.net