↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

How To Use Multithreading in Node.js

Node.js uses a single main thread for JavaScript, so CPU-heavy work blocks the event loop. The article shows how worker_threads let you run CPU tasks in parallel, keep I/O on libuv’s hidden threads, and reuse workers through pools for better throughput.

Reading notes
#

  • Node.js runs JavaScript on one main thread, so CPU-bound tasks block other code until they finish.
  • libuv provides hidden threads for I/O operations such as file reads and network requests, which keeps those calls from blocking the main thread.
  • worker_threads lets Node.js create threads for JavaScript work and send results back to the main thread with message passing.
  • The tutorial builds an example app that first runs a CPU-heavy task on one worker, then splits the work across four threads.
  • Worker pools are presented as the practical option for production because they reuse threads instead of creating a new worker for each request.
  • Piscina and Poolifier are listed as pool libraries, with Piscina shown for automatic worker lifecycle management and queued jobs.
  • The article stresses resource limits, timeouts, input validation, and secure defaults so a single worker cannot exhaust memory or CPU.
  • It recommends worker threads for hashing, compression, image processing, media processing, machine learning inference, and WebAssembly execution.
  • It recommends async I/O instead of workers for database queries, network requests, filesystem reads, and cache lookups.
  • Production guidance includes queue-backed APIs, backpressure, worker recycling, observability, and matching pool size to available vCPUs.
  • Monitoring should cover event loop lag, pool utilization, heap usage, queue depth, and worker memory.
  • Deployment advice covers Docker, process managers such as pm2 or systemd, and exporting metrics for dashboards and alerts.