SlideShare a Scribd company logo
High Performance NodeJS
by Harimurti Prasetio
Introduction
Name : Harimurti Prasetio
email : hari@nolimitid.com
twitter : http://twitter.com/harippe
facebook : https://www.facebook.com/harippe.murti
GitHub : http://github.com/aerios
Overview
What is NodeJS
How NodeJS works
High Performance in NodeJS
Case Study
What is NodeJS?
A javascript platform that run on top of V8 engine*
Non-blocking I/O
Single-threaded by nature
Originally developed by Ryan Dahl for his internal project
How NodeJS works
NodeJS use Event Loop in its core, provided by libuv library. Event Loop is
single-threaded and running indefinitely. Event Loop is responsible for :
abstracting I/O access from external request
invoke handler for I/O operation and delegates the operation to the handler
receive event from I/O handler regarding operation completion (success or
error)
trigger any callbacks associated with the event
I/O Handler
Event Loop
Main Thread
(event loop)
thread
thread
thread
thread
Event callback
Event Loop
As we can see, although NodeJS is single-threaded, but internally it still use
multithreading for I/O operation. This strategy ensure NodeJS to still able
process next request while waiting for the result from I/O operations.
Another benefit from using single-threaded environment is no memory
synchronization needed between callbacks. If two or more callbacks are
manipulating a variable, no race condition will occur. This feature, in my
personal experience, is the reason why develops application using NodeJS is
easy.
High Performance in NodeJS
In a sense, high performance means the ability to use all available resource
provided by host to deliver higher throughput
Depend on application requirement
It is harder to optimize NodeJS for CPU-intensize application than I/O-intensive application
Due to its single-threaded nature, it is almost impossible to perform parallel
programming using multithreading
almost, it means that there are several ways to use multithreading, but with limited
functionality
Case Study
Suppose our web application, built using NodeJS, need to be enhanced with
analytic functionality. After several discussion and benchmarking, it is decided
to perform data aggregation on application because performing JOIN and
GROUP on database will degrade its performance significantly.
Case Study
In the middle of the sprint, the developer bestowed with this task found that
during crunching very large dataset the application will freeze. He quickly
realize that the application stuck when it enter several tight loops, needed by
the APIs that provide analytic functionality. Changing the loops to native for
loops doesn’t make it either. So, he need to find a way so that the application
won’t freeze despite the tight loops.
Case Study
Tight loops is one case of CPU-intensive operation. Roughly there are 2 ways to
solve this problem :
1. Decrease input size
2. Increase the power
Number (1) is a no-go, because reducing the input size will produce misleading
output. So number (2) is the only option. But the question is, how do we
increase the (CPU) power when NodeJS is single-threaded by nature? How do
NodeJS application consume more CPU power explicitly from the host?
Case Study
After quick Google, there are several ways to achieve it :
1. Multiprocessing
2. WebWorker
3. Parallel.js
Multiprocessing
child_process Module
spawn
fork
exec
Multiprocess - spawn()
- enable NodeJS to create child process from another command
- suppose we need to run python script from our NodeJS application
- we can invoke the script via NodeJS using spawn
- syntax :
var spawn = require(“child_process”).spawn
var inst =
spawn(“py”,[“./path/to/python_script.py”,”parameter_for_py1”,”parameter_for_py2”])
Multiprocess - fork()
- special case of child_process.spawn
- enable NodeJS application to run another NodeJS application as its child
and perform bidirectional link between them
- by using fork, the parent and children can communicate via message
passing using send() method
- syntax
Multiprocess - fork()
main.js
var fork = require(“child_process”).fork
var inst = fork(“path/to/worker.js”,[“this is argument”])
inst.on(“message”,function(message){
console.log(message)
})
inst.send({data:”Hi worker”})
Multiprocess - fork()
worker.js
var argFromParent = process.argv[2]
process.on(“message”,function(message){
process.send({data : message.data,”response”:argFromParent})
})
Multiprocess - fork()
The result
{“data”:”Hi worker”,”response”:”this is argument”}
Multiprocess - fork()
By using fork(), the main application and its children can communicate back
and forth. This is the simplest form of message passing, a method for memory
sharing between different processes or actors. Using message passing :
main application can send data or command to its children
workers (child processes) can send back the output of calculation or
command execution
This feature enables developers to create simple job queue system using
purely NodeJS
Multiprocess - exec()
- NodeJS spawn a shell and execute the command within the shell
- any output or error is buffered and will be provided via callback
- useful to call bash command
- syntax :
var exec = require(“child_process”).exec
var inst = exec(“ls -lah ~/”,function(error,output,error){console.log(output)})
Multithreading in NodeJS
By default, multithreading is not supported in NodeJS. But some npm modules,
such as webworker-threadsand parallel.js enable developer to create new
thread. Both of these modules use WebWorker API, one of ES5 specification.
Multithreading in NodeJS
From my experience, using threads provided via WebWorker have some
benefits :
Lower resource overhead than multiprocess for initialization
Passing data back and forth incur lower overhead
The drawback for using threads:
Because the threads created using WebWorker are not native NodeJS thread,
it is not guaranted that several features provided by NodeJS is present
Demo
available at : https://github.com/aerios/bdd-3
Conclusion
NodeJS provide several ways to achieve high performance using parallel
programming
Developers can select tools provided natively by NodeJS or using
community-provided modules
In the end, high performance is not a problem that can be solved by simply
using tools. Understanding how things works and performing iterative
benchmarking is a must.
Reference
http://mcgill-csus.github.io/student_projects/Submission2.pdf
http://www.journaldev.com/7462/node-js-processing-model-single-threaded-
model-with-event-loop-architecture
http://nikhilm.github.io/uvbook/An%20Introduction%20to%20libuv.pdf
https://nikhilm.github.io/uvbook/threads.html
http://docs.libuv.org/en/v1.x/design.html
https://www.npmjs.com/package/webworker-threads
Thank You

