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 worker_threads. 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 workerthreads

The solution? Worker threads! Unlike clusters, which create multiple processes, worker threads allow us to run multiple tasks in parallel within the same process.

Implementing Worker Threads

Update server.js to use workerthreads:

const { Worker, isMainThread, parentPort } = require(’worker_threads’);

const express = require(’express’);

const app = express();

const PORT = 3000;

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

    res.send(’pong’);

});

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

    const worker = new Worker(’./worker.js’);

    worker.on(’message’, (sum) => {

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

    });

});

app.listen(PORT, () => {

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

});

Now, create a separate worker.js file:

const { parentPort } = require(’worker_threads’);

let sum = 0;

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

    sum += i;
}

parentPort.postMessage(sum);

Testing the Worker Threaded Server

  1. Restart your server:

2. node server.js

  1. You’ll see that /ping now responds instantly, even while /heavy is processing. That’s because the computation is offloaded to a separate thread!

Conclusion

By default, Node.js runs on a single thread, making it vulnerable to CPU-intensive tasks blocking other requests. Using worker_threads, we can distribute the workload across multiple threads within the same process, ensuring smooth and responsive APIs.

Now, go ahead and implement worker threads 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