SlideShare a Scribd company logo
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

More Related Content

PDF
Node.js Explained
PDF
Server Side Event Driven Programming
ODP
Asynchronous I/O in NodeJS - new standard or challenges?
KEY
OSCON 2011 - Node.js Tutorial
PDF
NodeJS: an Introduction
PDF
Original slides from Ryan Dahl's NodeJs intro talk
PPTX
Java script at backend nodejs
KEY
Writing robust Node.js applications
Node.js Explained
Server Side Event Driven Programming
Asynchronous I/O in NodeJS - new standard or challenges?
OSCON 2011 - Node.js Tutorial
NodeJS: an Introduction
Original slides from Ryan Dahl's NodeJs intro talk
Java script at backend nodejs
Writing robust Node.js applications

What's hot (20)

PDF
Nodejs vatsal shah
PDF
Communication in Node.js
PPTX
Introduction Node.js
PPTX
Introduction to Node js
PPTX
Introduction to node.js
PDF
NodeJS
PPTX
Introduction to Node.js
KEY
node.js: Javascript's in your backend
PPTX
NodeJS guide for beginners
PDF
Node.js and How JavaScript is Changing Server Programming
PPTX
Introduction to NodeJS
KEY
A million connections and beyond - Node.js at scale
PDF
Philly Tech Week Introduction to NodeJS
PDF
NodeJS for Beginner
PPT
RESTful API In Node Js using Express
PDF
Getting started with developing Nodejs
PDF
Building servers with Node.js
PPTX
Node js introduction
PPTX
introduction to node.js
Nodejs vatsal shah
Communication in Node.js
Introduction Node.js
Introduction to Node js
Introduction to node.js
NodeJS
Introduction to Node.js
node.js: Javascript's in your backend
NodeJS guide for beginners
Node.js and How JavaScript is Changing Server Programming
Introduction to NodeJS
A million connections and beyond - Node.js at scale
Philly Tech Week Introduction to NodeJS
NodeJS for Beginner
RESTful API In Node Js using Express
Getting started with developing Nodejs
Building servers with Node.js
Node js introduction
introduction to node.js
Ad

Viewers also liked (20)

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

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

PDF
Web server
PPTX
05.m3 cms list-ofwebserver
PPT
PPT
Programming Server side with Sevlet
PDF
Servlet and JSP
PPT
slides (PPT)
PDF
Building Modern Distributed Applications in Go with Service Weaver
PDF
Event Driven Architecture Concepts in Web Technologies - Part 2
PPT
GlobalsDB: Its significance for Node.js Developers
PPTX
Building SPA’s (Single Page App) with Backbone.js
PDF
Real time web
PPTX
AJAX for Scalability
PPTX
Ajax For Scalability
PPTX
WEB TECHNOLOGY Unit-3.pptx
PDF
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
PPTX
UNIT - 5.pptx Servlets And Database Connectivity
PDF
JavaScript for NET Developers 1st Edition Ovais Mehboob Ahmed Khan
PPTX
Delivering High Performance Websites with NGINX
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
PDF
WSO2 Application Server
Web server
05.m3 cms list-ofwebserver
Programming Server side with Sevlet
Servlet and JSP
slides (PPT)
Building Modern Distributed Applications in Go with Service Weaver
Event Driven Architecture Concepts in Web Technologies - Part 2
GlobalsDB: Its significance for Node.js Developers
Building SPA’s (Single Page App) with Backbone.js
Real time web
AJAX for Scalability
Ajax For Scalability
WEB TECHNOLOGY Unit-3.pptx
IT2255 Web Essentials - Unit V Servlets and Database Connectivity
UNIT - 5.pptx Servlets And Database Connectivity
JavaScript for NET Developers 1st Edition Ovais Mehboob Ahmed Khan
Delivering High Performance Websites with NGINX
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 1...
WSO2 Application Server

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