More Related Content

What's hot

The complete-beginners-guide-to-react dyrr
The complete-beginners-guide-to-react dyrrThe complete-beginners-guide-to-react dyrr
The complete-beginners-guide-to-react dyrr
AfreenK
 
From zero to hero with React Native!
From zero to hero with React Native!From zero to hero with React Native!
From zero to hero with React Native!
Commit University
 
Dropwizard and Friends
Dropwizard and FriendsDropwizard and Friends
Dropwizard and Friends
Yun Zhi Lin
 
Continuous integration using jenkins
Continuous integration using jenkinsContinuous integration using jenkins
Continuous integration using jenkins
Vinay H G
 
Game of Streams: How to Tame and Get the Most from Your Messaging Platforms
Game of Streams: How to Tame and Get the Most from Your Messaging PlatformsGame of Streams: How to Tame and Get the Most from Your Messaging Platforms
Game of Streams: How to Tame and Get the Most from Your Messaging Platforms
VMware Tanzu
 
Spring Boot and Microservices
Spring Boot and MicroservicesSpring Boot and Microservices
Spring Boot and Microservices
seges
 
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
🎤 Hanno Embregts 🎸
 
Workflow automation for Front-end web applications
Workflow automation for Front-end web applicationsWorkflow automation for Front-end web applications
Workflow automation for Front-end web applications
Mayank Patel
 
Java Development EcoSystem
Java Development EcoSystemJava Development EcoSystem
Java Development EcoSystemAlex Tumanoff
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
Maurice De Beijer [MVP]
 
Spring boot
Spring bootSpring boot
Spring boot
Bhagwat Kumar
 
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
Puppet
 
If Hemingway Wrote JavaDocs
If Hemingway Wrote JavaDocsIf Hemingway Wrote JavaDocs
If Hemingway Wrote JavaDocs
VMware Tanzu
 
Jenkins 101: Getting Started
Jenkins 101: Getting StartedJenkins 101: Getting Started
Jenkins 101: Getting Started
R Geoffrey Avery
 
Exploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & BeyondExploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & Beyond
Kaushal Dhruw
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJS
Cere Labs Pvt. Ltd
 
