-
Enhancing Node PerformanceFramework/Node.js 2020. 2. 21. 22:41
1. Overview
2. Intuition
2.1 Blocking the Event Loop
const express = require('express'); const app = express(); function doWork(duration) { const start = Date.now(); while(Date.now() - start < duration) {} } app.get('/', (req, res) => { doWork(5000); res.send('Hi there'); }); app.listen(3000)
as soon as you and I start writing some javascript code that takes some amount of time to execute our entire server is blocking any other requests that are coming in. And it can do absolutely nothing else until that original request gets resolved and handled. And it can do absolutely nothing else until that original request gets resolved and handled.
2.2 Cluster Manager
The cluster manager is responsible for monitoring the health of each of these individual instances. So the cluster manager can start instances it can stop them you can restart them.
That is our cluster manager automatically created for us the cluster manager is then responsible for starting up worker instances in those worker instances like what you see right here or what is actually responsible for processing those incoming requests. To create these worker instances the cluster manager is going to require in the cluster module from the node standard library.
There's one particular function on that cluster module called fork and whenever we call that fork function from within the cluster manager something very interesting happens.
So when you and I call fork node internally goes back to our index dot file and it executes it a second time but it executes it that second time in a slightly different mode. The very first time it's going to produce our cluster manager and then every time after that it's going to be producing our worker instances.
2. Cluster Mode
const cluster = require('cluster'); // Is the file being executed in master mode? if (cluster.isMaster) { // Cause index.js to be executed *again* but in child mode cluster.fork() // cluster.fork() // cluster.fork() // cluster.fork() } else { // Im a child, Im going to act like a server and do nothing else const express = require('express'); const app = express(); function doWork(duration) { const start = Date.now(); while(Date.now() - start < duration) {} } app.get('/', (req, res) => { doWork(5000); res.send('Hi there'); }); app.get('/fast', (req, res) => { res.send('This was fast!') }); app.listen(3000) }
3. PM2
PM2 fully-featured clustering solution where our master tries to monitor the health of every child and maybe restart them if they ever crash or anything like that.
So this to module that we just installed is going to do really fancy things like say if one of your instances ever crashes it's going to automatically restart it for you.
And we can start up our application using PM.
// allocate all logical cores pm2 start index.js -i 0
4. Worker Threads
onmessage is a property that we are going to assign a function to. That function will then be invoked any time we call postmessage on the opposing side. In other words, if you and I call Post Message from our server it's going to invoke the function assigned to a message inside the worker.
5. Reference
'Framework > Node.js' 카테고리의 다른 글
Data Caching with Redis (0) 2020.02.22 Single Thread, Event Loop, and Blocking Code (0) 2020.02.21 The Internals of Node.js (0) 2020.02.21