Minden, amit tudnia kell a Node.js beépített eszközeiről a méretezhetőség érdekében
Frissítés: Ez a cikk most a „Node.js Beyond The Basics” című könyvem része. Olvassa el a tartalom frissített verzióját és többet a Node-ról a jscomplete.com/node-beyond-basics címen .A Node.js méretezhetősége nem utólagos. Ez olyasmi, ami a futás magjába sül. A Node nevet Node-nak hívják, hogy hangsúlyozza azt az elképzelést, hogy egy Node alkalmazásnak több kis elosztott csomópontból kell állnia, amelyek egymással kommunikálnak.
Több csomópontot futtat a Node alkalmazásokhoz? Node-folyamatot futtat a termelőgépek minden CPU-magján, és a terhelések kiegyenlítik az összes kérést közöttük? Tudta, hogy a Node rendelkezik beépített modullal, amely segít ebben?
A Node fürtmodulja nemcsak a gép teljes CPU-teljesítményének kihasználását kínálja a dobozból, hanem segít a Node-folyamatok elérhetőségének növelésében, és lehetőséget nyújt az egész alkalmazás újraindítására nulla leállással . Ez a cikk minden jóságot és még sok minden mást tartalmaz.
Ez a cikk a Node.js-ről szóló Pluralsight tanfolyamom egy része. Én hasonló tartalmat fedek le videó formátumban ott.A méretezhetőség stratégiái
A munkaterhelés a legnépszerűbb ok, ami miatt méretezzük az alkalmazásokat, de nem ez az egyetlen ok. Alkalmazásainkat is méretezzük, hogy növeljük elérhetőségüket és a hibatűrést.
Főként három különböző dolgot tehetünk az alkalmazások méretezéséhez:
1 - Klónozás
A nagy alkalmazások méretezéséhez a legegyszerűbb az, ha többször klónozza, és mindegyik klónozott példány kezeli a munkaterhelés egy részét (például terheléselosztóval). Ez nem kerül sokba a fejlesztési idő szempontjából, és rendkívül hatékony. Ez a stratégia a minimum, amit meg kell tennie, és a Node.js rendelkezik a beépített modullal, cluster
hogy megkönnyítse a klónozási stratégia egyetlen kiszolgálón történő megvalósítását.
2 - Bomlik
Méretezhetünk egy alkalmazást úgy is, hogy a funkciók és szolgáltatások alapján lebontjuk. Ez azt jelenti, hogy több, különböző alkalmazás van különböző kódbázissal, és néha saját dedikált adatbázisokkal és felhasználói interfészekkel.
Ez a stratégia általában a Microservice kifejezéshez kapcsolódik , ahol a mikro jelzi, hogy ezeknek a szolgáltatásoknak a lehető legkisebbnek kell lenniük, de a valóságban nem a szolgáltatás mérete a fontos, hanem a laza összekapcsolás és a szolgáltatások közötti magas kohézió érvényesítése. Ennek a stratégiának a végrehajtása gyakran nem könnyű, és hosszú távon váratlan problémákat okozhat, de ha jól végezzük, akkor az előnyök nagyok.
3 - Hasadás
Az alkalmazást több példányra is fel tudjuk osztani, ahol minden példány az alkalmazás adatainak csak egy részéért felel. Ezt a stratégiát gyakran vízszintes particionálásnak vagy szilánkosításnak nevezik az adatbázisokban. Az adatok particionálása minden művelet előtt megkeresési lépést igényel annak meghatározásához, hogy az alkalmazás melyik példányát használja. Például talán fel akarjuk osztani a felhasználókat országuk vagy nyelvük alapján. Először meg kell keresnünk ezeket az információkat.
Egy nagy alkalmazás sikeres méretezéséhez végül végre kell hajtani mindhárom stratégiát. A Node.js megkönnyíti ezt, de a cikk klónozási stratégiájára fogok összpontosítani, és áttekintem a Node.js-ben elérhető beépített eszközöket annak megvalósításához.
Kérjük, vegye figyelembe, hogy a cikk elolvasása előtt jól ismernie kell a Node.js gyermekfolyamatokat . Ha még nem tette meg, javasoljuk, hogy először olvassa el ezt a másik cikket:
Node.js gyermekfolyamatok: Minden, amit tudnia kell
A spawn (), az exec (), az execFile () és a fork () használata a medium.freecodecamp.org
A fürt modul
A fürt modul használható a terheléselosztás engedélyezésére a környezet több CPU magján keresztül. Ez a gyermekfolyamat-modul fork
módszerén alapul, és alapvetően lehetővé teszi számunkra, hogy a fő alkalmazás folyamatát annyiszor elágazzuk, ahány CPU-magunk van. Ezután átveszi és betölti az összes kérelmet a fő folyamathoz az összes elágazó folyamatban.
A klaszter modul a Node segítője számunkra a klónozási skálázhatósági stratégia megvalósításához, de csak egy gépen. Ha nagy gépe van, sok erőforrással, vagy ha könnyebb és olcsóbb új erőforrásokat hozzáadni egy géphez, mint új gépeket hozzáadni, akkor a fürt modul nagyszerű lehetőség a klónozási stratégia igazán gyors megvalósításához.
Még a kis gépek is általában több maggal rendelkeznek, és még akkor is, ha nem aggódik a Node-kiszolgáló terhelése miatt, a kiszolgáló elérhetőségének és hibatűrésének növelése érdekében mindenképpen engedélyeznie kell a fürt modult. Ez egy egyszerű lépés, és ha például egy olyan folyamatkezelőt használunk, mint a PM2, akkor ez olyan egyszerűvé válik, mint csak argumentumot adni az indítási parancsnak!
De hadd mondjam el, hogyan kell natívan használni a fürt modult, és magyarázzam el, hogyan működik.
A klaszter modul felépítése egyszerű. Létrehozunk egy fő folyamatot, és ez a fő folyamat számos munkavállalói folyamatot összekapcsol és kezel. Minden dolgozói folyamat az alkalmazás egy példányát képviseli, amelyet méretezni akarunk. Az összes beérkező kérést a fő folyamat kezeli, amely dönti el, hogy melyik munkavállalói folyamat kezelje a bejövő kéréseket.