React nativebeginner1
React nativebeginner1React nativebeginner1
React nativebeginner1
Oswald Campesato
 
The Past Year in Spring for Apache Geode
The Past Year in Spring for Apache GeodeThe Past Year in Spring for Apache Geode
The Past Year in Spring for Apache Geode
VMware Tanzu
 
Java(ee) mongo db applications in the cloud
Java(ee) mongo db applications in the cloud Java(ee) mongo db applications in the cloud
Java(ee) mongo db applications in the cloud Shekhar Gulati
 

What's hot (20)

The complete-beginners-guide-to-react dyrr
The complete-beginners-guide-to-react dyrrThe complete-beginners-guide-to-react dyrr
The complete-beginners-guide-to-react dyrr
 
From zero to hero with React Native!
From zero to hero with React Native!From zero to hero with React Native!
From zero to hero with React Native!
 
Dropwizard and Friends
Dropwizard and FriendsDropwizard and Friends
Dropwizard and Friends
 
Continuous integration using jenkins
Continuous integration using jenkinsContinuous integration using jenkins
Continuous integration using jenkins
 
Game of Streams: How to Tame and Get the Most from Your Messaging Platforms
Game of Streams: How to Tame and Get the Most from Your Messaging PlatformsGame of Streams: How to Tame and Get the Most from Your Messaging Platforms
Game of Streams: How to Tame and Get the Most from Your Messaging Platforms
 
Spring Boot and Microservices
Spring Boot and MicroservicesSpring Boot and Microservices
Spring Boot and Microservices
 
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)Building a Spring Boot Application - Ask the Audience!  (from JavaLand 2017)
Building a Spring Boot Application - Ask the Audience! (from JavaLand 2017)
 
Workflow automation for Front-end web applications
Workflow automation for Front-end web applicationsWorkflow automation for Front-end web applications
Workflow automation for Front-end web applications
 
Java Development EcoSystem
Java Development EcoSystemJava Development EcoSystem
Java Development EcoSystem
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
 
Spring boot
Spring bootSpring boot
Spring boot
 
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
 
If Hemingway Wrote JavaDocs
If Hemingway Wrote JavaDocsIf Hemingway Wrote JavaDocs
If Hemingway Wrote JavaDocs
 
Jenkins 101: Getting Started
Jenkins 101: Getting StartedJenkins 101: Getting Started
Jenkins 101: Getting Started
 
Exploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & BeyondExploring the power of Gradle in android studio - Basics & Beyond
Exploring the power of Gradle in android studio - Basics & Beyond
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJS
 
React nativebeginner1
React nativebeginner1React nativebeginner1
React nativebeginner1
 
The Past Year in Spring for Apache Geode
The Past Year in Spring for Apache GeodeThe Past Year in Spring for Apache Geode
The Past Year in Spring for Apache Geode
 
Jenkins
JenkinsJenkins
Jenkins
 
Java(ee) mongo db applications in the cloud
Java(ee) mongo db applications in the cloud Java(ee) mongo db applications in the cloud
Java(ee) mongo db applications in the cloud
 

Viewers also liked

Lib uv node.js
Lib uv node.js Lib uv node.js
Lib uv node.js
Ben Crox
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
Bedis ElAchèche
 
NodeJS Concurrency
NodeJS ConcurrencyNodeJS Concurrency
NodeJS Concurrency
pgriess
 
Introduce native client
Introduce native clientIntroduce native client
Introduce native client
Young-Ho Cha
 
Nodejs Applications in Production
Nodejs Applications in ProductionNodejs Applications in Production
Nodejs Applications in Production
Hamidreza Soleimani
 
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, DhakaJavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
Nurul Ferdous
 
ID Android TechTalk Series #6 : Google Service and Gradle - Andrew Kurniadi
ID Android TechTalk Series #6 : Google Service and Gradle - Andrew KurniadiID Android TechTalk Series #6 : Google Service and Gradle - Andrew Kurniadi
ID Android TechTalk Series #6 : Google Service and Gradle - Andrew Kurniadi
Dicoding
 
