hallo, today i though why not i actually show off something that's pretty nice that's been in nodejs, cluster. Now, yes cluster has been around for a very long time ( been around since node 0.10.48 to be exact ), and it has been one of the most useful modules for me imo. but, what exactly does it do, and why is it useful you might say?
well, there's many reasons.
one could be to optimize a multi-thread CPU and run with more speed.
another could be you want to keep your database controller apart from the main process to speed things up.
no matter what it is, cluster is a wonderful choice.
My most recent project, quick-scripts ( You will learn more about this next month ), uses 3 threads total. it's as simple as this:
import express from "express";
import cluster from "cluster";
if (cluster.isMaster) {
const expressServer= cluster.fork() //express server (id 1)
const dbc= cluster.fork() //database manager (id 2)
}
now, it may not look like much, but this is splitting up resources and optimizing speed.
now, each worker ( this is what you call the child of a parent process ), listens to the master ( the master is the parent of the cluster ), it's extremely simple to communicate between the two. all it takes is
expressServer.send("deny all r")
and on the worker's end,
process.on('message', message => {
//...
})
and bam! you have a working communication between the two.
another thing cluster is useful for, is just clearing out a bit of clutter that normal processes make. it's simplistic, and denies the fame of childprocess, which imo is absolutely horrific to use.
now, lets go get cluster some rep it deserves for doing what it does.
Top comments (0)