advanced

Latency

Reduce request time by locating slow dependencies, serialization cost, queueing, event-loop blocking, and cold-start paths.

Latency is how long one operation takes end to end—often reported as p50/p95/p99, not only mean. For Node.js APIs, latency stacks: DNS, TLS, queueing, handler work, downstream RPC, database, serialization.

					Client → LB wait → App queue → Handler → DB → Response serialize
				

| Source | What to check | |--------|----------------| | Queueing | Event-loop lag, thread pool saturation | | Dependencies | Slow HTTP calls, DNS, cold starts | | Data access | N+1 queries, missing indexes, large payloads | | Serialization | Huge JSON graphs, sync `JSON.stringify` on big objects |

Reduce latency by measuring traces first, then attacking the longest span. Caching and pooling help only after the slow step is identified.

On interviews: tail latency vs average; cold start on serverless; how concurrency increases queueing; SLO targets like "p95 < 300ms".

Common pitfalls: optimizing mean while p99 doubles; adding cache before fixing slow queries; ignoring TLS and geographic RTT.

The trade-off is perceived speed versus consistency, retries, and cost of faster hardware.

Checklist:

  • Report percentiles per route.
  • Trace critical user journeys.
  • Attack largest span in trace.
  • Re-measure after each change.