How to scale and deploy NodeJS app
How to scale and deploy NodeJS appHow to scale and deploy NodeJS app
How to scale and deploy NodeJS appYacobus Reinhart
 
Bringing The Sexy Back To WebWorkers
Bringing The Sexy Back To WebWorkersBringing The Sexy Back To WebWorkers
Bringing The Sexy Back To WebWorkers
Corey Clark, Ph.D.
 
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
Valeri Karpov
 
Nodejs introduce - using Socket.io
Nodejs introduce - using Socket.ioNodejs introduce - using Socket.io
Nodejs introduce - using Socket.ioCaesar Chi
 
ID Developer Elite
ID Developer EliteID Developer Elite
ID Developer Elite
Dicoding
 
Talkshow - Android N & I/O Update
Talkshow - Android N & I/O UpdateTalkshow - Android N & I/O Update
Talkshow - Android N & I/O Update
Dicoding
 
ID Android TechTalk Series #6 : Google Service and Gradle - Ibnu Sina Wardy
ID Android TechTalk Series #6 : Google Service and Gradle - Ibnu Sina WardyID Android TechTalk Series #6 : Google Service and Gradle - Ibnu Sina Wardy
ID Android TechTalk Series #6 : Google Service and Gradle - Ibnu Sina Wardy
Dicoding
 
I/O Extended (GDG Bogor) - Narenda Wicaksono
I/O Extended (GDG Bogor) - Narenda WicaksonoI/O Extended (GDG Bogor) - Narenda Wicaksono
I/O Extended (GDG Bogor) - Narenda Wicaksono
Dicoding
 
Sidiq Permana - Building For The Next Billion Users
Sidiq Permana - Building For The Next Billion UsersSidiq Permana - Building For The Next Billion Users
Sidiq Permana - Building For The Next Billion Users
Dicoding
 
Java script at backend nodejs
Java script at backend   nodejsJava script at backend   nodejs
Java script at backend nodejs
Amit Thakkar
 
Menjadi Tuan Rumah di Negeri Sendiri - Fauzil Hamdi (CEO The Wali)
Menjadi Tuan Rumah di Negeri Sendiri - Fauzil Hamdi (CEO The Wali)Menjadi Tuan Rumah di Negeri Sendiri - Fauzil Hamdi (CEO The Wali)
Menjadi Tuan Rumah di Negeri Sendiri - Fauzil Hamdi (CEO The Wali)
Dicoding
 
Produktif dalam Membangun Game - Adam Ardisasmita (Arsakids)
Produktif dalam Membangun Game - Adam Ardisasmita (Arsakids)Produktif dalam Membangun Game - Adam Ardisasmita (Arsakids)
Produktif dalam Membangun Game - Adam Ardisasmita (Arsakids)
Dicoding
 

Viewers also liked (20)

Lib uv node.js
Lib uv node.js Lib uv node.js
Lib uv node.js
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
NodeJS Concurrency
NodeJS ConcurrencyNodeJS Concurrency
NodeJS Concurrency
 
Introduce native client
Introduce native clientIntroduce native client
Introduce native client
 
Nodejs Applications in Production
Nodejs Applications in ProductionNodejs Applications in Production
Nodejs Applications in Production
 
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, DhakaJavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
 
NodeJS
NodeJSNodeJS
NodeJS
 
ID Android TechTalk Series #6 : Google Service and Gradle - Andrew Kurniadi
ID Android TechTalk Series #6 : Google Service and Gradle - Andrew KurniadiID Android TechTalk Series #6 : Google Service and Gradle - Andrew Kurniadi
ID Android TechTalk Series #6 : Google Service and Gradle - Andrew Kurniadi
 
How to scale and deploy NodeJS app
How to scale and deploy NodeJS appHow to scale and deploy NodeJS app
How to scale and deploy NodeJS app
 
Bringing The Sexy Back To WebWorkers
Bringing The Sexy Back To WebWorkersBringing The Sexy Back To WebWorkers
Bringing The Sexy Back To WebWorkers
 
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
 