A fő folyamat feladata egyszerű, mert valójában csak egy körbefutó algoritmust használ a dolgozói folyamat kiválasztásához. Ez alapértelmezés szerint minden platformon engedélyezve van, a Windows kivételével, és globálisan módosítható, hogy a terheléselosztást maga az operációs rendszer tudja kezelni.
A körbefutó algoritmus a terhelést rotációs alapon egyenletesen osztja el az összes rendelkezésre álló folyamaton. Az első kérés továbbításra kerül az első dolgozói folyamathoz, a második a következő munkavállalói folyamathoz stb. A lista végének elérésekor az algoritmus újrakezdődik az elejétől.
Ez az egyik legegyszerűbb és leggyakrabban használt terheléselosztó algoritmus. De nem ez az egyetlen. A több kiemelt algoritmus lehetővé teszi a prioritások hozzárendelését és a legkevésbé terhelt vagy a leggyorsabb válaszidővel rendelkező kiszolgáló kiválasztását.
HTTP-kiszolgáló terheléselosztás
Klónozzuk és terheljük meg az egyensúlyt egy egyszerű HTTP szerveren a fürt modul segítségével. Íme az egyszerű Node hello-world példa kiszolgálója, amelyet kissé módosítottak néhány CPU-munka szimulálására, mielőtt válaszolt:
// server.js const http = require('http'); const pid = process.pid; http.createServer((req, res) => { for (let i=0; i { console.log(`Started process ${pid}`); });
Annak ellenőrzésére, hogy a kiegyensúlyozó, amelyet létrehozni fogunk, működni fog, belefoglaltam a folyamatot pid
a HTTP-válaszba, hogy azonosítsam, hogy az alkalmazás mely példánya foglalkozik valójában egy kéréssel.
Mielőtt létrehoznánk egy fürtöt, amely több szerverbe klónozza ezt a kiszolgálót, végezzünk el egy egyszerű benchmarkot arról, hogy ez a szerver hány kérést tud kezelni másodpercenként. Ehhez használhatjuk az Apache benchmarking eszközt. A server.js
fenti egyszerű kód futtatása után futtassa a következő ab
parancsot:
ab -c200 -t10 //localhost:8080/
Ez a parancs teszteli a kiszolgálót 200 egyidejű kapcsolattal 10 másodpercig.

