Skip to main content

Module 1: Core Concepts

Latency vs Throughput

Master latency and throughput metrics — p50/p99 latency, QPS, Little’s Law, and how caching, batching, and queues change both.

Latency is how long one request waits. Throughput is how many requests the system finishes per second. Interviews care which SLA you optimize — and whether your fix accidentally hurts the other metric.

What you will learn

  • Define latency vs throughput in interview language
  • Use p50/p95/p99 instead of averages that hide tails
  • Relate QPS, concurrency, and service time with Little’s Law intuition
  • Name levers that cut latency vs levers that raise throughput
  • Explain saturation: when queues grow, latency explodes and throughput plateaus
Left: one request waits in a queue before service. Right: a chart where latency climbs after the saturation knee.
Latency is one request. Throughput is completions per second. Past the knee, wait time explodes.

What each metric means

  • Latency answers “how long did this user wait?” Throughput answers “how much work did we finish per second?”
  • Always ask which one the interviewer is optimizing before you redraw the architecture.
  • Latency — time from request start to response end (client-perceived or server-side)
  • Throughput — completed work per time: QPS/RPS, messages/sec, or bytes/sec for streaming
  • They are related but not the same — a slow system can still push high throughput with huge concurrency
  • SLA language: “p99 under 200 ms” is a latency goal; “1M events/hour” is a throughput goal

Latency in depth

  • Measure what the user feels, then decompose into network RTT, queue wait, and service time.
  • Client-perceived latency includes DNS, TLS, RTT, and browser/app overhead
  • Server latency usually means arrival → response leave (plus any sync downstream calls)
  • Queueing delay often dominates once the system nears capacity
  • Sync fan-out (calling many services in series) stacks latencies end-to-end

Percentiles beat averages

  • Averages hide outages. A few slow requests can ruin UX while the mean still looks fine.
  • p50 — typical experience for half of requests
  • p95 / p99 — tail experience; this is what SLOs usually guard
  • Max latency is noisy — prefer high percentiles with enough sample size
  • Report the window (last 5 minutes / 1 hour) so spikes are interpretable

Where tail latency comes from

  • Tail latency is rarely “the happy-path code is slow.” It is variance: occasional stalls that push p99 up.
  • GC pauses, stop-the-world events, or long JIT/warmup cold starts
  • Lock contention, hot rows, or single-threaded bottlenecks
  • Cold caches, compulsory misses, or thundering herds after deploy
  • Noisy neighbors, disk I/O stalls, or saturated connection pools
  • Retries amplifying load during partial outages (retry storms)

Throughput in depth

  • Throughput is capacity to finish work. Raise it by doing more work in parallel or less work per request.
  • QPS/RPS for request/response APIs; messages/sec for queues; MB/s for media pipelines
  • Useful capacity = peak sustainable throughput before SLOs break — not a short lab spike
  • Efficiency matters: better indexes or caching can raise throughput without new machines
  • Backpressure matters: unlimited accept rate without processing just builds an infinite queue

Little’s Law intuition

  • Little’s Law: concurrency (L) ≈ throughput (λ) × latency (W). Use it as a sanity check, not a proof.
  • If QPS stays fixed and latency doubles, in-flight requests roughly double
  • Thread/connection pools must cover peak concurrency or requests queue outside the pool
  • Higher concurrency can hide latency problems until pools and memory melt
  • Interview use: estimate “how many open requests?” from target QPS × p99 latency

Latency vs throughput trade-offs

  • Many “optimizations” help one metric and hurt the other. Call the trade-off out loud.
  • Batching — higher throughput, often higher per-item wait
  • Sync replication — stronger correctness, higher write latency
  • Heavy compression — fewer bytes on the wire, more CPU latency per request
  • Giant fan-out joins — richer response, worse p99
  • Oversized thread pools — can raise throughput briefly, then thrash and raise latency

Levers that cut latency

  • Prefer removing wait from the critical path before buying more hardware.
  • Cache hot reads (Redis/CDN) and keep TTLs honest
  • Move users closer — regional deploy, edge/CDN for static and media
  • Avoid sync fan-out; parallelize independent calls or make non-critical work async
  • Pool DB/HTTP connections; fix N+1 queries and missing indexes
  • Trim payloads — pagination, field masks, compression where CPU allows

Levers that raise throughput

  • Throughput grows when each machine does more useful work per second, or you add more machines that share load.
  • Scale out stateless app tiers behind a load balancer
  • Offload heavy work to queues and workers that scale independently
  • Batch writes / bulk inserts where correctness allows
  • Sharpen indexes, reduce lock scope, and avoid full-table scans
  • Shard or partition when a single primary cannot absorb write QPS

Saturation and queues

  • Below capacity, latency stays flat-ish. Past saturation, queues grow: throughput plateaus while latency explodes.
  • Watch utilization (CPU, disk, pool wait) alongside QPS and p99
  • A full queue is a latency bomb — shed load, rate-limit, or scale before it fills
  • Timeouts without backoff create retry storms that destroy remaining throughput
  • Good interview diagram: arrival rate → queue → service; mark the knee where p99 breaks

Interview framing

  • Use this order when metrics come up in a design round.
  • Ask for the target: p99 latency, peak QPS, payload size, read/write mix
  • Estimate a rough capacity story (QPS × latency → concurrency)
  • Propose the smallest change that moves the named metric
  • Call out the metric you might worsen (e.g. batching helps throughput, hurts latency)
  • Define how you would measure success after the change

Quick revision checklist

  • Quote p99 (and the window), not only the average.
  • Separate client RTT from server processing time.
  • Ask whether the goal is UX latency or pipeline throughput.
  • Use Little’s Law to size pools: concurrency ≈ QPS × latency.
  • Name the trade-off when you batch, compress, or replicate sync.
  • Talk about the saturation knee — queues, not just “add CPUs.”

FAQs

Can high throughput and low latency coexist?

Yes while the system has spare capacity. Past saturation, queues grow: throughput plateaus and latency explodes. Design for the knee, not the lab spike.

Is QPS the same as concurrency?

No. QPS is completions per second. Concurrency is how many requests are in flight. Roughly concurrency ≈ QPS × latency (Little’s Law).

Why is p99 more important than average?

Users feel the slow requests. Averages hide a painful tail from GC, locks, cold caches, or retries. SLOs usually guard high percentiles.

Does batching always help?

Batching often raises throughput and efficiency, but each item may wait to fill a batch — so per-item latency can rise. Use it when delay is acceptable.

How do caching and CDN affect both metrics?

Cache/CDN hits cut work and network distance, so latency drops and origin throughput demand falls. Misses and stampedes can still spike both.

What should I optimize first in a user-facing API?

Usually p95/p99 latency on the critical path. Raise throughput next if you are approaching saturation or failing peak QPS targets.

Practice

Learner’s Task

  • Practice out loud for 8–10 minutes. Treat this like a metrics-first design drill.
  1. Pick an API (login, feed fetch, or image upload) and state a target: peak QPS + p99 latency + rough payload size.
  2. Estimate in-flight concurrency with Little’s Law (QPS × latency) and say what pool size that implies.
  3. List two changes that cut latency and two that raise throughput — mark any trade-off each change introduces.
  4. Draw arrival → queue → service and mark the saturation knee where p99 breaks.
  5. Write a 4-sentence closing: goal metric, current bottleneck, proposed lever, and the metric you might worsen.