Nodejs introduce - using Socket.io
Nodejs introduce - using Socket.ioNodejs introduce - using Socket.io
Nodejs introduce - using Socket.io
 
ID Developer Elite
ID Developer EliteID Developer Elite
ID Developer Elite
 
Talkshow - Android N & I/O Update
Talkshow - Android N & I/O UpdateTalkshow - Android N & I/O Update
Talkshow - Android N & I/O Update
 
ID Android TechTalk Series #6 : Google Service and Gradle - Ibnu Sina Wardy
ID Android TechTalk Series #6 : Google Service and Gradle - Ibnu Sina WardyID Android TechTalk Series #6 : Google Service and Gradle - Ibnu Sina Wardy
ID Android TechTalk Series #6 : Google Service and Gradle - Ibnu Sina Wardy
 
I/O Extended (GDG Bogor) - Narenda Wicaksono
I/O Extended (GDG Bogor) - Narenda WicaksonoI/O Extended (GDG Bogor) - Narenda Wicaksono
I/O Extended (GDG Bogor) - Narenda Wicaksono
 
Sidiq Permana - Building For The Next Billion Users
Sidiq Permana - Building For The Next Billion UsersSidiq Permana - Building For The Next Billion Users
Sidiq Permana - Building For The Next Billion Users
 
Java script at backend nodejs
Java script at backend   nodejsJava script at backend   nodejs
Java script at backend nodejs
 
Menjadi Tuan Rumah di Negeri Sendiri - Fauzil Hamdi (CEO The Wali)
Menjadi Tuan Rumah di Negeri Sendiri - Fauzil Hamdi (CEO The Wali)Menjadi Tuan Rumah di Negeri Sendiri - Fauzil Hamdi (CEO The Wali)
Menjadi Tuan Rumah di Negeri Sendiri - Fauzil Hamdi (CEO The Wali)
 
Produktif dalam Membangun Game - Adam Ardisasmita (Arsakids)
Produktif dalam Membangun Game - Adam Ardisasmita (Arsakids)Produktif dalam Membangun Game - Adam Ardisasmita (Arsakids)
Produktif dalam Membangun Game - Adam Ardisasmita (Arsakids)
 

Similar to High Performance NodeJS

Top 30 Node.js interview questions
Top 30 Node.js interview questionsTop 30 Node.js interview questions
Top 30 Node.js interview questions
techievarsity
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Vikash Singh
 
Building Applications With the MEAN Stack
Building Applications With the MEAN StackBuilding Applications With the MEAN Stack
Building Applications With the MEAN Stack
Nir Noy
 
Introduction to Node.JS
Introduction to Node.JSIntroduction to Node.JS
Introduction to Node.JS
Collaboration Technologies
 
Introduction to node.js By Ahmed Assaf
Introduction to node.js  By Ahmed AssafIntroduction to node.js  By Ahmed Assaf
Introduction to node.js By Ahmed Assaf
Ahmed Assaf
 
Plugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGemsPlugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGems
Sadayuki Furuhashi
 
Day in a life of a node.js developer
Day in a life of a node.js developerDay in a life of a node.js developer
Day in a life of a node.js developerEdureka!
 
Day In A Life Of A Node.js Developer
Day In A Life Of A Node.js DeveloperDay In A Life Of A Node.js Developer
Day In A Life Of A Node.js Developer
Edureka!
 
Kalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect GuideKalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect Guide
Kalp Corporate
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN StackRob Davarnia
 
NodeJS
NodeJSNodeJS
NodeJS
LinkMe Srl
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginners
Enoch Joshua
 
NodeJS : Communication and Round Robin Way
NodeJS : Communication and Round Robin WayNodeJS : Communication and Round Robin Way
NodeJS : Communication and Round Robin Way
Edureka!
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
Nicole Gomez
 
node.js.pptx
node.js.pptxnode.js.pptx
node.js.pptx
rani marri
 
node.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoonnode.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoon
Jesang Yoon
 

