docs.nestjs.com icon indicating copy to clipboard operation
docs.nestjs.com copied to clipboard

Multi threading / Clustering

Open MickL opened this issue 5 years ago • 11 comments

I have already opened an issue about multi threading two years ago: https://github.com/nestjs/nest/issues/248

Now I am back at the exact same thing. I am wondering how to have the NestJS app run on multiple cpu cores at the same time.

Use case: Socket.io connections can be split up per core to accept more concurrent connections.

It would be nice if this works either out of the box, maybe with worker threads for easier communication between the cores, or at least have an explanation how to do so in the documentation.

If someone could give me hints how to archive this, or if there might be some pitfalls, I could write the documentation page.

MickL avatar Mar 10 '20 11:03 MickL

Nothing has changed here. You can run NestJS application in a cluster in the same way as you do it with any Node.js application built on top of express/fastify/socket.io.

For better developer experience, we can consider adding a dedicated chapter in the docs though. Let me move this issue to the proper directory.

kamilmysliwiec avatar Mar 10 '20 11:03 kamilmysliwiec

Just for the record: It might be a good or even better idea to just spawn the same next.js app multiple times and let iptables linux kernel module or Nginx handle the load-balancing.

Accoring to this article it might be an even better approach than node cluster.

Also PM2 already includes an automatic load balancer that will share all HTTP[s]/Websocket/TCP/UDP connections between each spawned process. But it uses Node cluster which is kind of slower according to the article.

MickL avatar Mar 10 '20 12:03 MickL

according to @kamilmysliwiec answer I tried to run Multithreading using cluster-js That's mentioned in Nodejs Documentation https://nodejs.org/api/cluster.html

by added theese lines to main.ts Multithreading worked with me

const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

async function bootstrap() {
  const app = await NestFactory.create(AppModule);


  if (cluster.isMaster) {
    console.log(`Master ${process.pid} is running`);

    // Fork workers.
    for (let i = 0; i < numCPUs; i++) {
      cluster.fork();
    }

    cluster.on('exit', (worker, code, signal) => {
      console.log(`worker ${worker.process.pid} died`);
    });
  } else {
    // Workers can share any TCP connection
    // In this case it is an HTTP server
    await app.listen(process.env.PORT || 3000);

    console.log(`Worker ${process.pid} started`);
  }

}
bootstrap();

Abkholy avatar Oct 08 '20 08:10 Abkholy

Is there any way to use worker threads in nestjs? An example would be really helpful. :)

arkasaha2 avatar Oct 16 '20 14:10 arkasaha2

@MickL I found this and implemented it today. Works as expected. https://dev.to/danudenny/clustering-nest-js-2mj7

The only thing now is that I need to communicate with the worker threads.

fromage9747 avatar Feb 27 '21 06:02 fromage9747

Anyone managed to use Worker Threads with DI?

cerireyhan avatar Sep 11 '21 08:09 cerireyhan

As mentioned in https://nodejs.org/dist/latest-v16.x/docs/api/worker_threads.html you should not use worker_threads to bump up your io operations. It is for CPU intensive tasks. I guess the best way to use it is separate your cpu intensive code to worker script and use it with some worker pool (e.g. piscina) as simple async operation. Of course you can wrap your worker pool into injectable service.

KindOf avatar Dec 04 '21 19:12 KindOf

Here is simple example https://github.com/KindOf/nestjs-worker-pool-example

KindOf avatar Dec 04 '21 20:12 KindOf

Here is simple example https://github.com/KindOf/nestjs-worker-pool-example

Spasibo

levi770 avatar Oct 30 '23 14:10 levi770

Nestjs as known is a framework not library, and one of the framework characteristics is providing a full life cycle of the application included dependencies resolving, multi threading, database connection pools, outgoing networks, in going networks and many other aspects. I can see Nestjs has some of these and others it doesn't, worker threads now is a native feature in nodejs which means if I say I am a framework then I should support all the native runtime features, this is what we see in .net, laravel on php, spring boot on java and other languages. I hope someday working on this, this will open a lot of capabilities for nestjs to support different concurrent paradigms.

tareksaleem avatar Nov 27 '23 16:11 tareksaleem

You can spawn workers like this

const { workerData, parentPort } = require('worker_threads'); const { NestFactory } = require('@nestjs/core'); const { AppModule } = require('./app.module'); // Import your AppModule

async function bootstrap() { const appContext = await NestFactory.createApplicationContext(AppModule); // Use appContext to get your services and providers

// Example of handling messages from the main thread parentPort.on('message', (message) => { console.log('Message from main thread:', message); // You can call your services here }); }

bootstrap();

levi770 avatar Nov 27 '23 16:11 levi770