Appearance
Performance and Diagnosis
1. How do you optimize an Express application?
The short answer
Measure first, then fix the biggest cost. For most Express APIs, time goes to I/O, not JavaScript: slow or too many database queries, missing indexes, N+1 queries, and slow external calls. So: fix queries and indexes, cache hot reads, run independent calls in parallel with Promise.all, use connection pooling, and set timeouts. Then the Node side: never block the event loop with CPU work or synchronous calls, keep middleware lean, stream large responses, compress text responses, serve static files from a CDN, set NODE_ENV=production, and scale across cores with several processes.
The checklist, ordered by usual payoff:
| Area | Fix |
|---|---|
| Database | Indexes, no N+1, select only needed columns, pooling (slow queries) |
| Waiting on other services | Run independent calls in parallel; timeouts; caching |
| Caching | Redis for hot reads; HTTP caching headers (caching API responses) |
| Event loop | Move CPU work to worker threads; no *Sync calls in requests (CPU work) |
| Payloads | Compression, pagination, smaller JSON, streaming for big files |
| Middleware | Mount heavy middleware only on routes that need it |
| Static files | CDN or Nginx, not Express |
| Runtime | NODE_ENV=production, current Node LTS, one process per core |
Parallel instead of one after another:
ts
// sequential: about 120 + 90 + 60 = 270 ms
const user = await users.get(id)
const orders = await orders.recent(id)
const recs = await recommendations.for(id)
// parallel: about max(120, 90, 60) = 120 ms
const [user, orders, recs] = await Promise.all([users.get(id), orders.recent(id), recommendations.for(id)])The three calls do not depend on each other, so waiting for each before starting the next wastes time.
NODE_ENV=production matters. Express caches view templates and produces less verbose errors, and many libraries skip development-only checks when it is set.
2. What is compression middleware, and how does gzip compression work?
The short answer
Compression middleware, the compression package in Express, compresses response bodies before sending them, when the client's Accept-Encoding header says it supports it, and sets Content-Encoding so the browser decompresses. gzip uses the DEFLATE algorithm: it replaces repeated byte sequences with short back-references to earlier copies, then encodes the result with Huffman coding, which gives common symbols shorter codes. JSON and HTML are very repetitive, so they often shrink by 70 to 90%. Brotli usually compresses text better still. Compress text formats only, not images or video, and in production often let Nginx or the CDN do it.
ts
import compression from 'compression'
app.use(compression({
threshold: 1024, // (1)
filter: (req, res) => req.headers['x-no-compression'] ? false : compression.filter(req, res), // (2)
}))- Skip tiny responses under 1 KB, where compressing costs more than it saves.
- Use the default filter, which only compresses compressible content types such as JSON, HTML, CSS and JavaScript, with an escape hatch.
What happens on the wire:
http
GET /api/products HTTP/1.1
Accept-Encoding: br, gzip, deflate
HTTP/1.1 200 OK
Content-Type: application/json
Content-Encoding: gzip
Vary: Accept-EncodingWhy Vary: Accept-Encoding matters. It tells caches and CDNs to keep separate copies for clients that do and do not support compression. Without it, a CDN could serve gzip bytes to a client that cannot decode them.
| Format | Compress? | Why |
|---|---|---|
| JSON, HTML, CSS, JS, SVG | Yes | Text with lots of repetition |
| JPEG, PNG, WebP, MP4, ZIP | No | Already compressed; you only spend CPU |
| Tiny responses | No | Overhead exceeds savings |
CPU cost. Compression uses CPU on every response. On a busy service, compress at the edge, in Nginx or the CDN, or pre-compress static assets at build time with Brotli. The compression package supports Brotli since version 1.8.
3. How do you monitor and profile production performance?
The short answer
Monitor continuously with metrics: request rate, error rate and latency percentiles, p50, p95 and p99, per endpoint, plus event loop lag, CPU, memory, garbage collection, database pool usage and dependency latency. Trace requests across services to see where time goes. Then profile when something is wrong: capture a CPU profile or flame graph with the Node inspector, --cpu-prof, or a continuous profiler, and heap snapshots for memory. Compare against a baseline, profile under realistic load, and change one thing at a time.
What to watch in a Node service:
| Signal | Why | Alarm when |
|---|---|---|
| p95 and p99 latency per route | Users feel the slow tail | Above the target for 5 minutes |
| Error rate, 5xx | Something is failing | Above about 1% |
| Event loop lag | Blocked event loop delays every request | Above 100 ms |
| Heap used after garbage collection | Memory leaks show as a rising floor | Keeps rising across hours |
| Database pool waiting count | Requests queue for connections | Above zero for long |
| Dependency latency and errors | Most slowness is downstream | Above the dependency's normal |
Event loop lag, measured inside the app:
ts
import { monitorEventLoopDelay } from 'node:perf_hooks'
const loop = monitorEventLoopDelay({ resolution: 20 })
loop.enable()
setInterval(() => {
metrics.gauge('event_loop_lag_p99_ms', loop.percentile(99) / 1e6) // (1)
loop.reset()
}, 10_000)- The 99th-percentile delay, in nanoseconds, converted to milliseconds. A healthy service stays in single digits.
Why percentiles, not averages. If 99 requests take 50 ms and one takes 5 seconds, the average is about 100 ms and looks fine. The p99 is 5 seconds, which is what that unlucky user felt.
Profiling tools: node --cpu-prof writes a .cpuprofile you open in Chrome DevTools; Clinic.js and 0x make flame graphs; continuous profilers such as Pyroscope or Datadog sample production all the time with little overhead (profiling Node).
4. How would you troubleshoot high latency in an API?
The short answer
Narrow it down step by step. Check which requests are slow, by endpoint, customer, region and instance, and when it started, against deploys, traffic and config changes. Use traces to see which span takes the time: the database, a downstream API, a lock wait, or your own code. Then check the usual suspects: slow queries and missing indexes, connection pool exhaustion, a blocked event loop, garbage collection pauses, CPU or memory limits, cold caches, retries and timeouts in a dependency, and network or DNS issues. Fix the cause, then confirm the p99 is back.
A diagnosis order that finds most problems:
- Scope. All endpoints or one? All instances or one? Since when? A single instance points at that host; one endpoint points at its code or queries; everything at once points at a shared dependency or a deploy.
- Correlate. Line up the start time with deploys, feature flags, traffic peaks, database changes and dependency incidents.
- Trace a slow request. The longest span shows where the time goes.
- Check the database. Slow-query log,
pg_stat_statements, locks inpg_stat_activity, and pool waiting counts. - Check the process. Event loop lag, CPU, memory, garbage collection, and container CPU throttling.
- Check dependencies. Their latency, their error rate, and whether your retries are adding load.
| Symptom | Likely cause |
|---|---|
| Slow on every endpoint, one instance only | Bad host, noisy neighbour, CPU throttling, a leak on that instance |
| Slow on one endpoint | A query or code path; check its trace |
| Slow at peaks only | Pool exhaustion, CPU limits, lock contention |
| Slow right after a deploy | New query, missing index, N+1, a new synchronous call |
| p99 spikes every few minutes | Garbage collection, a cron job, cache expiring everywhere at once |
| Gradually slower over days | Memory leak, growing table without an index |
5. How do you diagnose memory leaks in production?
The short answer
A leak shows as heap memory that keeps rising after garbage collection, until the process is killed for running out of memory or slows down from constant garbage collection. Confirm it with metrics over hours. Then capture two or three heap snapshots some time apart, from one instance taken out of the load balancer, and compare them in Chrome DevTools to find which object types keep growing and what is holding them, the retainers. The usual causes are caches with no size limit, objects added to module-level arrays or maps, event listeners that are never removed, timers that are never cleared, and closures holding large data.
Steps, in order:
Confirm a leak. Plot
heapUsedand the container's memory. A sawtooth that returns to the same floor is normal. A floor that rises steadily is a leak.Take a snapshot safely. Writing a heap snapshot pauses the process and needs extra memory, so take the instance out of rotation first.
tsimport { writeHeapSnapshot } from 'node:v8' process.on('SIGUSR2', () => { const file = writeHeapSnapshot() // writes a .heapsnapshot file logger.info({ file }, 'heap snapshot written') })Wait, take another, after some traffic.
Compare in DevTools' Memory tab, using the "Comparison" view, sorted by size delta. Click the growing type and read its retainers: the chain of references keeping it alive, such as
MapinsidesessionCacheinside a module.Fix and verify that the floor stops rising.
| Leak | Example | Fix |
|---|---|---|
| Unbounded cache | const cache = new Map() that only grows | LRU with a maximum size, or TTL |
| Listeners | emitter.on('data', …) per request, never removed | once, or off in cleanup |
| Timers | setInterval per connection, never cleared | clearInterval on close |
| Closures | A callback stored forever that captures a large buffer | Keep only what is needed |
| Global collections | Pushing every request into an array for "debugging" | Remove it |
Node also accepts --heapsnapshot-near-heap-limit=2, which writes snapshots automatically just before the process runs out of memory (memory leaks in Node).
6. How do you debug unexpectedly high CPU usage?
The short answer
In Node, high CPU on the main thread means JavaScript is busy and every request waits. Find what code is burning it with a CPU profile: run with --cpu-prof, attach the inspector to a live process, or use a continuous profiler, and read the flame graph, where the widest bars are the functions using the most time. Common causes are expensive regular expressions, huge JSON.parse or JSON.stringify calls, synchronous crypto or compression, tight loops over large arrays, excessive logging, and garbage collection pressure from allocating too much. Move unavoidable CPU work to worker threads or another service.
Profiling a running process without restarting it:
- Send
SIGUSR1to the process, or start it with--inspect=127.0.0.1:9229. This opens the inspector on localhost only. - Connect Chrome DevTools to it, through an SSH tunnel if it is remote.
- Record a CPU profile for 30 to 60 seconds while the problem happens.
- Read the flame graph: wide bars at the top of the stack are where the CPU time goes.
| Cause | Sign in the profile | Fix |
|---|---|---|
| Catastrophic regex backtracking | Time inside RegExp on one input | Rewrite the pattern; limit input length (ReDoS) |
| Huge JSON | JSON.parse or stringify wide | Paginate, stream, smaller payloads |
| Synchronous crypto or zlib | pbkdf2Sync, gzipSync | Async versions, which use the thread pool |
| Logging | Serialiser or console.log wide | Structured async logger, lower level |
| Garbage collection | Large "GC" share | Allocate less, reuse buffers |
| Hot loop | One of your functions dominates | Better algorithm; worker thread |
Never expose the inspector publicly. Anyone who connects can run any code in the process. Bind it to 127.0.0.1 and reach it through a tunnel (high CPU in Node).