Similar to High Performance NodeJS (20)

Top 30 Node.js interview questions
Top 30 Node.js interview questionsTop 30 Node.js interview questions
Top 30 Node.js interview questions
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Building Applications With the MEAN Stack
Building Applications With the MEAN StackBuilding Applications With the MEAN Stack
Building Applications With the MEAN Stack
 
Introduction to Node.JS
Introduction to Node.JSIntroduction to Node.JS
Introduction to Node.JS
 
Introduction to node.js By Ahmed Assaf
Introduction to node.js  By Ahmed AssafIntroduction to node.js  By Ahmed Assaf
Introduction to node.js By Ahmed Assaf
 
Plugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGemsPlugin-based software design with Ruby and RubyGems
Plugin-based software design with Ruby and RubyGems
 
Day in a life of a node.js developer
Day in a life of a node.js developerDay in a life of a node.js developer
Day in a life of a node.js developer
 
Day In A Life Of A Node.js Developer
Day In A Life Of A Node.js DeveloperDay In A Life Of A Node.js Developer
Day In A Life Of A Node.js Developer
 
Kalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect GuideKalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect Guide
 
Beginning MEAN Stack
Beginning MEAN StackBeginning MEAN Stack
Beginning MEAN Stack
 
NodeJS
NodeJSNodeJS
NodeJS
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginners
 
Node js
Node jsNode js
Node js
 
NodeJS : Communication and Round Robin Way
NodeJS : Communication and Round Robin WayNodeJS : Communication and Round Robin Way
NodeJS : Communication and Round Robin Way
 
Nt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language AnalysisNt1310 Unit 3 Language Analysis
Nt1310 Unit 3 Language Analysis
 
node.js.pptx
node.js.pptxnode.js.pptx
node.js.pptx
 
node.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoonnode.js 실무 - node js in practice by Jesang Yoon
node.js 실무 - node js in practice by Jesang Yoon
 
BPMS1
BPMS1BPMS1
BPMS1
 
BPMS1
BPMS1BPMS1
BPMS1
 
Express node js
Express node jsExpress node js
Express node js
 

More from Dicoding

I/O Extended (GDG Bogor) - Andrew Kurniadi
I/O Extended (GDG Bogor) - Andrew KurniadiI/O Extended (GDG Bogor) - Andrew Kurniadi
I/O Extended (GDG Bogor) - Andrew Kurniadi
Dicoding
 
Mencari Genre Game yang Sesuai Passion - Cipto Adiguno (Ekuator Games)
Mencari Genre Game yang Sesuai Passion - Cipto Adiguno (Ekuator Games)Mencari Genre Game yang Sesuai Passion - Cipto Adiguno (Ekuator Games)
Mencari Genre Game yang Sesuai Passion - Cipto Adiguno (Ekuator Games)
Dicoding
 
Continuous Integration & Continuous Delivery on Android - Nur Rendra Toro Sin...
Continuous Integration & Continuous Delivery on Android - Nur Rendra Toro Sin...Continuous Integration & Continuous Delivery on Android - Nur Rendra Toro Sin...
Continuous Integration & Continuous Delivery on Android - Nur Rendra Toro Sin...
Dicoding
 
Rendra Toro - Model View Presenter
Rendra Toro - Model View PresenterRendra Toro - Model View Presenter
Rendra Toro - Model View Presenter
Dicoding
 
Yoza Aprilio - We must design
Yoza Aprilio - We must designYoza Aprilio - We must design
Yoza Aprilio - We must design
Dicoding
 
Agus Hamonangan - Sejarah Android, Penetrasi/Pertumbungan, dan Peluang Smartp...
Agus Hamonangan - Sejarah Android, Penetrasi/Pertumbungan, dan Peluang Smartp...Agus Hamonangan - Sejarah Android, Penetrasi/Pertumbungan, dan Peluang Smartp...
Agus Hamonangan - Sejarah Android, Penetrasi/Pertumbungan, dan Peluang Smartp...
Dicoding
 
ID Android Dev Talk - Observer Pattern, Event Bus Usage, Firebase & Geofire
ID Android Dev Talk  - Observer Pattern, Event Bus Usage, Firebase & GeofireID Android Dev Talk  - Observer Pattern, Event Bus Usage, Firebase & Geofire
ID Android Dev Talk - Observer Pattern, Event Bus Usage, Firebase & Geofire
Dicoding
 

More from Dicoding (7)

I/O Extended (GDG Bogor) - Andrew Kurniadi
I/O Extended (GDG Bogor) - Andrew KurniadiI/O Extended (GDG Bogor) - Andrew Kurniadi
I/O Extended (GDG Bogor) - Andrew Kurniadi
 
Mencari Genre Game yang Sesuai Passion - Cipto Adiguno (Ekuator Games)
Mencari Genre Game yang Sesuai Passion - Cipto Adiguno (Ekuator Games)Mencari Genre Game yang Sesuai Passion - Cipto Adiguno (Ekuator Games)
Mencari Genre Game yang Sesuai Passion - Cipto Adiguno (Ekuator Games)
 
Continuous Integration & Continuous Delivery on Android - Nur Rendra Toro Sin...
Continuous Integration & Continuous Delivery on Android - Nur Rendra Toro Sin...Continuous Integration & Continuous Delivery on Android - Nur Rendra Toro Sin...
Continuous Integration & Continuous Delivery on Android - Nur Rendra Toro Sin...
 
Rendra Toro - Model View Presenter
Rendra Toro - Model View PresenterRendra Toro - Model View Presenter
Rendra Toro - Model View Presenter
 
Yoza Aprilio - We must design
Yoza Aprilio - We must designYoza Aprilio - We must design
Yoza Aprilio - We must design
 
Agus Hamonangan - Sejarah Android, Penetrasi/Pertumbungan, dan Peluang Smartp...
Agus Hamonangan - Sejarah Android, Penetrasi/Pertumbungan, dan Peluang Smartp...Agus Hamonangan - Sejarah Android, Penetrasi/Pertumbungan, dan Peluang Smartp...
Agus Hamonangan - Sejarah Android, Penetrasi/Pertumbungan, dan Peluang Smartp...
 
ID Android Dev Talk - Observer Pattern, Event Bus Usage, Firebase & Geofire
ID Android Dev Talk  - Observer Pattern, Event Bus Usage, Firebase & GeofireID Android Dev Talk  - Observer Pattern, Event Bus Usage, Firebase & Geofire
ID Android Dev Talk - Observer Pattern, Event Bus Usage, Firebase & Geofire
 

Recently uploaded

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 

Recently uploaded (20)

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 

High Performance NodeJS

  • 1. High Performance NodeJS by Harimurti Prasetio
  • 2. Introduction Name : Harimurti Prasetio email : hari@nolimitid.com twitter : http://twitter.com/harippe facebook : https://www.facebook.com/harippe.murti GitHub : http://github.com/aerios
  • 3. Overview What is NodeJS How NodeJS works High Performance in NodeJS Case Study
  • 4. What is NodeJS? A javascript platform that run on top of V8 engine* Non-blocking I/O Single-threaded by nature Originally developed by Ryan Dahl for his internal project
  • 5. How NodeJS works NodeJS use Event Loop in its core, provided by libuv library. Event Loop is single-threaded and running indefinitely. Event Loop is responsible for : abstracting I/O access from external request invoke handler for I/O operation and delegates the operation to the handler receive event from I/O handler regarding operation completion (success or error) trigger any callbacks associated with the event
  • 6. I/O Handler Event Loop Main Thread (event loop) thread thread thread thread Event callback
  • 7. Event Loop As we can see, although NodeJS is single-threaded, but internally it still use multithreading for I/O operation. This strategy ensure NodeJS to still able process next request while waiting for the result from I/O operations. Another benefit from using single-threaded environment is no memory synchronization needed between callbacks. If two or more callbacks are manipulating a variable, no race condition will occur. This feature, in my personal experience, is the reason why develops application using NodeJS is easy.
  • 8. High Performance in NodeJS In a sense, high performance means the ability to use all available resource provided by host to deliver higher throughput Depend on application requirement It is harder to optimize NodeJS for CPU-intensize application than I/O-intensive application Due to its single-threaded nature, it is almost impossible to perform parallel programming using multithreading almost, it means that there are several ways to use multithreading, but with limited functionality
  • 9. Case Study Suppose our web application, built using NodeJS, need to be enhanced with analytic functionality. After several discussion and benchmarking, it is decided to perform data aggregation on application because performing JOIN and GROUP on database will degrade its performance significantly.
  • 10. Case Study In the middle of the sprint, the developer bestowed with this task found that during crunching very large dataset the application will freeze. He quickly realize that the application stuck when it enter several tight loops, needed by the APIs that provide analytic functionality. Changing the loops to native for loops doesn’t make it either. So, he need to find a way so that the application won’t freeze despite the tight loops.
  • 11. Case Study Tight loops is one case of CPU-intensive operation. Roughly there are 2 ways to solve this problem : 1. Decrease input size 2. Increase the power Number (1) is a no-go, because reducing the input size will produce misleading output. So number (2) is the only option. But the question is, how do we increase the (CPU) power when NodeJS is single-threaded by nature? How do NodeJS application consume more CPU power explicitly from the host?
  • 12. Case Study After quick Google, there are several ways to achieve it : 1. Multiprocessing 2. WebWorker 3. Parallel.js
  • 14. Multiprocess - spawn() - enable NodeJS to create child process from another command - suppose we need to run python script from our NodeJS application - we can invoke the script via NodeJS using spawn - syntax : var spawn = require(“child_process”).spawn var inst = spawn(“py”,[“./path/to/python_script.py”,”parameter_for_py1”,”parameter_for_py2”])
  • 15. Multiprocess - fork() - special case of child_process.spawn - enable NodeJS application to run another NodeJS application as its child and perform bidirectional link between them - by using fork, the parent and children can communicate via message passing using send() method - syntax
  • 16. Multiprocess - fork() main.js var fork = require(“child_process”).fork var inst = fork(“path/to/worker.js”,[“this is argument”]) inst.on(“message”,function(message){ console.log(message) }) inst.send({data:”Hi worker”})
  • 17. Multiprocess - fork() worker.js var argFromParent = process.argv[2] process.on(“message”,function(message){ process.send({data : message.data,”response”:argFromParent}) })
  • 18. Multiprocess - fork() The result {“data”:”Hi worker”,”response”:”this is argument”}
  • 19. Multiprocess - fork() By using fork(), the main application and its children can communicate back and forth. This is the simplest form of message passing, a method for memory sharing between different processes or actors. Using message passing : main application can send data or command to its children workers (child processes) can send back the output of calculation or command execution This feature enables developers to create simple job queue system using purely NodeJS
  • 20. Multiprocess - exec() - NodeJS spawn a shell and execute the command within the shell - any output or error is buffered and will be provided via callback - useful to call bash command - syntax : var exec = require(“child_process”).exec var inst = exec(“ls -lah ~/”,function(error,output,error){console.log(output)})
  • 21. Multithreading in NodeJS By default, multithreading is not supported in NodeJS. But some npm modules, such as webworker-threadsand parallel.js enable developer to create new thread. Both of these modules use WebWorker API, one of ES5 specification.
  • 22. Multithreading in NodeJS From my experience, using threads provided via WebWorker have some benefits : Lower resource overhead than multiprocess for initialization Passing data back and forth incur lower overhead The drawback for using threads: Because the threads created using WebWorker are not native NodeJS thread, it is not guaranted that several features provided by NodeJS is present
  • 23. Demo available at : https://github.com/aerios/bdd-3
  • 24. Conclusion NodeJS provide several ways to achieve high performance using parallel programming Developers can select tools provided natively by NodeJS or using community-provided modules In the end, high performance is not a problem that can be solved by simply using tools. Understanding how things works and performing iterative benchmarking is a must.