Node.js concurrency

Node.js concurrency
Giacomo Fornari
University of Padua,
Sistemi concorrenti e distribuiti,
2016 - 2017
What is Node.js?
2
Two theses
1. waiting an I/O operation is a waste of time
2. thread-per-connection is memory-expensive
“Non-blocking”
L1-cache
L2-cache
RAM
Disk
Network
The cost of I/O
3 cycles
14 cycles
250 cycles
41 000 000 cycles
240 000 000 cycles
“Blocking”
3
Ryan Dahl: Introduction to Node.js, https://www.youtube.com/watch?v=M-sc73Y-zQA
Features
● Single-thread
● Highly concurrent
● No parallelism
● Event-driven
Programmer’s point of view
4
“everything runs in parallel except your code” 1
1. http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
Architecture
Node Bindings
Node Standard Library
V8
Thread
Pool
Event
Loop
JavaScript
C
5
Event Loop
● Has a sequential queue of functions linked with events
● Runs on single-thread
○ it has only one stack
● The functions are defined by callbacks
○ short execution time
○ they can yield events and asynchronous operations
● Implicit usage
6
Event Loop diagram
Node.js
Application
Node.js
Event Loop
Thread 1 Thread 2 Thread n
…
(function,
callback)
Task 1
Task 2
Task 3
Return 1
Task 4
Callback 1
7
Thread Pool
● The strategy to achieve asynchronous I/O is not always a thread pool
○ Yes file-system operation
○ No network operations
● It has a fixed number of 4 thread
○ It is possible to set the number of thread before it is requested and created
● It has an internal queue of
○ function to execute
○ data for the function
○ function to call with the result
8
Thread delegation
9
Call function with callback Submit a work request
Return to JS codeContinue processing
Process the work request
Finish processing and
return result
Call the callback
Execute the callback of the
function
JavaScript C++
Main thread Worker thread
Event Loop and Thread Pool diagram
10https://strongloop.com/strongblog/node-js-is-faster-than-java/
return vs. callback
function add(a, b) {
return a + b;
}
function add(a, b, cb) {
cb(a + b);
}
11
sync op vs. async op
const dinner = ['pasta', 'meat', 'tiramisù'];
dinner.forEach((food) => console.log(food));
const fs = require('fs');
dinner.forEach((food) => {
fs.readdir('.', () => {
console.log(food);
});
});
console.log('prosecco');
12
pasta
meat
tiramisù
prosecco
meat
tiramisù
pasta
● Concurrency reasoning easy
● No concurrent access of the state
● No locks
● No deadlock
● Still possible
○ starvation
○ race condition
Conclusions
13
14
References
● http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
● http://www.journaldev.com/7462/node-js-architecture-single-threaded-event-loop
● http://neilk.net/blog/2013/04/30/why-you-should-use-nodejs-for-CPU-bound-tasks/
● https://oparu.uni-ulm.de/xmlui/bitstream/handle/123456789/2450/vts_8082_11772.pdf, ch. 5.5
● https://strongloop.com/strongblog/node-js-is-faster-than-java/
● http://www.slideshare.net/RonPerlmuter/nodejs-meetup-at-palo-alto-networks-tel-aviv
● http://www.slideshare.net/ganeshiyer7/nodejs-event-driven-concurrency-for-web-applications
● http://stackoverflow.com/questions/10680601/nodejs-event-loop
● http://stackoverflow.com/questions/22644328/when-is-the-thread-pool-used
● https://www.youtube.com/watch?v=M-sc73Y-zQA
1 sur 14

Recommandé

NodeJS Concurrency par
NodeJS ConcurrencyNodeJS Concurrency
NodeJS Concurrencypgriess
6.4K vues12 diapositives
Node.js and How JavaScript is Changing Server Programming par
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
10.4K vues65 diapositives
Introduction to node.js aka NodeJS par
Introduction to node.js aka NodeJSIntroduction to node.js aka NodeJS
Introduction to node.js aka NodeJSJITENDRA KUMAR PATEL
2.2K vues38 diapositives
Introduction to Node.js: What, why and how? par
Introduction to Node.js: What, why and how?Introduction to Node.js: What, why and how?
Introduction to Node.js: What, why and how?Christian Joudrey
1.6K vues21 diapositives
Node.js Patterns for Discerning Developers par
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developerscacois
34.8K vues76 diapositives
Introduction to Node.js par
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsRob O'Doherty
2.9K vues68 diapositives

