Express Server Performance Tips for Production
Express in production needs more than in dev. Here are real performance tips.
Express Server Performance Tips for Production
Express is fine out of the box, but production needs more. Here are real performance tips.
1. Use Cluster or PM2
A single Node process uses one CPU core. Use the cluster module or PM2 to spawn one worker per CPU. This scales across cores.
2. Add Compression
Add compression middleware to gzip JSON responses. Cuts payload size and improves perceived performance.
3. Cache Where You Can
Cache expensive queries in Redis. Cache hot API responses with a short TTL. Cache HTTP responses with Cache-Control headers where appropriate.
4. Use Connection Pooling for MongoDB
Mongoose uses pooling. Set poolSize to match expected concurrency (default 100 is usually fine). Avoid one connection per request.
5. Keep Handlers Async
Never block the event loop with sync work. Read files with fs.promises, hash with bcrypt (async), call DB with await. Skip fs.readFileSync in request handlers.
6. Use Lean Queries
If you only need to send JSON, use .lean() on Mongoose queries. Skips hydrating full documents, which is faster.
7. Add Indexes in MongoDB
Index the fields you query and sort by. Without indexes, MongoDB scans the whole collection. Use explain() to verify queries use indexes.
8. Buffer and Stream Large Responses
For large data, stream instead of buffering the whole thing. For file uploads, stream to a destination instead of holding in memory.
9. Limit Payload Size
Set a limit on JSON body parsing: express.json({ limit: '10kb' }). This prevents oversized payload attacks.
10. Use HTTP/2 With Nginx
Behind Nginx, enable HTTP/2. Express itself uses HTTP/1.1, but Nginx in front can use HTTP/2 with the client. Multiplexing improves latency.
11. Use a Process Manager
PM2 keeps the server alive, restarts on crash, and lets you reload without downtime. Run the server under PM2 in production.
12. Monitor Event Loop Lag
Use tools like clinic.js or the built-in --inspect to measure event loop lag. If lag grows, something is blocking. Find and fix it.
The Takeaway
Express production performance: use cluster or PM2, add compression, cache with Redis, use DB connection pooling, keep handlers async, use lean queries, add indexes, stream large responses, limit payload size, use HTTP/2 with Nginx, use a process manager, and monitor event loop lag.
Use cluster or PM2, add compression middleware, cache with Redis, use DB connection pooling, keep handlers async, use Mongoose lean queries, add indexes in MongoDB, stream large responses, limit payload size, and use HTTP/2 with Nginx.
PM2 spawns one worker per CPU, restarts on crash, and lets you reload without downtime. It keeps the server alive and uses all available cores. PM2 is a small addition with big production payoff.
It skips hydrating full Mongoose documents and returns plain JS objects. Faster when you only need to send JSON to the client, since you skip building model instances and virtuals.
express.json({ limit: '10kb' }) prevents oversized payload attacks. Without a limit, a malicious client can send huge bodies, eating memory and slowing the server down.
Use PM2 monit, clinic.js for profiling, or Application Performance Monitoring like New Relic or Datadog. Watch event loop lag, response times, and error rates. If lag grows, something is blocking the event loop.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

