4. Introduction
• Node.js was first released in 2009 by Ryan Dahl as a reaction to how slow web
servers were at the time
• Most web servers would block for any I/O task, such as reading from the file
system or accessing the network, and this would dramatically lower their
throughput.
• Node.js changed this model by making all I/O tasks non-blocking and
asynchronous. This allowed web servers written in Node.js to serve thousands of
requests concurrently.
6. Introduction
• Illustrates the threaded model processing two requests, GetFile and
GetData.
• The GetFile request first opens the file, reads the contents, and then
sends the data back in a response.
• All this occurs in order on the same thread.
• The GetData request connects to the DB, queries the necessary data,
and then sends the data in the response.
7. Introduction
The Node.js event model does things differently.
• Instead of executing all the work for each request on individual threads,
• work is added to an event queue and then picked up by a single thread running
an event loop.
• The event loop grabs the top item in the event queue, executes it, and then
grabs the next item.
• When executing code that is no longer live or has blocking I/O, instead of
calling the function directly, the function is added to the event queue along
with a callback that is executed after the function completes.
• When all events on the Node.js event queue have been executed, the Node
application terminates.
9. Introduction
• illustrates the way Node.js handles the GetFile and GetData requests.
• The GetFile and GetData requests are added to the event queue.
• Node.js first picks up the GetFile request, executes it, and then completes by
adding the Open() callback function to the event queue.
• Next, it picks up the GetData request, executes it, and completes by adding
the Connect() callback function to the event queue.
• This continues until there are no callback functions to be executed.
• Notice that the events for each thread do not necessarily follow a direct
interleaved order.
• For example, the Connect request takes longer to complete than the Read
request, so Send(file) is called before Query(db).
10. Blocking I/O in Node.js
• Blocking I/O stops the execution of the current thread and waits for a
response before continuing.
• Some examples of blocking I/O are
• Reading a file
• Querying a database
• Socket request
• Accessing a remote service
• Therefore, any requests that perform blocking I/O are performed on a different thread in
the background. Node.js implements a thread pool in the background. When an event that
requires blocking I/O is retrieved from the event queue, Node.js retrieves a thread from the
thread pool and executes the function there instead of on the main event loop thread. This
prevents the blocking I/O from holding up the rest of the events in the event queue
12. Introduction
•In the Node.js event model,
•work is added as a function with a callback to the
event queue,
•and then picked up on the event loop thread.
• The function is then executed on the event loop
thread in the case of non-blocking, or on a separate
thread in the case of blocking