Ever built a Node.js API and noticed that a single CPU-intensive request can bring everything to a halt? Yep, that’s because Node.js runs on a single thread by default. But don’t worry! We can fix this using node:cluster. Let me walk you through the problem and the solution with a hands-on example.

The Single-Thread Problem

Node.js is event-driven and non-blocking, which makes it great for handling I/O-heavy applications. But when it comes to CPU-intensive tasks, that single-threaded nature becomes a bottleneck. Let’s demonstrate this by creating two simple API endpoints:

  1. A ping endpoint (/ping) that just responds with "pong".
  2. A CPU-intensive endpoint (/heavy) that does some heavy computation.

Setting Up the Express Server

First, install Express if you haven’t already:

pnpm add express

Now, create server.js and add the following code:

const express = require(’express’);

const app = express();

const PORT = 3000;

// Simple /ping route

app.get(’/ping’, (req, res) => {

    res.send(’pong’);

});

// CPU-intensive route

app.get(’/heavy’, (req, res) => {

    let sum = 0;

    for (let i = 0; i < 1e9; i++) {

        sum += i;

    }

    res.send(`Sum: ${sum}`);

});

app.listen(PORT, () => {

    console.log(`Server running on port ${PORT}`);

});

The Problem in Action

  1. Start your server:

2. node server.js

  1. Open your browser and visit http://localhost:3000/ping. It should respond instantly.
  2. Now, visit http://localhost:3000/heavy. This will take a while because of the heavy computation.
  3. While the /heavy request is processing, try hitting /ping again. You’ll notice that it doesn’t respond until /heavy is done! That’s because the single thread is blocked.

Solving It with nod cluster

The solution? Node.js clusters! They allow us to spawn multiple processes (workers), utilizing multiple CPU cores instead of just one.

Implementing Clustering

Update server.js to use node:cluster:

const cluster = require(’node:cluster’);

const os = require(’node:os’);

const express = require(’express’);

 

const numCPUs = os.cpus().length;

 

if (cluster.isPrimary) {

    console.log(`Primary process ${process.pid} is running`);

   

    // Fork workers

    for (let i = 0; i < numCPUs; i++) {

        cluster.fork();

    }

    cluster.on(’exit’, (worker) => {

        console.log(`Worker ${worker.process.pid} died. Spawning a new one...`);

        cluster.fork();
    });

} else {

    const app = express();

    const PORT = 3000;

    app.get(’/ping’, (req, res) => {

        res.send(’pong’);

    });

    app.get(’/heavy’, (req, res) => {

        let sum = 0;

        for (let i = 0; i < 1e9; i++) {

            sum += i;
        }
        res.send(`Sum: ${sum}`);
    });
    app.listen(PORT, () => {

        console.log(`Worker ${process.pid} running on port ${PORT}`);
    });
}

Testing the Clustered Server

  1. Restart your server:

2. node server.js

  1. You’ll see multiple worker processes running.
  2. Now, try /ping while /heavy is processing. You’ll notice that /ping responds instantly! That’s because the workload is now distributed among multiple processes.

Conclusion

By default, Node.js runs on a single thread, making it vulnerable to CPU-intensive tasks blocking other requests. Using node:cluster, we can distribute the load across multiple worker processes, ensuring smooth and responsive APIs.

Now, go ahead and implement clustering in your own Node.js projects to unlock their full potential!

Our Trusted
Partner.

Unlock Valuable Cloud and Technology Credits

Imagine reducing your operational costs by up to $100,000 annually without compromising on the technology you rely on. Through our partnerships with leading cloud and technology providers like AWS (Amazon Web Services), Google Cloud Platform (GCP), Microsoft Azure, and Nvidia Inception, we can help you secure up to $25,000 in credits over two years (subject to approval).

These credits can cover essential server fees and offer additional perks, such as:

  • Google Workspace accounts
  • Microsoft accounts
  • Stripe processing fee waivers up to $25,000
  • And many other valuable benefits

Why Choose Our Partnership?

By leveraging these credits, you can significantly optimize your operational expenses. Whether you're a startup or a growing business, the savings from these partnerships ranging from $5,000 to $100,000 annually can make a huge difference in scaling your business efficiently.

The approval process requires company registration and meeting specific requirements, but we provide full support to guide you through every step. Start saving on your cloud infrastructure today and unlock the full potential of your business.

exclusive-partnersexclusive-partners

Let's TALK

Let's TALK and bring your ideas to life! Our experienced team is dedicated to helping your business grow and thrive. Reach out today for personalized support or request your free quote to kickstart your journey to success.

DIGITAL PRODUCTUI/UX DESIGNDIGITAL STUDIOBRANDING DESIGNUI/UX DESIGNEMAIL MARKETINGBRANDING DESIGNUI/UX DESIGNEMAIL MARKETING
DIGITAL PRODUCTUI/UX DESIGNDIGITAL STUDIOBRANDING DESIGNUI/UX DESIGNEMAIL MARKETINGBRANDING DESIGNUI/UX DESIGNEMAIL MARKETING