SlideShare a Scribd company logo
Globalcode – Open4education
Node.js Cluster
Derek Stavis
Software Engineer
Globalcode – Open4education
Who?
Derek Stavis
github.com/derekstavis
Software Engineer
@ Pagar.me
Ruby, JavaScript, Python, Go, C
Node, React & Webpack Advocate
Globalcode – Open4education
Node.js Runtime
Globalcode – Open4education
single threaded
non-blocking
asynchronous
concurrent
engine
Globalcode – Open4education
Heap
Memory Available
Task Queue
listOnTimeout
listOnTimeout
listOnTimeout
listOnTimeout
listOnTimeout
Delayed Tasks
Call Stack
write()
saveFile()
updateTime()
tryOnTimeout
listOnTimeout
Execution Contexts
Vendor APIs (Node, Web)
Runtime and Platform Abstraction
Event Loop
Task Scheduler
Globalcode – Open4education
Single threaded
Globalcode – Open4education
Isn’t concurrent
sufficient?
Globalcode – Open4education
We have multiple
processors for a while
Globalcode – Open4education
We want to distribute
the load
Globalcode – Open4education
Enter cluster module
Globalcode – Open4education
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length
How to
Globalcode – Open4education
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
}
How to
Globalcode – Open4education
else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello worldn');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
How to
Globalcode – Open4education
Globalcode – Open4education
What?
Globalcode – Open4education
Two processes binding
on the same port?
Globalcode – Open4education
cluster is just a wrapper
for child_process.fork
Globalcode – Open4education
Socket.listen is aware
of cluster module
Globalcode – Open4education
When a “slave” process
uses a Socket
Globalcode – Open4education
The underlying
implementation listens
on a file descriptor
Globalcode – Open4education
That is fed by the
master process
Globalcode – Open4education
Master process acts like a
load balancer
Globalcode – Open4education
Globalcode – Open4education
Perk: You can do
clustering with anything
that is socket based
Globalcode – Open4education
Here comes the JabĂĄ
Globalcode – Open4education
TeleMMO is a
RPG game into a
Telegram bot
Globalcode – Open4education
We needed to make our
game multicore
Globalcode – Open4education
Telegram API can use
two modes: Polling and
Webhook
Globalcode – Open4education
Webhooks are based on a
HTTP server
Globalcode – Open4education
We use a third-party
module to integrate with
Telegram
Globalcode – Open4education
Cluster came in handy
Globalcode – Open4education
We just followed the
standard path
(click me)
Globalcode – Open4education
Globalcode – Open4education
What more?
Globalcode – Open4education
You can do nice things
Globalcode – Open4education
Zero downtime
code reload
Globalcode – Open4education
Process management
Globalcode – Open4education
Automatic software
update
Globalcode – Open4education
var recluster = require('recluster'),
path = require('path');
var cluster = recluster(path.join(__dirname, 'server.js'));
cluster.run();
process.on('SIGUSR2', function() {
console.log('Got SIGUSR2, reloading cluster...');
cluster.reload();
});
console.log("spawned cluster, kill -s SIGUSR2", process.pid, "to reload");
https://github.com/doxout/recluster
Globalcode – Open4education
Is it all flowers?
Globalcode – Open4education
NOPE
Globalcode – Open4education
Processes are heavy
Globalcode – Open4education
They are subject to
operating system
scheduling
Globalcode – Open4education
Context switches are
very expensive
Globalcode – Open4education
Memory usage is high
Globalcode – Open4education
Each worker reloads the
entire application into
memory
Globalcode – Open4education
Kinda hacky, huh?
Globalcode – Open4education
So, use it wisely
Globalcode – Open4education
Don’t do more
processes than the
number of CPUs
Globalcode – Open4education
Don’t use it to fix your
bad code performance
Globalcode – Open4education
Globalcode – Open4education
Thanks for watching
Questions?
github.com/derekstavis
twitter.com/derekstavis
facebook.com/derekstavis

More Related Content

What's hot (20)

PDF
Campus HTC at #TechEX15
Rob Gardner
 
PDF
React for Beginners
Derek Willian Stavis
 
PDF
Programming language for the cloud infrastructure
Yaroslav Muravskyi
 
KEY
groovy & grails - lecture 13
Alexandre Masselot
 
PDF
Asynchronous web apps with the Play Framework 2.0
Oscar Renalias
 
ODP
Trac Project And Process Management For Developers And Sys Admins Presentation
guest3fc4fa
 
PDF
DAST ĐČ CI/CD, ĐžĐ»ŃŒĐłĐ° ĐĄĐČĐžŃ€ĐžĐŽĐŸĐČа
Mail.ru Group
 
PDF
About Node.js
Artemisa Yescas Engler
 
PDF
Situated Program Challenge with Haskell & Clojure
Kent Ohashi
 
PPT
Groovy & Grails: Scripting for Modern Web Applications
rohitnayak
 
PDF
Altitude NY 2018: Programming the edge workshop
Fastly
 
KEY
Plack at OSCON 2010
Tatsuhiko Miyagawa
 
KEY
Deploying Plack Web Applications: OSCON 2011
Tatsuhiko Miyagawa
 
PPTX
Android and REST
Roman WoĆșniak
 
PDF
Composable and streamable Play apps
Yevgeniy Brikman
 
PDF
Presentation security automation (Selenium Camp)
Artyom Rozumenko
 
PDF
Contract-driven development with OpenAPI 3 and Vert.x | DevNation Tech Talk
Red Hat Developers
 
