Two different currencies
Latency is how long ONE request takes, measured in milliseconds. Throughput is how MANY requests the system finishes per second. They are related but not the same: a cargo ship has huge throughput and terrible latency; a sports car is the opposite. Systems usually optimize one at the expense of the other.
The latency numbers every engineer should know
Order-of-magnitude figures (classic "Numbers Every Programmer Should Know", still accurate enough for design):
- L1/L2 cache reference: ~1-10 ns
- Main memory (RAM) reference: ~100 ns
- Read 1 MB sequentially from RAM: ~0.03 ms; from SSD: ~1 ms
- SSD random read: ~0.1 ms; HDD seek: ~10 ms
- Round trip inside one datacenter: ~0.5 ms
- Round trip cross-continent: ~150 ms
The one-line summary: RAM is ~1000x faster than disk; the network is slower than both. Every caching, CDN and batching decision follows from this table.
Latency: averages lie, percentiles tell the truth
An average (p50) latency of 50 ms can hide a p99 of 2 seconds: 1 in 100 users waits 2s. At scale, the tail IS the product. Why p99 blows up: one request often fans out to many internal calls; if each has a 1% chance of being slow, a request touching 100 services has a ~63% chance of hitting at least one slow call (1 - 0.99^100). Fixes: hedged/parallel requests, strict timeouts, cutting optional work at high load.
Throughput: where it caps
Throughput is set by the slowest saturated resource: CPU, DB connections, disk IOPS, or network. Example: if each request needs 10 ms of CPU on one core, one core gives 100 RPS; a 16-core server gives at most 1,600 RPS, and in practice less (context switches, GC, lock contention). This is also Little's Law in disguise: concurrency = throughput x latency. At 1,000 RPS with 100 ms average latency, ~100 requests are in flight at all times, and your thread/connection pools must be sized for it.
The classic tradeoff: batching
Processing items one by one: low latency, low throughput (every item pays fixed overhead). Batching 100 items: overhead is paid once, so throughput jumps, but items wait for the batch to fill, so latency rises. Kafka producers (linger.ms), database bulk inserts and video encoding all live on this dial.
Worked example
"Design an API at 10,000 RPS." One app server handles ~2,000 RPS of this workload, so ~6 servers plus headroom behind an LB. Average latency target 100 ms means ~1,000 concurrent requests in flight (Little's Law): set pool sizes accordingly, and set p99 budgets for each downstream call (cache 1 ms, DB 5 ms) so the total fits.
Common interview traps
- Quoting average latency when the question is about user experience (use p95/p99).
- Assuming latency improves by adding servers: it fixes throughput, not per-request latency. Per-request latency improves with caching, better algorithms, less work.
- Ignoring fan-out amplification on tail latency.
Interview Q&A
Q: p99 latency doubled after traffic doubled. Why?
A: Some shared resource (DB pool, CPU, queue) crossed a saturation knee: queuing delay grows non-linearly near 100% utilization. Find the saturated resource (metrics), add capacity or cut work (cache, batch, drop optional calls), and it snaps back.
Q: How do you raise throughput without touching latency?
A: Add more parallel workers/servers behind the LB: each request still takes the same time, but more are processed concurrently.
Do alag currencies
Latency = EK request ko kitna time laga, milliseconds mein. Throughput = system per second KITNE requests khatam karta hai. Dono related hain par same nahi: cargo ship ka throughput zabardast, latency bekaar; sports car ulta. Systems usually ek optimize karte hain doosre ki keemat par.
Latency numbers jo har engineer ko yaad hone chahiye
Order-of-magnitude figures (classic "Numbers Every Programmer Should Know", design ke liye aaj bhi kaafi accurate):
- L1/L2 cache reference: ~1-10 ns
- Main memory (RAM) reference: ~100 ns
- RAM se 1 MB sequential read: ~0.03 ms; SSD se: ~1 ms
- SSD random read: ~0.1 ms; HDD seek: ~10 ms
- Ek datacenter ke andar round trip: ~0.5 ms
- Cross-continent round trip: ~150 ms
Ek line ka summary: RAM disk se ~1000x faster hai; network dono se slow hai. Har caching, CDN aur batching decision isi table se nikalta hai.
Latency: average jhoot bolta hai, percentiles sach
Average (p50) latency 50 ms ho sakti hai jabki p99 2 seconds ho: har 100 mein se 1 user 2s wait karta hai. Scale par tail hi product hai. p99 kyun phoolta hai: ek request aksar andar andar kayi calls karti hai; agar har call ka 1% slow chance hai, to 100 services chhoone wali request ka ~63% chance hai kam se kam ek slow call pakadne ka (1 - 0.99^100). Fixes: hedged/parallel requests, strict timeouts, high load par optional work kaatna.
Throughput: cap kahan lagta hai
Throughput sabse slow saturated resource set karta hai: CPU, DB connections, disk IOPS, ya network. Example: har request ek core par 10 ms CPU leti hai, to ek core 100 RPS; 16-core server max 1,600 RPS, aur practically usse kam (context switches, GC, locks). Yehi Little's Law hai: concurrency = throughput x latency. 1,000 RPS aur 100 ms average latency par ~100 requests har waqt in-flight hain, aur thread/connection pools itne size ke hone chahiye.
Classic tradeoff: batching
Ek-ek item process karo: latency kam, throughput kam (har item fixed overhead deta hai). 100 items ka batch: overhead ek baar, throughput jump, par items batch bharne ka wait karte hain, latency badhti hai. Kafka producers (linger.ms), DB bulk inserts, video encoding, sab isi dial par hain.
Worked example
"10,000 RPS ka API design karo." Ek app server is workload ke ~2,000 RPS deti hai, to ~6 servers + headroom LB ke peeche. 100 ms latency target ka matlab ~1,000 concurrent requests in-flight (Little's Law): pool sizes waise set karo, aur har downstream call ka p99 budget banao (cache 1 ms, DB 5 ms) taaki total fit ho.
Common interview traps
- User experience ki baat ho rahi ho aur average latency quote karna (p95/p99 bolo).
- Maan lena ki servers add karne se latency sudharti hai: wo throughput fix karta hai, per-request latency nahi. Per-request latency caching, better algorithms, kam kaam se sudharti hai.
- Tail latency par fan-out ka effect ignore karna.
Interview Q&A
Q: Traffic double hua aur p99 latency double ho gayi. Kyun?
A: Koi shared resource (DB pool, CPU, queue) saturation knee cross kar gaya: 100% utilization ke paas queuing delay non-linearly badhta hai. Saturated resource dhundo (metrics), capacity badhao ya kaam kaato (cache, batch, optional calls drop), aur ye wapas normal ho jaata hai.
Q: Latency chhue bina throughput kaise badhao?
A: LB ke peeche parallel workers/servers badhao: har request utna hi time leti hai, par ek saath zyada process hoti hain.