Skip to content

This project demonstrates how to handle CPU-bound tasks in Node.js using Worker Threads. It includes multiple API routes to test blocking, single-worker, and multi-worker scenarios, along with simple timing comparisons.

Notifications You must be signed in to change notification settings

Mahin725/Node-Multithreading

Repository files navigation

# Node.js Worker Threads Demo

This project demonstrates how to handle **CPU-bound tasks** in Node.js using **Worker Threads**.  
It includes multiple API routes to test **blocking**, **single-worker**, and **multi-worker** scenarios, with simple timing comparisons.

## Features

- `/non-blocking` – Regular API route without CPU-heavy computation
- `/blocking` – CPU-heavy computation using:
  - **Single Worker**
  - **Four Workers** (task distributed across threads)
- Easy to compare **response times** and **CPU usage**
- Demonstrates how to prevent blocking the Node.js main thread

## API Endpoints

### 1. `/non-blocking`

- **Method:** `GET`
- **Description:** Simple non-blocking route that returns a message immediately.
- **Example:**
  ```bash
  curl http://localhost:4000/non-blocking
  • Response:
    this is Non Blocking Response Sent
    

2. /blocking

  • Method: GET
  • Description: Performs a CPU-intensive computation.
  • Behavior:
    • Single Worker: Offloads the entire computation to one worker thread.
    • Four Workers: Splits the task evenly across four worker threads.
  • Example (with timing on Windows):
    curl -o NUL -s -w "Total time: %{time_total}s\n" http://localhost:4000/blocking
  • Cross-platform timing example:
    curl -s -w "Total time: %{time_total}s\n" -o /dev/null http://localhost:4000/blocking

Observe the timing differences between single worker and four workers.
Compare with /non-blocking to see main thread responsiveness.

How It Works

  • Single Worker: The entire computation runs in one Worker Thread.
  • Four Workers: The computation is divided into four equal parts, executed in parallel, and results are combined.
  • Workers receive data via workerData and communicate results with postMessage.
  • The main thread uses Promise.all to aggregate results from multiple workers.

Practical Notes

  • The number of workers should ideally match the number of logical CPU cores.
  • Using too many workers can increase memory usage and reduce performance due to context switching.
  • Worker Threads are beneficial only for CPU-bound tasks. For I/O-bound tasks, Node.js's event loop is already efficient.

Testing / Benchmarking

Use curl, Postman, or any HTTP client to measure response times.

Route Workers Approximate Response Time
/non-blocking None ~0.01s
/blocking 1 worker ~7–8s
/blocking 4 workers ~2–3s

Example curl commands

curl -s -w "Total time: %{time_total}s\n" -o /dev/null http://localhost:4000/non-blocking
curl -s -w "Total time: %{time_total}s\n" -o /dev/null http://localhost:4000/blocking

Times will vary depending on your CPU and system load.

Project Structure

├── index.js          # Express server and route definitions
├── worker.js         # Single worker implementation
├── four_worker.js    # Four-worker task splitting implementation
├── package.json
└── README.md

Conclusion

This project helps understand:

  • Limitations of the Node.js main thread with CPU-bound tasks
  • How to use Worker Threads for offloading work
  • Benefits of task distribution across multiple workers
  • Practical performance testing with multithreading

Author: Raihanul Islam Sharif
Goal: Explore multithreading in Node.js and optimize CPU-bound operations

About

This project demonstrates how to handle CPU-bound tasks in Node.js using Worker Threads. It includes multiple API routes to test blocking, single-worker, and multi-worker scenarios, along with simple timing comparisons.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published