Key Takeaways
- Database indexes alone cut query time by 70%
- Redis caching eliminated redundant database calls
- Promise.all() for independent operations saved 400ms per request
- Connection pooling prevented connection exhaustion under load
The Performance Challenge
Our API was struggling with response times averaging 800ms. After optimization, we brought this down to 300ms. Here's how.
Why This Matters
In production, every 100ms of latency costs engagement. At scale, inefficient APIs compound into infrastructure costs and poor user experience.
1. Database Query Optimization
Use Indexes Wisely
// Before: Full table scan
db.users.find({ email: "user@example.com" });
// After: Create an index
db.users.createIndex({ email: 1 });
Select Only What You Need
// Bad: Fetching entire documents
const users = await User.find({});
// Good: Projection
const users = await User.find({}, { name: 1, email: 1 });
2. Implement Caching
We used Redis for multi-level caching:
const Redis = require('ioredis');
const redis = new Redis();
async function getUserWithCache(userId) {
const cacheKey = "user:" + userId;
const cached = await redis.get(cacheKey);
if (cached) return JSON.parse(cached);
const user = await User.findById(userId);
await redis.setex(cacheKey, 300, JSON.stringify(user));
return user;
}
3. Parallelize Independent Operations
// Bad: Sequential, 1200ms total
const user = await getUser(id);
const posts = await getPosts(id);
const notifications = await getNotifications(id);
// Good: Parallel, 400ms total
const [user, posts, notifications] = await Promise.all([
getUser(id),
getPosts(id),
getNotifications(id)
]);
Results
| Metric | Before | After |
|---|---|---|
| Avg Response Time | 800ms | 300ms |
| P95 Latency | 2s | 500ms |
| Throughput | 500 req/s | 2000 req/s |
Key Lessons
- Measure first: Use APM tools to identify actual bottlenecks
- Cache aggressively: But invalidate correctly
- Think parallel: Most operations can run concurrently
- Monitor continuously: Performance degrades over time without attention
Performance optimization is iterative. Start with the biggest wins and work your way down.
💡 Strategic Insight
This isn't just technical knowledge — it's the kind of engineering thinking that separates production systems from toy projects. Apply these patterns to reduce costs, improve reliability, and ship faster.
Frequently Asked Questions
Use APM tools like New Relic, Datadog, or open-source alternatives like Clinic.js. Focus on P95 latency, not just averages.
No. Cache data that's read frequently and updated rarely. Always implement proper cache invalidation strategies.
Tagged with
TL;DR
- Database indexes alone cut query time by 70%
- Redis caching eliminated redundant database calls
- Promise.all() for independent operations saved 400ms per request
- Connection pooling prevented connection exhaustion under load
Need help implementing this?
I help teams architect scalable systems, build AI-powered applications, and ship production-ready software.

Written by
Gaurav Garg
Full Stack & AI Developer · Building scalable systems
I write engineering breakdowns of major tech events, architecture deep dives, and practical guides based on real production experience. Every post is built from code, not theory.
7+
Articles
5+
Yrs Exp.
500+
Readers
Get tech breakdowns before everyone else
Engineering insights on AI, cloud, and modern architecture — delivered when it matters. No spam.
Join 500+ engineers. Unsubscribe anytime.



