18
Node JS Understanding the Single Thread Event Loop Rares Crisan Lead Educator , BrainStation

Understanding the Single Thread Event Loop

Embed Size (px)

Citation preview

Page 1: Understanding the Single Thread Event Loop

Node JS Understanding the Single Thread Event Loop

Rares Crisan Lead Educator , BrainStation

Page 2: Understanding the Single Thread Event Loop

About the Speaker

Rares Crisan Rares Crisan is an experienced developer that has worked with many startup's in Toronto's growing

tech community. He also has a degree in Computer Science from Queen's and studied biomedical

engineering at UBC before completing a law degree at the UBC Peter A Allard Faculty of Law. Rares

like to merge an academic understanding of technology with the real world applications of it.

Page 3: Understanding the Single Thread Event Loop

f

How Servers Work

What We’ll Cover

What is a thread and what is the difference between a single thread event loop and multithreading.

Multi Threading vrs Single Thread

Performance Gains and Costs

Understanding how server’s handle large volumes of user requests and why Node was created to solve this problem

Node was design to optimally preform as a web server, however, this does not make Node suitable for every type of application.

Page 4: Understanding the Single Thread Event Loop

Web servers preform a very narrow range of operations. For most servers, the slowest operations are I/O events.

Servers.

Page 5: Understanding the Single Thread Event Loop

The Web Server

The Web Server There are many types of servers and they have many different roles in facilitating communication and data transfer. The first HTTP

web server was developed by Tim Berners-Lee in 1989 at CERN for the purpose of posting and sharing scientific information.Web

servers preform many similar operations responding to user data and page requests. A web servers functions may have not vastly

differentiated since its creation, but the volume of requests have increased considerably.

Page 6: Understanding the Single Thread Event Loop

Server I/O

Server I/O At their core, web servers are simply handling a large stream of short input requests and small output payloads. They do not need to

process complex algorithms or return large volumes of data. Improving the performance of a web server requires increasing the

frequency at which inputs can be processed and outputs can be returned

Some examples of I/O functions are:

Reading/Writing to file.

Database lookups

External Api Requests

Page 7: Understanding the Single Thread Event Loop

A thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler

Threads.

Page 8: Understanding the Single Thread Event Loop

Blocking I/O

Blocking I/O Whenever a process needs to run some operations the instructions are broken down into threads. Threads are short instruction sets

that are run on the CPU. Schedulers are programs built into an OS that prioritize which threads are run and which resources they

require in order to run. Whenever a thread is running and using up a resources the CPU is blocked from handling any more threads

until the thread completes its execution and is switched off the CPU. During this time no other threads can be run on the CPU.

Page 9: Understanding the Single Thread Event Loop

Multithreading

Multithreading Multithreading is a technique used to split up threads across multiple cores of a CPU so that when one core becomes available the

next available thread in the queue is executed. The output generated by the thread is sent back to the process and then sent back to

wherever the data needs to go. This can speed up thread execution by reducing the amount of time any one core is blocked.

However, on a quad core computer, the most amount of threads that can be processed at a single time is four.

Page 10: Understanding the Single Thread Event Loop

Single Thread Event Loop

The Single Thread Event Loop Because web servers are designed to handle short, quick, and similar types of request, Node was built to serve those exact needs. It

is equivalent to the idea of pouring a coffee at a coffee shop; it does not take long to pour a single cup of coffee but it can take a long

time to clear a queue if 1000 people show up asking for coffee at once.

Time is lost processing requests when the CPU has to switch from one thread to another; referred to as context switching. The role

of an OS scheduler is to prioritize and move threads on and off the CPU. Node’s event loop relies on the asynchronous features of

JavaScript and utilizes callbacks so the next set of instructions and be added to the single thread after a response is returned from

a worker thread executing the original instructions. Thus, the Node is able to reduce time lost due to context switching.

Page 11: Understanding the Single Thread Event Loop

Multithreading vrs Single

Page 12: Understanding the Single Thread Event Loop

Node Architecture

Node ArchitectureTNode is built upon a multitude of other technologies, languages,

and design paradigms. The reason that Node libraries are built in

JavaScript is because Google’s V8 engine supports

asynchronous JavaScript code. Asynchronous code allows for

callback functions to be stored until needed, Thus, node can

continuously accept inputs and once a response returns, the

callback function can be retrieved from memory and placed on

the call stack to be executed. Ultimately, node is an event-driven

system, which is exactly how the JavaScript language works.

Node Still utilizes OS threads to run tasks such as file I/O or

database queries, however these threads are not part of the

single thread event loop. The completion of these worker threads

are what trigger the callback functions for a particular event

Page 13: Understanding the Single Thread Event Loop

Node Architecture

Node ArchitectureT

Page 14: Understanding the Single Thread Event Loop

Node is designed to preform optimally with small requests, but it’s design can come at a cost.

Performance.

Page 15: Understanding the Single Thread Event Loop

Speed

SpeedT

From the graphs below you can see that Node is able to outperform .NET and PHP, but this trend also extends to

outperforming Rails, Django, and Nginx, for HTTP requests. Node’s speed even allows it to preform well when

handling real time updates of content via sockets.

*Note: The tests below are not perfect but they help give some visual guidelines on Node’s Performance

Source: http://www.salmanq.com/blog/net-and-node-js-performance-comparison/2013/03/ Source: http://www.kogonuso.com/2015/09/comparing-nodejs-vs-php-performance.html

Page 16: Understanding the Single Thread Event Loop

Costs

CostsT

Node’s biggest shortcoming is it’s inability to handle computationally intensive tasks. If an event running through the

event loop takes a large amount of time to execute (like a sorting a really large array), it will inevitably block the

event loop thread.

Another event that can make Node JS run extremely slow is running it on a server with a single core. If Node is

running on a single core, its single thread blocks other OS threads from operating on the CPU. One personal

example I have preformed was placing a PostGres Database on the same server as NodeJS and utilizing a single

core. Context switching is required to unload the Node Thread and load the DB query thread. The delay is

substantial to say the least.

Page 17: Understanding the Single Thread Event Loop

Questions?

Page 18: Understanding the Single Thread Event Loop

Thank you!Follow @BrainStation and @rarescrisan on Twitter.

Toronto 445 King St W, #300 Toronto, ON M5V 1K4

Costa Rica Terracampus Corporativo Torre 1, Piso 5, San José Costa Rica

Vancouver 1110 Hamilton St. #410 Vancouver, BC V6B 2S2

LOCATIONS