Server Clustering is a method of turning multiple computer servers into a cluster, which is a group of servers that acts like a single system. Its different with Load Balancer. Load Balancing is about the distribution of workloads across multiple computing resources, such as computers, server clusters, network links, etc.
In general, the cluster maximizes the processor performance of the server. If you have 8core processor, you can make it all work as cluster or you just need 2core, you can set it up.
This time, I will talk about how to create clusters for nestjs. As you know, Nestjs is a framework that can be relied on to build applications such as Rest API. I won't go into detail about the concept but will get straight to how it works.
Installation
For Nestjs Installation, you can look in the documentation. After you made it all, create app-cluster.service.ts
in src/
directory.
import * as cluster from 'cluster';
import * as os from 'os';
import { Injectable } from '@nestjs/common';
const numCPUs = os.cpus().length;
@Injectable()
export class AppClusterService {
static clusterize(callback: Function): void {
if(cluster.isMaster){
console.log(`Master server started on ${process.pid}`);
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died. Restarting`);
cluster.fork();
})
} else {
console.log(`Cluster server started on ${process.pid}`)
callback();
}
}
}
Then, you can called it in main.ts
in src/
directory.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { AppClusterService } from './app_cluster.service';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
//Call app-cluster.service.ts here.
AppClusterService.clusterize(bootstrap);
Now, run you app using nodemon or pm2. I used PM2 to run the project. pm2 start dist/main.js -i max
. -i max
as a sign of how many cores we will use. If max
, that means all we will use. Or you just change the max
with specified number. (Note: run npm run build
to generate dist directory.)
b. Using specified resources. e.g 4
If either worker crashes or dies, it will be automatically moved to the live worker as the server prepares a new worker to replace the crashed worker.
Github Repo : https://github.com/danudenny/nestjs-cluster
I want to discuss further about this, maybe there is a statement from me that is wrong or maybe there is a suggestion to be more effective and efficient.
Top comments (4)
Hello,
Thanks for sharing.
I have doubts about that clusterize function and having to inject the bootstrap method as a callback because the PM2 cluster mode doesn't require ANY code modification.
source : pm2.keymetrics.io/docs/usage/clust...
Hi what about if you would like delegate only certain modules to separated thread? Let's imagine that I have Main thread , and some 2 modules with heavy endpoints and services, how to put them on same PORT in nest?
In Your cauterization will be possible to use same listen port ? 8080 Cheers nice article!
So the filename is
app_cluster.service.ts
instead ofapp-cluster.service.ts
@Injectable in AppClusterService I think it doesnt need it because that marks a class as a provider. Providers can be injected into other classes via constructor parameter injection