1 like
·
1.8K reads
1 comment
Hi Klaus, first of all, welcome to JS/TS world ! Please let me suggest something to your code, if you're using multiple cores, for each 4 cores, spawn a node cluster to improve significantly the ammount of requests. For example:
/////// File : cluster.service.ts
import as cluster from "cluster"; import as process from "node:process";
const numOfClusters = 4; // Im using 16 cores
@Injectable()
export class ClusterService {
static clusterize(callback: Function): void {
if (cluster.isMaster) {
console.log(MASTER SERVER (${process.pid}) IS RUNNING
);
for (let i = 0; i < numOfClusters; i++) { cluster.fork(); }
cluster.on("exit", (worker, code, signal) => {
console.log(worker ${worker.process.pid} died
);
});
} else {
callback();
}
}
}
/////// File : main.ts
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { ClusterService } from './cluster/cluster.service'; import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
async function bootstrap() { const app = await NestFactory.create( AppModule, new FastifyAdapter() ); await app.listen(3000, '127.0.0.1'); } ClusterService.clusterize(bootstrap)
/////// Conclusion :
I tested this in bare metal and docker containers and i get 20% ~ 40% more requests than spring boot equivalent. Im really surprised how nodejs can handle too many requests with minimal effort