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

Node.js concurrency

  • 1.
    Node.js concurrency Giacomo Fornari Universityof Padua, Sistemi concorrenti e distribuiti, 2016 - 2017
  • 2.
  • 3.
    Two theses 1. waitingan 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 ● Highlyconcurrent ● 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 StandardLibrary V8 Thread Pool Event Loop JavaScript C 5
  • 6.
    Event Loop ● Hasa 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 EventLoop Thread 1 Thread 2 Thread n … (function, callback) Task 1 Task 2 Task 3 Return 1 Task 4 Callback 1 7
  • 8.
    Thread Pool ● Thestrategy 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 functionwith 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 andThread Pool diagram 10https://strongloop.com/strongblog/node-js-is-faster-than-java/
  • 11.
    return vs. callback functionadd(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 reasoningeasy ● 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