Skip to content

Interview PrepPerformance · Core

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:

AreaFix
DatabaseIndexes, no N+1, select only needed columns, pooling (slow queries)
Waiting on other servicesRun independent calls in parallel; timeouts; caching
CachingRedis for hot reads; HTTP caching headers (caching API responses)
Event loopMove CPU work to worker threads; no *Sync calls in requests (CPU work)
PayloadsCompression, pagination, smaller JSON, streaming for big files
MiddlewareMount heavy middleware only on routes that need it
Static filesCDN or Nginx, not Express
RuntimeNODE_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)
}))
  1. Skip tiny responses under 1 KB, where compressing costs more than it saves.
  2. 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-Encoding

Why 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.

FormatCompress?Why
JSON, HTML, CSS, JS, SVGYesText with lots of repetition
JPEG, PNG, WebP, MP4, ZIPNoAlready compressed; you only spend CPU
Tiny responsesNoOverhead 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:

SignalWhyAlarm when
p95 and p99 latency per routeUsers feel the slow tailAbove the target for 5 minutes
Error rate, 5xxSomething is failingAbove about 1%
Event loop lagBlocked event loop delays every requestAbove 100 ms
Heap used after garbage collectionMemory leaks show as a rising floorKeeps rising across hours
Database pool waiting countRequests queue for connectionsAbove zero for long
Dependency latency and errorsMost slowness is downstreamAbove 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)
  1. 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:

  1. 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.
  2. Correlate. Line up the start time with deploys, feature flags, traffic peaks, database changes and dependency incidents.
  3. Trace a slow request. The longest span shows where the time goes.
  4. Check the database. Slow-query log, pg_stat_statements, locks in pg_stat_activity, and pool waiting counts.
  5. Check the process. Event loop lag, CPU, memory, garbage collection, and container CPU throttling.
  6. Check dependencies. Their latency, their error rate, and whether your retries are adding load.
SymptomLikely cause
Slow on every endpoint, one instance onlyBad host, noisy neighbour, CPU throttling, a leak on that instance
Slow on one endpointA query or code path; check its trace
Slow at peaks onlyPool exhaustion, CPU limits, lock contention
Slow right after a deployNew query, missing index, N+1, a new synchronous call
p99 spikes every few minutesGarbage collection, a cron job, cache expiring everywhere at once
Gradually slower over daysMemory 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:

  1. Confirm a leak. Plot heapUsed and the container's memory. A sawtooth that returns to the same floor is normal. A floor that rises steadily is a leak.

  2. Take a snapshot safely. Writing a heap snapshot pauses the process and needs extra memory, so take the instance out of rotation first.

    ts
    import { writeHeapSnapshot } from 'node:v8'
    
    process.on('SIGUSR2', () => {
      const file = writeHeapSnapshot()                                          // writes a .heapsnapshot file
      logger.info({ file }, 'heap snapshot written')
    })
  3. Wait, take another, after some traffic.

  4. 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 Map inside sessionCache inside a module.

  5. Fix and verify that the floor stops rising.

LeakExampleFix
Unbounded cacheconst cache = new Map() that only growsLRU with a maximum size, or TTL
Listenersemitter.on('data', …) per request, never removedonce, or off in cleanup
TimerssetInterval per connection, never clearedclearInterval on close
ClosuresA callback stored forever that captures a large bufferKeep only what is needed
Global collectionsPushing 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:

  1. Send SIGUSR1 to the process, or start it with --inspect=127.0.0.1:9229. This opens the inspector on localhost only.
  2. Connect Chrome DevTools to it, through an SSH tunnel if it is remote.
  3. Record a CPU profile for 30 to 60 seconds while the problem happens.
  4. Read the flame graph: wide bars at the top of the stack are where the CPU time goes.
CauseSign in the profileFix
Catastrophic regex backtrackingTime inside RegExp on one inputRewrite the pattern; limit input length (ReDoS)
Huge JSONJSON.parse or stringify widePaginate, stream, smaller payloads
Synchronous crypto or zlibpbkdf2Sync, gzipSyncAsync versions, which use the thread pool
LoggingSerialiser or console.log wideStructured async logger, lower level
Garbage collectionLarge "GC" shareAllocate less, reuse buffers
Hot loopOne of your functions dominatesBetter 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).

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Interview Prep
Production & Operations
Performance
Scaling
Edge
Deploying
Delivery
Observability
Other topics
Nothing picked yet