• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

# Is Node.js Really Single-Threaded? Here’s What Most Developers Get Wrong About Node’s Backend Power

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



When I recently hit a production issue where one of our Azure Functions unexpectedly fired hundreds of API calls within seconds, it pushed me down a rabbit hole.

? What caused it?


Node.js was the culprit — or rather, my misunderstanding of how Node.js handles caching and concurrency.

Due to a cold start, the in-memory cache reset, and with Azure's high parallelism settings, each parallel thread triggered its own set of calls. Boom — API surge.

This got me asking...

How does Node.js actually work behind the scenes?

Is Node.js really "single-threaded"?

How does it compare to Java and other backend technologies?
Let’s break it down.

? Node.js Architecture — A Quick Recap


At its core, Node.js is built around:

  • ? A single-threaded event loop
  • ⏳ A non-blocking I/O model
  • ? An internal thread pool (libuv) for some tasks
  • ? Optional worker threads for CPU-heavy work

This model shines in high I/O scenarios: chat apps, API gateways, real-time apps, etc.

? How Request Handling Works

  1. I/O-bound requests (e.g., DB reads, file reads) → delegated to non-blocking handlers
  2. CPU-heavy work (e.g., image processing) → needs special treatment or it blocks the event loop
  3. Event Loop picks up completed work and executes callbacks
⚡ What Makes Node.js Different?

✅ Strengths:


FeatureNode.js
Event LoopYes
Single ThreadedBy default
Asynchronous by DesignYes
CPU-bound WorkNeeds manual scaling
Native MultithreadingNow possible via Worker Threads
Scales via Cluster or WorkersYes
Best UseAPIs, Microservices, Real-time apps
? Comparing with Java (and Others)

FeatureNode.jsJava
Thread ModelSingle (default) + workersMulti-threaded
Default ConcurrencyEvent LoopThread Pools
Memory OverheadLowHigher
ScalingManual (cluster/fork/worker)Automatic via thread pools
Heavy CPU WorkNeeds worker threadsNative threading
Ideal UseReal-time, async I/OComplex business logic, CPU-heavy apps
? Java inherently manages threads and thread pools.

Node requires explicit configuration to utilize more CPU cores (via cluster, worker threads, etc.)
?️ Worker Threads in Node.js — Multithreading in Action


const { Worker } = require('worker_threads');

function runWorker(path, data) {
return new Promise((resolve, reject) => {
const worker = new Worker(path, { workerData: data });
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0) reject(new Error(`Worker stopped with code ${code}`));
});
});
}

// Usage:
runWorker('./heavyTask.js', { payload: 123 }).then(console.log);

Using this, you can spawn real threads up to the number of CPU cores available on the system.

? Real-World Problem: Azure + Node.js + Cold Start = Disaster


In my recent experience:

  1. Node.js Azure Function used in-memory cache
  2. Cold start triggered cache reset
  3. High degree of parallelism (16 threads!) meant every invocation missed the cache
  4. Result: hundreds of external API calls = system overload

This led me to study Node’s internals more deeply and compare with Java, which would’ve used:

  • JVM-level thread pooling
  • Shared memory space
  • Potentially avoided the burst entirely
? Thread Utilization and Scaling

  • A system with 8 cores, 16 logical processors can theoretically handle 16 threads in parallel
  • But OS and system apps use some threads → leave 2–4 for system
  • Use max 12–14 threads (Java or Node workers)

In Node.js:


const os = require('os');
const usableThreads = os.cpus().length - 2; // e.g., 14 threads
? When Should You Choose Node.js?


Choose Node.js if:

  • You’re building real-time apps (chat, WebSocket)
  • You need API gateways, proxies, or high I/O throughput
  • You can split heavy work into services or offload to queues

Avoid Node.js if:

  • Your app involves heavy CPU-bound synchronous logic
  • You need robust multithreading natively
  • You prefer strongly typed OOP-heavy codebases
✅ Final Thoughts

  • Node.js isn’t just single-threaded — it’s smartly single-threaded, with options to scale via worker threads or clusters.
  • But those choices are manual, and you must understand the architecture to avoid pitfalls like I did on Azure.
  • If you’re choosing a backend tech stack, factor in:
  • Type of load: I/O-heavy vs CPU-heavy
  • Scaling model: thread-based or event-driven
  • Operational complexity

.
.
.
NodeJs, Backend, Backend Architecture, Event Loop, Multi-threading, Worker Threads, Java, Java Vs NodeJs, Performance, Azure, Cold Start, High Parallelism, CPU Bound Tasks


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу