KEMBAR78
JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP на процессах и потоках в NodeJS | PPTX
Comparing Node.js
processes and threads
for clustering
HTTP, TCP, and UDP
Vitalii Kukhar
Full Stack Software Engineer at Metarhia
8-9 NOVEMBER ‘19 KIEV, UKRAINEPROFESSIONAL JS CONFERENCE
Javascript is a single threaded language
Asynchronous I/O
● Rack: a Ruby Webserver Interface
● NGINX uses an asynchronous, event-driven approach
● an asynchronous event-driven JavaScript runtime
● JavaScript + asynchronous IO + HTTP server stuff
Performing CPU-intensive operations
● long-running JavaScript functions
● unresponsive page
● limit to perform new asynchronous I / O tasks
● bad user experience
Inter process communication (IPC)
● child_process module
● cluster module
● net module (tcp client-server)
● BroadcastChannel API, Web Storage API, etc.
● no shared memory
The worker_threads module
● enables to use threads
● execute JavaScript in parallel
● useful for performing CPU-intensive operations
● shared memory
Basic Concepts
● process
● thread
● multiprocessing
● multithreading
What the worker_threads module gave us(before)?
● one process
● one thread
● one event loop
● one JS Engine instance
● one NodeJS instance process
thread
V8 libuv
NodeJS
JavaScript
What the worker_threads module gave us(after)?
● one process
● multiple threads
● one event loop per thread
● one JS Engine Instance per thread
● one NodeJS Instance per thread process
thread
V8 libuv
NodeJS
JavaScript
thread
V8 libuv
NodeJS
JavaScript
Tools for working with multithreading
Server Clusters
● high-availability
● load-balancing
● high performance computing
Our cluster
Experimental servers
● http
● tcp
● udp
Research
● CPU usage
● memory usage
● multiple instances of NodeJS
● worker_threads with I/O-intensive work
Project structure
● transport
○ http.js
○ tcp.js
○ udp.js
● master.js
● worker.js
● config.js
● spawner
○ processes.js
○ threads.js
● transport
○ http.js
○ tcp.ja
○ udp.js
● master.js
● worker.js
● config.js
Client Server
Configuration files
{
ports: [0, …, N],
hostname:’xxx.xxx.xxx.xxx’,
transportName: 'http',
concurrentConnection: 20,
}
{
ports: [0, …, N],
hostname: '127.0.0.1',
spawnerName: 'processes',
transportName: http,
disconnect: 2
}
Client Server
HTTP Client
const requesterAsync = async port => {
while (condition) {
await requester(port);
}
};
TCP Client
const requesterAsync = port => {
socket.connect({ port, host }, () => socket.write('Run'));
socket.on('data', data => {
if (condition) {
socket.write('Run')
}
});
};
UDP Client
const requesterAsync = port => {
socket.send('Run!', port, host);
socket.on('message', (msg, info) => {
if (condition) {
socket.send('Run', info.port, info.address);
}
});
};
Server return
{
port,
workerId || processId,
count,
memoryUsage
}
Test #1
● HTTP
● Server
○ 7 processes
○ 7 threads
● Client
○ 70 connections
○ ~3600 rps on port
HTTP: CPU Usage Processes vs Threads
HTTP: CPU Usage with disconnects
Processes vs Threads
HTTP: Memory Usage Processes vs Threads
HTTP: RSS
Test #2
● TCP
● Server
○ 7 processes
○ 7 threads
● Client
○ 140 connections
○ ~24000 rps on port
TCP: CPU Usage Processes vs Threads
TCP: CPU Usage with disconnects
Processes vs Threads
TCP: Memory Usage Processes vs Threads
TCP: RSS Processes vs Threads
Test #3
● UDP
● Server
○ 7 processes
○ 7 threads
● Client
○ 140 connections
○ ~24000 rps on port
UDP: CPU Usage Processes vs Threads
UDP: CPU Usage with stop/restart
Processes vs Threads
UDP: Memory Usage
Processes vs Threads
UDP: RSS Processes vs Threads
Thank you!
Questions?
kukhar.vitalii@gmail.com
8-9 NOVEMBER ‘19 KIEV, UKRAINEPROFESSIONAL JS CONFERENCE

JS Fest 2019/Autumn. Виталий Кухар. Сравнение кластеризации HTTP, TCP и UDP на процессах и потоках в NodeJS