Node.js worker threads are problematic, but they work great for us
4 days ago
- #Event Loop
- #Worker Threads
- #Node.js
- Node.js runs on a single thread, which can lead to event loop starvation when CPU-heavy tasks monopolize the main thread.
- Worker threads in Node.js provide isolated execution contexts with their own V8 isolates and event loops, preventing CPU-bound tasks from blocking critical operations like heartbeats.
- Worker threads communicate via message passing using the structured clone algorithm, which serializes data but cannot serialize functions.
- Each worker thread has significant memory overhead (~10MB) and startup time, making them suitable for long-lived tasks rather than short-lived ones.
- Bundlers struggle to detect worker thread files automatically, requiring explicit inclusion in build configurations.
- Inngest Connect moved its WebSocket and heartbeat logic to a worker thread to prevent event loop starvation caused by user code.
- Worker threads require designing a message-passing protocol for communication between the main thread and worker thread.
- Respawning crashed worker threads requires implementing exponential backoff to avoid tight respawn loops.
- Worker threads offer hard isolation but come with constraints that differ from threading models in Go, Rust, or Python.