
Kar started this conversation 1 week ago.
Random API slowdowns due to event loop blocking
Some APIs in my Node.js/Express app respond slowly occasionally—responses take several seconds even though CPU & memory usage look normal and DB calls are fast. Distributing load across PM2 cluster doesn't help. What might be causing this slowdown?
Digiaru
Posted 1 week ago
Even with asynchronous I/O, CPU-bound or synchronous code can block the Node.js event loop, preventing other requests from processing promptly. This creates unpredictable latency in APIs despite available hardware resources ([turn0reddit17]turn0search1). Common culprits: • Long-running synchronous loops, recursive calculations, or CPU-heavy processing. • Triggers like synchronous string manipulation, JSON parsing of large payloads, or ORM overhead. • Event loop lag accumulates under concurrent request pressure. Fixes: • Move heavy computation to Worker Threads or microservices. • Offload CSV processing, image manipulation, or CPU-intensive logic to separate threads. • Monitor event loop lag using tools like blocked-at or node-blocked, and log slow tick durations ([turn0reddit17]. • Ensure third-party libraries don’t perform blocking operations synchronously.