Contenu connexe

Tendances

Node.js for beginner par
Node.js for beginnerNode.js for beginner
Node.js for beginnerSarunyhot Suwannachoti
563 vues32 diapositives
Node.js tutoria for beginner par
Node.js tutoria for beginnerNode.js tutoria for beginner
Node.js tutoria for beginnerManinder Singh
619 vues12 diapositives
Introduction to node.js by jiban par
Introduction to node.js by jibanIntroduction to node.js by jiban
Introduction to node.js by jibanJibanananda Sana
2.5K vues34 diapositives
Nodejs intro par
Nodejs introNodejs intro
Nodejs introNdjido Ardo BAR
3.4K vues13 diapositives
NodeJS - Server Side JS par
NodeJS - Server Side JS NodeJS - Server Side JS
NodeJS - Server Side JS Ganesh Kondal
5K vues28 diapositives
Java script at backend nodejs par
Java script at backend   nodejsJava script at backend   nodejs
Java script at backend nodejsAmit Thakkar
1.8K vues34 diapositives

Tendances(20)

Java script at backend nodejs par Amit Thakkar
Java script at backend   nodejsJava script at backend   nodejs
Java script at backend nodejs
Amit Thakkar1.8K vues
Create a RESTful API with NodeJS, Express and MongoDB par Hengki Sihombing
Create a RESTful API with NodeJS, Express and MongoDBCreate a RESTful API with NodeJS, Express and MongoDB
Create a RESTful API with NodeJS, Express and MongoDB
Hengki Sihombing961 vues
JavaScript is the new black - Why Node.js is going to rock your world - Web 2... par Tom Croucher
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
JavaScript is the new black - Why Node.js is going to rock your world - Web 2...
Tom Croucher1.2K vues
Non-blocking I/O, Event loops and node.js par Marcus Frödin
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
Marcus Frödin22.8K vues
Node.js, for architects - OpenSlava 2013 par Oscar Renalias
Node.js, for architects - OpenSlava 2013Node.js, for architects - OpenSlava 2013
Node.js, for architects - OpenSlava 2013
Oscar Renalias13.7K vues
Introduction to node.js par jacekbecela
Introduction to node.jsIntroduction to node.js
Introduction to node.js
jacekbecela9.4K vues
OSCON 2011 - Node.js Tutorial par Tom Croucher
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js Tutorial
Tom Croucher5.3K vues
Intro to Node.js (v1) par Chris Cowan
Intro to Node.js (v1)Intro to Node.js (v1)
Intro to Node.js (v1)
Chris Cowan1.5K vues
Understanding the Single Thread Event Loop par TorontoNodeJS
Understanding the Single Thread Event LoopUnderstanding the Single Thread Event Loop
Understanding the Single Thread Event Loop
TorontoNodeJS1.8K vues
Basic Understanding and Implement of Node.js par Gary Yeh
Basic Understanding and Implement of Node.jsBasic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.js
Gary Yeh886 vues

Similaire à Node.js concurrency

Introduction to NodeJS par
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJSUttam Aaseri
230 vues23 diapositives
Nodejs par
NodejsNodejs
NodejsVinod Kumar Marupu
442 vues21 diapositives
Kubernetes @ Squarespace: Kubernetes in the Datacenter par
Kubernetes @ Squarespace: Kubernetes in the DatacenterKubernetes @ Squarespace: Kubernetes in the Datacenter
Kubernetes @ Squarespace: Kubernetes in the DatacenterKevin Lynch
1.2K vues53 diapositives
NodeJS for Beginner par
NodeJS for BeginnerNodeJS for Beginner
NodeJS for BeginnerApaichon Punopas
15K vues54 diapositives
Nodejs - Should Ruby Developers Care? par
Nodejs - Should Ruby Developers Care?Nodejs - Should Ruby Developers Care?
Nodejs - Should Ruby Developers Care?Felix Geisendörfer
3.8K vues52 diapositives
introduction to node.js par
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
3.7K vues30 diapositives

Similaire à Node.js concurrency(20)