A gépemen az egy csomópontú szerver másodpercenként körülbelül 51 kérést tudott kezelni. Természetesen az eredmények különbözőek lesznek a különböző platformokon, és ez egy nagyon leegyszerűsített teljesítményteszt, amely nem 100% -os pontosságú, de egyértelműen megmutatja azt a különbséget, amelyet egy fürt többmagos környezetben hozna létre.
Most, hogy van referencia-referenciaértékünk, a klasztermodul segítségével méretezhetjük az alkalmazást a klónozási stratégiával.
A server.js
fenti fájllal azonos szinten létrehozhatunk egy új fájlt ( cluster.js
) a fő folyamathoz ezzel a tartalommal (magyarázat következik):
// cluster.js const cluster = require('cluster'); const os = require('os'); if (cluster.isMaster) { const cpus = os.cpus().length; console.log(`Forking for ${cpus} CPUs`); for (let i = 0; i
In
cluster.js
, we first required both the cluster
module and the os
module. We use the os
module to read the number of CPU cores we can work with using os.cpus()
.
The
cluster
module gives us the handy Boolean flag isMaster
to determine if this cluster.js
file is being loaded as a master process or not. The first time we execute this file, we will be executing the master process and that isMaster
flag will be set to true. In this case, we can instruct the master process to fork our server as many times as we have CPU cores.
Now we just read the number of CPUs we have using the
os
module, then with a for loop over that number, we call the cluster.fork
method. The for loop will simply create as many workers as the number of CPUs in the system to take advantage of all the available processing power.
When the
cluster.fork
line is executed from the master process, the current file, cluster.js
, is run again, but this time in worker mode with the isMaster
flag set to false. There is actually another flag set to true in this case if you need to use it, which is the isWorker
flag.
When the application runs as a worker, it can start doing the actual work. This is where we need to define our server logic, which, for this example, we can do by requiring the
server.js
file that we have already.
That’s basically it. That’s how easy it is to take advantage of all the processing power in a machine. To test the cluster, run the
cluster.js
file:

Original text

I have 8 cores on my machine so it started 8 processes. It’s important to understand that these are completely different Node.js processes. Each worker process here will have its own event loop and memory space.
When we now hit the web server multiple times, the requests will start to get handled by different worker processes with different process ids. The workers will not be exactly rotated in sequence because the cluster module performs some optimizations when picking the next worker, but the load will be somehow distributed among the different worker processes.
We can use the same ab
command above to load-test this cluster of processes:

The cluster I created on my machine was able to handle 181 requests per second in comparison to the 51 requests per second that we got using a single Node process. The performance of this simple application tripled with just a few lines of code.
Broadcasting Messages to All Workers
Communicating between the master process and the workers is simple because under the hood the cluster module is just using the child_process.fork
API, which means we also have communication channels available between the master process and each worker.
Based on the server.js
/cluster.js
example above, we can access the list of worker objects using cluster.workers
, which is an object that holds a reference to all workers and can be used to read information about these workers. Since we have communication channels between the master process and all workers, to broadcast a message to all them we just need a simple loop over all the workers. For example:
Object.values(cluster.workers).forEach(worker => { worker.send(`Hello Worker ${worker.id}`); });
We simply used Object.values
to get an array of all workers from the cluster.workers
object. Then, for each worker, we can use the send
function to send over any value that we want.
In a worker file, server.js
in our example, to read a message received from this master process, we can register a handler for the message
event on the global process
object. For example:
process.on('message', msg => { console.log(`Message from master: ${msg}`); });
Here is what I see when I test these two additions to the cluster/server example:

Every worker received a message from the master process. Note how the workers did not start in order.
Let’s make this communication example a little bit more practical. Let’s say we want our server to reply with the number of users we have created in our database. We’ll create a mock function that returns the number of users we have in the database and just have it square its value every time it’s called (dream growth):
// **** Mock DB Call const numberOfUsersInDB = function() 5; this.count = this.count * this.count; return this.count; // ****
Every time numberOfUsersInDB
is called, we’ll assume that a database connection has been made. What we want to do here — to avoid multiple DB requests — is to cache this call for a certain period of time, such as 10 seconds. However, we still don’t want the 8 forked workers to do their own DB requests and end up with 8 DB requests every 10 seconds. We can have the master process do just one request and tell all of the 8 workers about the new value for the user count using the communication interface.
In the master process mode, we can, for example, use the same loop to broadcast the users count value to all workers:
// Right after the fork loop within the isMaster=true block const updateWorkers = () => { const usersCount = numberOfUsersInDB(); Object.values(cluster.workers).forEach(worker => { worker.send({ usersCount }); }); }; updateWorkers(); setInterval(updateWorkers, 10000);
Here we’re invoking updateWorkers
for the first time and then invoking it every 10 seconds using a setInterval
. This way, every 10 seconds, all workers will receive the new user count value over the process communication channel and only one database connection will be made.
In the server code, we can use the usersCount
value using the same message
event handler. We can simply cache that value with a module global variable and use it anywhere we want.
For example:
const http = require('http'); const pid = process.pid; let usersCount; http.createServer((req, res) => { for (let i=0; i { console.log(`Started process ${pid}`); }); process.on('message', msg => { usersCount = msg.usersCount; });
The above code makes the worker web server respond with the cached usersCount
value. If you test the cluster code now, during the first 10 seconds you’ll get “25” as the users count from all workers (and only one DB request would be made). Then after another 10 seconds, all workers would start reporting the new user count, 625 (and only one other DB request would be made).
This is all possible thanks to the communication channels between the master process and all workers.
Increasing Server Availability
One of the problems in running a single instance of a Node application is that when that instance crashes, it has to be restarted. This means some downtime between these two actions, even if the process was automated as it should be.
This also applies to the case when the server has to be restarted to deploy new code. With one instance, there will be downtime which affects the availability of the system.
When we have multiple instances, the availability of the system can be easily increased with just a few extra lines of code.
To simulate a random crash in the server process, we can simply do a process.exit
call inside a timer that fires after a random amount of time:
// In server.js setTimeout(() => { process.exit(1) // death by random timeout }, Math.random() * 10000);
When a worker process exits like this, the master process will be notified using the exit
event on the cluster
model object. We can register a handler for that event and just fork a new worker process when any worker process exits.
For example:
// Right after the fork loop within the isMaster=true block cluster.on('exit', (worker, code, signal) => { if (code !== 0 && !worker.exitedAfterDisconnect) { console.log(`Worker ${worker.id} crashed. ` + 'Starting a new worker...'); cluster.fork(); } });
It’s good to add the if condition above to make sure the worker process actually crashed and was not manually disconnected or killed by the master process itself. For example, the master process might decide that we are using too many resources based on the load patterns it sees and it will need to kill a few workers in that case. To do so, we can use the disconnect
methods on any worker and, in that case, the exitedAfterDisconnect
flag will be set to true. The if statement above will guard to not fork a new worker for that case.
If we run the cluster with the handler above (and the random crash in server.js
), after a random number of seconds, workers will start to crash and the master process will immediately fork new workers to increase the availability of the system. You can actually measure the availability using the same ab
command and see how many requests the server will not be able to handle overall (because some of the unlucky requests will have to face the crash case and that’s hard to avoid.)
When I tested the code, only 17 requests failed out of over 1800 in the 10-second testing interval with 200 concurrent requests.

That’s over 99% availability. By just adding a few lines of code, we now don’t have to worry about process crashes anymore. The master guardian will keep an eye on those processes for us.
Zero-downtime Restarts
What about the case when we want to restart all worker processes when, for example, we need to deploy new code?
We have multiple instances running, so instead of restarting them together, we can simply restart them one at a time to allow other workers to continue to serve requests while one worker is being restarted.
Implementing this with the cluster module is easy. Since we don’t want to restart the master process once it’s up, we need a way to send this master process a command to instruct it to start restarting its workers. This is easy on Linux systems because we can simply listen to a process signal like SIGUSR2
, which we can trigger by using the kill
command on the process id and passing that signal:
// In Node process.on('SIGUSR2', () => { ... }); // To trigger that $ kill -SIGUSR2 PID
This way, the master process will not be killed and we have a way to instruct it to start doing something. SIGUSR2
is a proper signal to use here because this will be a user command. If you’re wondering why not SIGUSR1
, it’s because Node uses that for its debugger and you want to avoid any conflicts.
Unfortunately, on Windows, these process signal are not supported and we would have to find another way to command the master process to do something. There are some alternatives. We can, for example, use standard input or socket input. Or we can monitor the existence of a process.pid
file and watch that for a remove event. But to keep this example simple, we’ll just assume this server is running on a Linux platform.
Node works very well on Windows, but I think it’s a much safer option to host production Node applications on a Linux platform. This is not just because of Node itself, but many other production tools that are much more stable on Linux. This is my personal opinion and feel free to completely ignore it.
By the way, on recent versions of Windows, you can actually use a Linux subsystem and it works very well. I’ve tested it myself and it was nothing short of impressive. If you’re developing a Node applications on Windows, check out Bash on Windows and give it a try.
In our example, when the master process receives the SIGUSR2
signal, that means it’s time for it to restart its workers, but we want to do that one worker at a time. This simply means the master process should only restart the next worker when it’s done restarting the current one.
To begin this task, we need to get a reference to all current workers using the cluster.workers
object and we can simply just store the workers in an array:
const workers = Object.values(cluster.workers);
Then, we can create a restartWorker
function that receives the index of the worker to be restarted. This way we can do the restarting in sequence by having the function call itself when it’s ready for the next worker. Here’s an example restartWorker
function that we can use (explanation follows):
const restartWorker = (workerIndex) => { const worker = workers[workerIndex]; if (!worker) return; worker.on('exit', () => { if (!worker.exitedAfterDisconnect) return; console.log(`Exited process ${worker.process.pid}`); cluster.fork().on('listening', () => { restartWorker(workerIndex + 1); }); }); worker.disconnect(); }; restartWorker(0);
Inside the restartWorker
function, we got a reference to the worker to be restarted and since we will be calling this function recursively to form a sequence, we need a stop condition. When we no longer have a worker to restart, we can just return. We then basically want to disconnect this worker (using worker.disconnect
), but before restarting the next worker, we need to fork a new worker to replace this current one that we’re disconnecting.
We can use the exit
event on the worker itself to fork a new worker when the current one exists, but we have to make sure that the exit action was actually triggered after a normal disconnect call. We can use the exitedAfetrDisconnect
flag. If this flag is not true, the exit was caused by something else other than our disconnect call and in that case, we should just return and do nothing. But if the flag is set to true, we can go ahead and fork a new worker to replace the one that we’re disconnecting.
When this new forked worker is ready, we can restart the next one. However, remember that the fork process is not synchronous, so we can’t just restart the next worker after the fork call. Instead, we can monitor the listening
event on the newly forked worker, which tells us that this worker is connected and ready. When we get this event, we can safely restart the next worker in sequence.
That’s all we need for a zero-downtime restart. To test it, you’ll need to read the master process id to be sent to the SIGUSR2
signal:
console.log(`Master PID: ${process.pid}`);
Start the cluster, copy the master process id, and then restart the cluster using the kill -SIGUSR2 PID
command. You can also run the same ab
command while restarting the cluster to see the effect that this restart process will have on availability. Spoiler alert, you should get ZERO failed requests:

Process monitors like PM2, which I personally use in production, make all the tasks we went through so far extremely easy and give a lot more features to monitor the health of a Node.js application. For example, with PM2, to launch a cluster for any app, all you need to do is use the -i
argument:
pm2 start server.js -i max
And to do a zero downtime restart you just issue this magic command:
pm2 reload all
However, I find it helpful to first understand what actually will happen under the hood when you use these commands.
Shared State and Sticky Load Balancing
Good things always come with a cost. When we load balance a Node application, we lose some features that are only suitable for a single process. This problem is somehow similar to what’s known in other languages as thread safety, which is about sharing data between threads. In our case, it’s sharing data between worker processes.
For example, with a cluster setup, we can no longer cache things in memory because every worker process will have its own memory space. If we cache something in one worker’s memory, other workers will not have access to it.
If we need to cache things with a cluster setup, we have to use a separate entity and read/write to that entity’s API from all workers. This entity can be a database server or if you want to use in-memory cache you can use a server like Redis or create a dedicated Node process with a read/write API for all other workers to communicate with.

Don’t look at this as a disadvantage though, because using a separate entity for your application caching needs is part of decomposing your app for scalability. You should probably be doing that even if you’re running on a single core machine.
Other than caching, when we’re running on a cluster, stateful communication in general becomes a problem. Since the communication is not guaranteed to be with the same worker, creating a stateful channel on any one worker is not an option.
The most common example for this is authenticating users.

With a cluster, the request for authentication comes to the master balancer process, which gets sent to a worker, assuming that to be A in this example.

Worker A now recognizes the state of this user. However, when the same user makes another request, the load balancer will eventually send them to other workers, which do not have them as authenticated. Keeping a reference to an authenticated user session in one instance memory is not going to work anymore.
This problem can be solved in many ways. We can simply share the state across the many workers we have by storing these sessions’ information in a shared database or a Redis node. However, applying this strategy requires some code changes, which is not always an option.
If you can’t do the code modifications needed to make a shared storage of sessions here, there is a less invasive but not as efficient strategy. You can use what’s known as Sticky Load Balancing. This is much simpler to implement as many load balancers support this strategy out of the box. The idea is simple. When a user authenticates with a worker instance, we keep a record of that relation on the load balancer level.

Then, when the same user sends a new request, we do a lookup in this record to figure out which server has their session authenticated and keep sending them to that server instead of the normal distributed behavior. This way, the code on the server side does not have to be changed, but we don’t really get the benefit of load balancing for authenticated users here so only use sticky load balancing if you have no other option.
The cluster module actually does not support sticky load balancing, but a few other load balancers can be configured to do sticky load balancing by default.
Thanks for reading.
Learning React or Node? Checkout my books:
- Learn React.js by Building Games
- Node.js Beyond the Basics