PDF
Seven perilous pitfalls to avoid with Java | DevNation Tech Talk
Red Hat Developers
 
KEY
groovy & grails - lecture 10
Alexandre Masselot
 
PPTX
Java Play Restful JPA
Faren faren
 
Campus HTC at #TechEX15
Rob Gardner
 
React for Beginners
Derek Willian Stavis
 
Programming language for the cloud infrastructure
Yaroslav Muravskyi
 
groovy & grails - lecture 13
Alexandre Masselot
 
Asynchronous web apps with the Play Framework 2.0
Oscar Renalias
 
Trac Project And Process Management For Developers And Sys Admins Presentation
guest3fc4fa
 
DAST ĐČ CI/CD, ĐžĐ»ŃŒĐłĐ° ĐĄĐČĐžŃ€ĐžĐŽĐŸĐČа
Mail.ru Group
 
About Node.js
Artemisa Yescas Engler
 
Situated Program Challenge with Haskell & Clojure
Kent Ohashi
 
Groovy & Grails: Scripting for Modern Web Applications
rohitnayak
 
Altitude NY 2018: Programming the edge workshop
Fastly
 
Plack at OSCON 2010
Tatsuhiko Miyagawa
 
Deploying Plack Web Applications: OSCON 2011
Tatsuhiko Miyagawa
 
Android and REST
Roman WoĆșniak
 
Composable and streamable Play apps
Yevgeniy Brikman
 
Presentation security automation (Selenium Camp)
Artyom Rozumenko
 
Contract-driven development with OpenAPI 3 and Vert.x | DevNation Tech Talk
Red Hat Developers
 
Seven perilous pitfalls to avoid with Java | DevNation Tech Talk
Red Hat Developers
 
groovy & grails - lecture 10
Alexandre Masselot
 
Java Play Restful JPA
Faren faren
 

Similar to Node.js cluster (20)

PDF
APIs Rest com NodeJS
Jakeliny Gracielly
 
PPTX
PHP as a Service TDC2019
Paulo Victor Gomes
 
PDF
TDC2018SP | Trilha Java - Computacao [Concorrente | Paralela | Distribuida] e...
tdc-globalcode
 
PPTX
Ensuring Software Quality in the cloud
Ricardo Martinelli de Oliveira
 
PDF
State of Big Data on ARM64 / AArch64 - Apache Bigtop
Ganesh Raju
 
PPTX
Gorush: A push notification server written in Go
Bo-Yi Wu
 
PDF
Excelian hyperledger walkthrough-feb17
Excelian | Luxoft Financial Services
 
PDF
Using Groovy to empower WebRTC Network Systems
antonry
 
PPTX
Functional as a service TDC 2020
Paulo Victor Gomes
 
PDF
Joomla Code Quality Control and Automation Testing
Shyam Sunder Verma
 
PPTX
Enabling Java: Windows on Arm64 - A Success Story!
Monica Beckwith
 
PDF
The State of the Veil Framework
VeilFramework
 
PDF
(phpconftw2012) PHP as a Middleware in Embedded Systems
sosorry
 
PPT
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
Bruno Tanoue
 
PDF
OSDC.no 2015 introduction to node.js workshop
leffen
 
PPT
OGCE Project Overview
marpierc
 
PDF
Building APIs with NodeJS on Microsoft Azure Websites - Redmond
Rick G. Garibay
 
PDF
What_s_New_in_OpenShift_Container_Platform_4.6.pdf
chalermpany
 
PPTX
Java 7 - New Features - by Mihail Stoynov and Svetlin Nakov
Svetlin Nakov
 
PDF
Programando o ESP8266 com Python
Relsi Maron
 
APIs Rest com NodeJS
Jakeliny Gracielly
 
PHP as a Service TDC2019
Paulo Victor Gomes
 
TDC2018SP | Trilha Java - Computacao [Concorrente | Paralela | Distribuida] e...
tdc-globalcode
 
Ensuring Software Quality in the cloud
Ricardo Martinelli de Oliveira
 
State of Big Data on ARM64 / AArch64 - Apache Bigtop
Ganesh Raju
 
Gorush: A push notification server written in Go
Bo-Yi Wu
 
Excelian hyperledger walkthrough-feb17
Excelian | Luxoft Financial Services
 
Using Groovy to empower WebRTC Network Systems
antonry
 
Functional as a service TDC 2020
Paulo Victor Gomes
 
Joomla Code Quality Control and Automation Testing
Shyam Sunder Verma
 
Enabling Java: Windows on Arm64 - A Success Story!
Monica Beckwith
 
The State of the Veil Framework
VeilFramework
 
(phpconftw2012) PHP as a Middleware in Embedded Systems
sosorry
 
[ENGLISH] TDC 2015 - PHP Trail - Tests and PHP Continuous Integration Enviro...
Bruno Tanoue
 
OSDC.no 2015 introduction to node.js workshop
leffen
 
OGCE Project Overview
marpierc
 
Building APIs with NodeJS on Microsoft Azure Websites - Redmond
Rick G. Garibay
 
What_s_New_in_OpenShift_Container_Platform_4.6.pdf
chalermpany
 
Java 7 - New Features - by Mihail Stoynov and Svetlin Nakov
Svetlin Nakov
 
Programando o ESP8266 com Python
Relsi Maron
 
Ad

Recently uploaded (20)

PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Why Orbit Edge Tech is a Top Next JS Development Company in 2025
mahendraalaska08
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Ad

Node.js cluster