Kubernetes @ Squarespace: Kubernetes in the Datacenter par Kevin Lynch
Kubernetes @ Squarespace: Kubernetes in the DatacenterKubernetes @ Squarespace: Kubernetes in the Datacenter
Kubernetes @ Squarespace: Kubernetes in the Datacenter
Kevin Lynch1.2K vues
introduction to node.js par orkaplan
introduction to node.jsintroduction to node.js
introduction to node.js
orkaplan3.7K vues
Node js par hazzaz
Node jsNode js
Node js
hazzaz937 vues
Looming Marvelous - Virtual Threads in Java Javaland.pdf par jexp
Looming Marvelous - Virtual Threads in Java Javaland.pdfLooming Marvelous - Virtual Threads in Java Javaland.pdf
Looming Marvelous - Virtual Threads in Java Javaland.pdf
jexp113 vues
Introduce about Nodejs - duyetdev.com par Van-Duyet Le
Introduce about Nodejs - duyetdev.comIntroduce about Nodejs - duyetdev.com
Introduce about Nodejs - duyetdev.com
Van-Duyet Le867 vues
Scalable server component using NodeJS & ExpressJS par Andhy Koesnandar
Scalable server component using NodeJS & ExpressJSScalable server component using NodeJS & ExpressJS
Scalable server component using NodeJS & ExpressJS
Andhy Koesnandar1.2K vues
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н... par JSFestUA
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP н...
JSFestUA287 vues
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC... par Vitalii Kukhar
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
JS Fest 2019: Comparing Node.js processes and threads for clustering HTTP, TC...
Vitalii Kukhar327 vues
Binary Studio Academy: Concurrency in C# 5.0 par Binary Studio
Binary Studio Academy: Concurrency in C# 5.0Binary Studio Academy: Concurrency in C# 5.0
Binary Studio Academy: Concurrency in C# 5.0
Binary Studio273 vues
Original slides from Ryan Dahl's NodeJs intro talk par Aarti Parikh
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talk
Aarti Parikh2.1K vues
node.js: Javascript's in your backend par David Padbury
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
David Padbury12.6K vues

Dernier

2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx par
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptxanimuscrm
13 vues19 diapositives
Advanced API Mocking Techniques par
Advanced API Mocking TechniquesAdvanced API Mocking Techniques
Advanced API Mocking TechniquesDimpy Adhikary
19 vues11 diapositives
What Can Employee Monitoring Software Do?​ par
What Can Employee Monitoring Software Do?​What Can Employee Monitoring Software Do?​
What Can Employee Monitoring Software Do?​wAnywhere
21 vues11 diapositives
Unleash The Monkeys par
Unleash The MonkeysUnleash The Monkeys
Unleash The MonkeysJacob Duijzer
7 vues28 diapositives
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM... par
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...Deltares
7 vues40 diapositives
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... par
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...Marc Müller
38 vues62 diapositives

Dernier(20)

2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx par animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm13 vues
What Can Employee Monitoring Software Do?​ par wAnywhere
What Can Employee Monitoring Software Do?​What Can Employee Monitoring Software Do?​
What Can Employee Monitoring Software Do?​
wAnywhere21 vues
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM... par Deltares
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...
DSD-INT 2023 Next-Generation Flood Inundation Mapping for Taiwan - Delft3D FM...
Deltares7 vues
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... par Marc Müller
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
Marc Müller38 vues
SUGCON ANZ Presentation V2.1 Final.pptx par Jack Spektor
SUGCON ANZ Presentation V2.1 Final.pptxSUGCON ANZ Presentation V2.1 Final.pptx
SUGCON ANZ Presentation V2.1 Final.pptx
Jack Spektor22 vues
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea... par Safe Software
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...
Geospatial Synergy: Amplifying Efficiency with FME & Esri ft. Peak Guest Spea...
Safe Software412 vues
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... par Deltares
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
Deltares9 vues
MariaDB stored procedures and why they should be improved par Federico Razzoli
MariaDB stored procedures and why they should be improvedMariaDB stored procedures and why they should be improved
MariaDB stored procedures and why they should be improved
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports par Ra'Fat Al-Msie'deen
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug ReportsBushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
BushraDBR: An Automatic Approach to Retrieving Duplicate Bug Reports
DSD-INT 2023 FloodAdapt - A decision-support tool for compound flood risk mit... par Deltares
DSD-INT 2023 FloodAdapt - A decision-support tool for compound flood risk mit...DSD-INT 2023 FloodAdapt - A decision-support tool for compound flood risk mit...
DSD-INT 2023 FloodAdapt - A decision-support tool for compound flood risk mit...
Deltares13 vues
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -... par Deltares
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
Deltares6 vues
Software testing company in India.pptx par SakshiPatel82
Software testing company in India.pptxSoftware testing company in India.pptx
Software testing company in India.pptx
SakshiPatel827 vues
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ... par Deltares
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
Deltares9 vues
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI... par Marc Müller
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Dev-Cloud Conference 2023 - Continuous Deployment Showdown: Traditionelles CI...
Marc Müller36 vues

