Q&A-2Explain the difference between throughput and latency using a concrete example, and describe a scenario where optimizing one hurts the other.
Answer
First 90 seconds
Throughput and latency are two axes on the same system, and Little's Law ties them: concurrency = throughput * latency. With a fixed number of requests in flight they move in strict opposite directions, so "optimizing one hurts the other" usually just means you moved along that curve. The more interesting case is changing the per-request fixed cost, which shifts the whole curve: that's what batching does. Numbers below are illustrative, not measured.
Assume an event-ingest API in front of Postgres, 8 connections in the pool, one INSERT in flight per connection. Each event row costs 0.1 ms of DB work; a round trip costs 2 ms no matter how many rows it carries. Those two constants drive everything.
Unbatched, one event per INSERT: 2.1 ms each. One connection sustains 1 / 0.0021 s ~ 476 events/s; the pool ~3,810/s. p50 is ~2.5 ms with no backlog; when a burst queues all 8 connections, p99 ~15 ms.
Batched at 100 events per INSERT behind a 20 ms accumulation window: 2 ms + 100 * 0.1 ms = 12 ms per INSERT, so 100 / 0.012 ~ 8,333 events/s per connection and ~66,600/s for the pool. An event arriving just after a flush waits a full window: p50 ~ 10 ms (uniform arrivals, half a window on average) + 12 ms = 22 ms, p99 ~ 32 ms.
| config | events per INSERT | service time | pool throughput | p50 | p99 |
|---|---|---|---|---|---|
| unbatched | 1 | 2.1 ms | ~3,810/s | ~2.5 ms | ~15 ms |
| batch 100, 20 ms window | 100 | 12 ms | ~66,600/s | ~22 ms | ~32 ms |
17.5x the throughput, 8.8x the p50. Same change, opposite directions, because the 2 ms round trip went from covering one event to covering 100: service time per event fell from 2.1 ms to 0.12 ms.
Why this isn't just an accounting identity: at 8 connections Little's Law gives throughput = 8 / latency, so any latency increase at fixed concurrency is a straight throughput loss. Batching escapes that only by cutting service time; the window is the pure added wait you pay for it. The other lever is concurrency: go to 20 connections and you get 20 / 0.0021 ~ 9,524 events/s at ~2.5 ms p50, but DB CPU rises from 8 * 0.1/2.1 ~ 38% to 20 * 0.1/2.1 ~ 95%, so queue wait starts eating the gain. The choice is: spend latency (batch) or spend DB CPU (more connections), and they saturate in different places.
Pipelining is the third option: libpq pipeline mode (added in Postgres 14), or pgx's SendBatch, lets a client send inserts back-to-back without waiting for each response, which removes round-trip serialization with no accumulation window and keeps p50 near 2.5 ms. Batching is the wrong call when the SLO is interactivity (a read path with p99 under 20 ms), when load is too low for the window to amortize anything, and when per-key ordering matters, since a batch can fail or partially apply.
Failure modes I'd watch:
- Timer restarted on every arrival: at 50 events/s a 100-event batch never fills and the timer never fires, so every event waits 100/50 = 2 s. Detect it with a batch-size histogram sitting below max while p99 tracks a window-derived value. Fix: start the timer on the first arrival in the window.
- Window too large for the load: at 5 events/s p50 is still ~10 ms despite zero contention. Latency paid for throughput you don't need. Detect: high throughput headroom with latency pinned near window/2.
- Oversized batch: 1,000 rows is 2 + 100 = 102 ms holding one of eight connections; at 1 MB per event that's 1 GB of in-flight batch memory. Detect: connection wait time and per-slot heap.
- Poison event: one bad row fails all 100, and a blind retry re-sends the batch. Duplicates if the commit succeeded before a client timeout. Detect: sink-side duplicate counts keyed on event id, plus error rate jumping by roughly the batch factor.
What would change my answer: an ingest SLO of p99 under 10 ms rules batching out. I'd pipeline and shard writes wider instead. A nightly analytics consumer that tolerates seconds would let me batch at 1,000 and bank the throughput.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.