Skip to content

cluster: Worker based implementation? #37449

Description

@ronag

Would it make sense/be possible to re-implement/create alternative implementation for cluster that is based on Workers instead of child processes? Would make it possible to more efficiently pass data between the parent process and workers.

Activity

  1. added
    clusterIssues and PRs related to the cluster subsystem.
    questionIssues asking questions about Node.js.
    on Feb 20, 2021
  2. ljluestc commented on Nov 11, 2023

    @ljluestc
    const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
    const http = require('http');
    
    if (isMainThread) {
      // This is the main thread (master process).
      const numWorkers = 4;
      const workers = [];
    
      // Create worker threads.
      for (let i = 0; i < numWorkers; i++) {
        const worker = new Worker(__filename, { workerData: { workerId: i } });
        workers.push(worker);
      }
    
      // Create a simple HTTP server to distribute requests.
      const server = http.createServer((req, res) => {
        // Distribute incoming HTTP requests among workers.
        const worker = workers[Math.floor(Math.random() * numWorkers)];
        worker.postMessage(req.url);
      });
    
      server.listen(3000, () => {
        console.log('Server is running on port 3000');
      });
    } else {
      // This is a worker thread.
      const workerId = workerData.workerId;
    
      parentPort.on('message', (url) => {
        // Handle the request in the worker thread.
        console.log(`Worker ${workerId} processing request for URL: ${url}`);
    
        // Simulate some work (e.g., processing the request).
        setTimeout(() => {
          console.log(`Worker ${workerId} finished processing request for URL: ${url}`);
          parentPort.postMessage(`Response from Worker ${workerId} for URL: ${url}`);
        }, 1000);
      });
    }
    
    
  3. github-actions commented on Jun 27, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  4. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jun 27, 2026
  5. github-actions commented on Jul 28, 2026

    @github-actions
    Contributor

    This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days).
    If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clusterIssues and PRs related to the cluster subsystem.questionIssues asking questions about Node.js.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions