Worker threads provide a way to create independent JavaScript execution threads that run in parallel. This post walks through an example of offloading CPU-intensive tasks to a worker thread in Node.js. Without offloading, tasks can block the event loop and prevent the server from processing other requests. The article shows an example of the server going from processing 7.3k requests to 0 tasks due to a CPU-intensive task.
Wednesday, March 20, 2024To profile the CPU usage of a Node.js application, launch the application with the ‘--inspect flag' and connect to it using Chrome DevTools via ‘chrome://inspect'. Start and stop the CPU profiling using the Record button in the Performance panel of DevTools to capture how CPU time is spent during application execution.
Node.js has added an experimental flag called --experimental-strip-types that transpiles TypeScript source code into JavaScript source code.
This guide explores profiling techniques to pinpoint high CPU usage in Node.js applications. It covers using built-in profilers, Chrome DevTools, the Node Inspector API, and the perf tool. The guide also mentions continuous profiling for ongoing monitoring and explores memory leak detection.
Thursday, July 4, 2024Trigger encountered reliability and performance issues in its Node.js application due to event loop lag, which led to high CPU usage and crashes. To fix this, it fixed inefficient code, added pagination, and monitored event loop lag with OpenTelemetry. Moving forward, it will keep an eye on larger payloads, as a learning from this experience.
This article recounts the author's experience debugging a memory leak in a Node.js application. The story began with intermittent "502 Bad Gateway" errors on a crowdfunding website, leading the author to suspect PM2, a process manager, as the culprit. After ruling out PM2, the investigation focused on a memory leak within the Next.js application. The author used tools like Chrome DevTools to take heap snapshots and identified Moment.js as the source of the leak. It simply needed to be upgraded to fix the leak.