Node.js threads for
I/O-bound tasks
Node.js performance measurement APIs
● perf_hooks
eventLoopUtilization
Added in: v14.10.0, v12.19.0
● worker.performance
eventLoopUtilization
Added in: v15.1.0, v14.17.0, v12.22.0
Node.js performance measurement APIs
eventLoopUtilization([elu1[, elu1]]): elu;
interface EventLoopUtilization {
idle: number;
active: number;
utilization: number;
}
Node.js I/O concurrency model
wait
Node.js I/O concurrency model
wait
wait
Node.js I/O concurrency model
wait
wait
wait
Node.js I/O concurrency model
wait
wait
wait
wait
Node.js I/O concurrency model
wait
wait
wait
wait
Node.js I/O concurrency model
wait
wait
wait
wait
Node.js I/O concurrency model
wait
wait
wait
wait
Node.js I/O concurrency model
wait
wait
wait
wait
Node.js I/O concurrency model
wait
wait
wait
wait
Node.js I/O concurrency model
wait
wait
wait
wait
idle profit
delay
Node.js Event Loop
Node.js performance measurement APIs
● perf_hooks
eventLoopUtilization
Added in: v14.10.0, v12.19.0
● worker.performance
eventLoopUtilization
Added in: v15.1.0, v14.17.0, v12.22.0
Node.js ELU bugs
● Separation of concerns in ELU
current ELU, current - previous, difference
● Code duplication
Proportion in eventLoopUtilization = 3
2 versions of eventLoopUtilization * 3 = 6
● Negative utilization
https://github.com/nodejs/node/issues/40525
Node.js I/O concurrency model
Main
thread
Node.js I/O concurrency model
Main
thread
Node.js I/O concurrency model
Main
thread
Node.js I/O concurrency model
Main
thread
Node.js I/O concurrency model
Main
thread
Worker
Thread
Worker
Thread
Node.js I/O concurrency model
Main
thread
Worker
Thread
Worker
Thread
Node.js I/O concurrency model
Main
thread
Worker
Thread
Worker
Thread
Worker
Thread
Node.js I/O concurrency model
Main
thread
Worker
Thread
Worker
Thread
Worker
Thread
Worker
Thread
Node.js I/O concurrency model
Main
thread
Worker
Thread
Worker
Thread
Worker
Thread
Worker
Thread
Worker
Thread
Node.js I/O concurrency model
Main
thread
Worker
Thread
Worker
Thread
Worker
Thread
Worker
Thread
Worker
Thread
Node.js I/O concurrency model
Main
thread
Worker
Thread
Worker
Thread
Worker
Thread
Node.js I/O concurrency model
Main
thread
Thread
Pool
Task
Balancer
Node.js I/O concurrency model
Main
thread
Thread
Pool
Task Balancer
Node.js I/O concurrency model
Main
thread
Thread
Pool
Task Balancer
module 1
module 2
...
module M
Node.js I/O concurrency model
Main
thread
Thread
Pool
Task Balancer
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
Node.js I/O concurrency model
Main
thread
Thread
Pool
Task Balancer
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
Node.js I/O concurrency model
Main
thread
Thread
Pool
Task Balancer
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
Prototype: noroutine
const noroutine = require('noroutine');
const module1 = require('./module1.js');
const module2 = require('./module2.js);
noroutine.init({ modules: [module1, module2] });
(async () => {
const res1 = await module1.method1('value1');
const res2 = await module2.method2('value2');
console.log({ res1, res2 });
})();
Configure noroutine
noroutine.init({
modules: [module1, module2],
pool: 5, // number of workers in thread pool
wait: 2000, // maximum delay to wait for a free thread
timeout: 5000, // maximum timeout for executing fn
monitoring: 5000, // utilization monitoring interval
});
Node.js I/O concurrency model
Main
thread
Thread
Pool
Task Balancer
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
Node.js I/O concurrency model
Main
thread
Thread
Pool
Task Balancer
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
Shared Memory
Node.js I/O concurrency model
Main
thread
Thread
Pool
Task Balancer
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
module 1
module 2
...
module M
Shared Memory
Roadmap
● Better statistics for balancing
● Improve balancing algorithms
● Crossworker data access
● Race condition protection
● Dynamic module loading in userland workers
● Load testing
Лекции
Курс по ноде
github.com/HowProgrammingWorks/Index/blob/master/Courses/NodeJS.md
Курс по асинхронщине
github.com/HowProgrammingWorks/Index/blob/master/Courses/Asynchronous.md
Все лекции
github.com/HowProgrammingWorks/Index
Доклады
Индекс докладов
github.com/HowProgrammingWorks/Index/blob/master/Courses/Talks.md
Презентация Метархии
youtu.be/PHyl4b8Fj5A
Node.js в 2021
youtu.be/nnB7ADYso8s
Ссылки
Гитхаб
github.com/tshemsedinov
Ютюб
youtube.com/c/TimurShemsedinov
Телеграм
github.com/HowProgrammingWorks/Index/blob/master/Links.md

Node.js threads for I/O-bound tasks