Node.js concurrency

  • 1. Node.js concurrency Giacomo Fornari University of Padua, Sistemi concorrenti e distribuiti, 2016 - 2017
  • 3. Two theses 1. waiting an I/O operation is a waste of time 2. thread-per-connection is memory-expensive “Non-blocking” L1-cache L2-cache RAM Disk Network The cost of I/O 3 cycles 14 cycles 250 cycles 41 000 000 cycles 240 000 000 cycles “Blocking” 3 Ryan Dahl: Introduction to Node.js, https://www.youtube.com/watch?v=M-sc73Y-zQA
  • 4. Features ● Single-thread ● Highly concurrent ● No parallelism ● Event-driven Programmer’s point of view 4 “everything runs in parallel except your code” 1 1. http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
  • 5. Architecture Node Bindings Node Standard Library V8 Thread Pool Event Loop JavaScript C 5
  • 6. Event Loop ● Has a sequential queue of functions linked with events ● Runs on single-thread ○ it has only one stack ● The functions are defined by callbacks ○ short execution time ○ they can yield events and asynchronous operations ● Implicit usage 6
  • 7. Event Loop diagram Node.js Application Node.js Event Loop Thread 1 Thread 2 Thread n … (function, callback) Task 1 Task 2 Task 3 Return 1 Task 4 Callback 1 7
  • 8. Thread Pool ● The strategy to achieve asynchronous I/O is not always a thread pool ○ Yes file-system operation ○ No network operations ● It has a fixed number of 4 thread ○ It is possible to set the number of thread before it is requested and created ● It has an internal queue of ○ function to execute ○ data for the function ○ function to call with the result 8
  • 9. Thread delegation 9 Call function with callback Submit a work request Return to JS codeContinue processing Process the work request Finish processing and return result Call the callback Execute the callback of the function JavaScript C++ Main thread Worker thread
  • 10. Event Loop and Thread Pool diagram 10https://strongloop.com/strongblog/node-js-is-faster-than-java/
  • 11. return vs. callback function add(a, b) { return a + b; } function add(a, b, cb) { cb(a + b); } 11
  • 12. sync op vs. async op const dinner = ['pasta', 'meat', 'tiramisù']; dinner.forEach((food) => console.log(food)); const fs = require('fs'); dinner.forEach((food) => { fs.readdir('.', () => { console.log(food); }); }); console.log('prosecco'); 12 pasta meat tiramisù prosecco meat tiramisù pasta
  • 13. ● Concurrency reasoning easy ● No concurrent access of the state ● No locks ● No deadlock ● Still possible ○ starvation ○ race condition Conclusions 13
  • 14. 14 References ● http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/ ● http://www.journaldev.com/7462/node-js-architecture-single-threaded-event-loop ● http://neilk.net/blog/2013/04/30/why-you-should-use-nodejs-for-CPU-bound-tasks/ ● https://oparu.uni-ulm.de/xmlui/bitstream/handle/123456789/2450/vts_8082_11772.pdf, ch. 5.5 ● https://strongloop.com/strongblog/node-js-is-faster-than-java/ ● http://www.slideshare.net/RonPerlmuter/nodejs-meetup-at-palo-alto-networks-tel-aviv ● http://www.slideshare.net/ganeshiyer7/nodejs-event-driven-concurrency-for-web-applications ● http://stackoverflow.com/questions/10680601/nodejs-event-loop ● http://stackoverflow.com/questions/22644328/when-is-the-thread-pool-used ● https://www.youtube.com/watch?v=M-sc73Y-zQA