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.
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
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
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
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
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
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
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
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
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
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
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