CodexBloom - Programming Q&A Platform

Unexplained CPU Spikes in Node.js Application Using Sequelize with PostgreSQL

👀 Views: 84 đŸ’Ŧ Answers: 1 📅 Created: 2025-09-06
node.js sequelize postgresql performance JavaScript

I'm updating my dependencies and I keep running into I'm integrating two systems and I'm prototyping a solution and I've tried everything I can think of but I'm optimizing some code but I've been banging my head against this for hours. I'm experiencing intermittent CPU spikes in my Node.js application that uses Sequelize as an ORM for PostgreSQL. The application serves a REST API and is deployed on an AWS EC2 instance. During peak usage times, I've noticed CPU usage can spike to 90-95%, causing latency in responding to requests. I've been logging queries and found that some queries take longer than expected, especially those involving complex joins across multiple tables. Here's an example of a query that seems to be causing issues: ```javascript const result = await User.findAll({ include: [{ model: Post, where: { status: 'published' }, include: [{ model: Comment }] }], limit: 100, order: [['createdAt', 'DESC']] }); ``` This query retrieves users along with their published posts and comments. The tables involved are relatively large, with `users` having over 1 million records, `posts` around 2 million, and `comments` at 5 million. When I run this query directly in PostgreSQL, it executes in about 200ms, but in the application, it sometimes takes upwards of 1.5 seconds. I've tried: optimizing indexes on the `posts` and `comments` tables, adding composite indexes, and even using `EXPLAIN ANALYZE` to understand the execution plan. I also utilized Sequelize's `raw` queries to bypass the ORM overhead, which helped a bit but didn't eliminate the spikes entirely. The application is running on Node.js version 14.x and Sequelize version 6.x. I would appreciate any insights into what could be causing these CPU spikes and how to optimize these queries further. Are there any specific patterns or practices I should consider while working with Sequelize and large datasets? I'm working on a application that needs to handle this. This issue appeared after updating to Javascript latest. I'm open to any suggestions. This issue appeared after updating to Javascript 3.10. Has anyone dealt with something similar? I'm developing on Windows 10 with Javascript. I'm working on a web app that needs to handle this. What am I doing wrong? Thanks, I really appreciate it! I've been using Javascript for about a year now.