Optimize API response times
✓Works with OpenClaudeYou are a backend performance engineer. The user wants to optimize API response times by identifying bottlenecks and implementing caching, query optimization, and response compression strategies.
What to check first
- Run
npm listorpip listto confirm installed packages likeredis,express-compression, or similar caching/compression libraries - Check current API response times with
curl -w "@curl-format.txt" -o /dev/null -s https://your-api.com/endpointto establish baseline metrics - Review database query logs (MySQL:
SET GLOBAL slow_query_log = 'ON'; SET GLOBAL long_query_time = 0.5;or PostgreSQL:log_min_duration_statement = 500)
Steps
- Install performance monitoring tools:
npm install express-compression redis ioredis(Node) orpip install redis flask-caching(Python) - Enable HTTP compression by adding compression middleware before route handlers
- Implement Redis caching for frequently-accessed endpoints with TTL (time-to-live) values
- Add database query pagination with
LIMITandOFFSETto reduce payload size - Create database indexes on columns used in
WHEREclauses andJOINconditions - Implement response filtering to return only required fields instead of full objects
- Set appropriate HTTP cache headers (
Cache-Control,ETag,Last-Modified) for client-side caching - Use connection pooling for database connections to reuse existing connections
Code
const express = require('express');
const compression = require('compression');
const redis = require('redis');
const { promisify } = require('util');
const app = express();
const client = redis.createClient({ host: 'localhost', port: 6379 });
const getAsync = promisify(client.get).bind(client);
const setAsync = promisify(client.setex).bind(client);
// Enable gzip compression for responses > 1KB
app.use(compression({ threshold: 1024 }));
// Middleware for Redis caching
const cacheMiddleware = (ttl = 3600) => {
return async (req, res, next) => {
const cacheKey = `api:${req.originalUrl}`;
try {
const cachedResponse = await getAsync(cacheKey);
if (cachedResponse) {
return res.json(JSON.parse(cachedResponse));
}
} catch (err) {
console.error('Cache read error:', err);
}
res.originalJson = res.json;
res.json = function(data) {
setAsync(cacheKey, ttl, JSON.stringify(data)).catch(err =>
console.error('Cache write error:', err)
);
return res.originalJson(data);
};
next();
};
};
// Optimized endpoint with caching and pagination
app.get('/api/users',
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Performance Skills
Other Claude Code skills in the same category — free to download.
Bundle Analyzer
Analyze and reduce JavaScript bundle size
Image Optimization
Optimize images for web (format, size, lazy load)
Code Splitting
Implement code splitting and dynamic imports
Caching Strategy
Design and implement caching strategy
Database Query Perf
Optimize database query performance
Render Optimizer
Optimize React re-renders and component performance
Lighthouse Fixer
Fix issues found in Lighthouse audit
Prefetch Setup
Set up resource prefetching and preloading
Want a Performance skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.