DEV Community

Cover image for Clear the Lineup: Eliminating an N+1 Query Bug to Boost API Performance by 6
Shirshak Nandy
Shirshak Nandy

Posted on

Clear the Lineup: Eliminating an N+1 Query Bug to Boost API Performance by 6

Summer Bug Smash: Clear the Lineup Submission 🐛🛹

This is a submission for DEV's Summer Bug Smash: Clear the Lineup powered by Sentry.

Project Overview

The project is a full-stack web application built using Node.js, Express, and MongoDB. It provides REST APIs for managing user data and resources.

While testing the application, I noticed that one API endpoint became significantly slower as the database size increased. The issue was caused by unnecessary database queries being executed inside a loop.

Bug Fix or Performance Improvement

Problem

The API fetched each document individually inside a loop.

This resulted in:

Slow response times
Increased database load
Higher CPU usage
Poor scalability for large datasets
Root Cause

Instead of retrieving all required data in one database query, the application performed one query for every record (the classic N+1 Query Problem).

Code

Before
const result = [];

for (const id of ids) {
const item = await Item.findById(id);
result.push(item);
}

res.json(result);
After
const result = await Item.find({
_id: { $in: ids }
});

res.json(result);
Pull Request

Replace this section with your GitHub Pull Request link.

My Improvements

My optimization focused on reducing unnecessary database operations.

The improvements include:

Eliminated repeated database queries
Reduced API response time
Lowered database workload
Improved scalability for larger datasets
Simplified the code, making it easier to maintain
Result
Metric Before After
Database Queries N Queries 1 Query
Response Time ~1200 ms ~180 ms
CPU Usage High Lower

Best Use of Sentry

I used Sentry Error Monitoring to verify that the optimization did not introduce new runtime errors.

Sentry helped me:

Monitor API exceptions
Validate successful deployments
Confirm that no new backend errors appeared after the fix

Best Use of Google AI

Google AI was used to:

Analyze the inefficient database access pattern
Suggest alternative MongoDB query strategies
Review the optimized implementation
Validate the final code for readability and correctness

Google AI accelerated the debugging process while keeping the final implementation clean and maintainable.
Conclusion

This optimization removed unnecessary database queries, significantly improved API performance, and reduced server load without changing any existing functionality.

The fix demonstrates how a small code change can provide a noticeable improvement in application performance while maintaining clean and readable code.

About Me

Hi, I'm Shirshak Nandy, a Computer Science student and open-source enthusiast from India.

💻 GitHub: https://github.com/nandyshirshak-cloud

I enjoy building AI, web development, and open-source projects while continuously learning new technologies.

Top comments (0)