Latencia frente a Rendimiento

Ley de Little · Latencia de cola

Escrito por Darjus Kazakevic · revisado el

Batching buys throughput with latency, and users feel the p99, not the average.

Latency is the time one request takes, in milliseconds. Throughput is how many requests finish per second. They often move in opposite directions. Batching 100 writes into one round trip raises throughput, but the first write now waits for the hundredth. Little's law links them: requests in flight = arrival rate × time in the system.

The average hides the pain. Users feel the slow tail, measured as p99 (the time that 99% of requests beat). A page that calls ten services waits for the slowest, so about one page in ten hits some service's p99. Design per path: small batches and tight timeouts where users wait, large batches for jobs.

En esencia — Latency is how long one request takes and throughput is how many finish per second; most levers trade one for the other, so choose per path and measure the tail.

Cuándo recurrir a él

Recurre a él cuando

  • Tuning batch size, pool size or concurrency on a shared client
  • A page fans out to many services and waits for the slowest one
  • Sizing a pool, a queue or a timeout from measured numbers

Reconsidéralo cuando

  • Offline batch jobs, where nobody waits on any single item
  • Lightly loaded paths where nothing queues and both numbers are fine
  • The cause is one slow query — fix the query before tuning the pool

Conceptos clave

Latency
The time from sending a request to getting the answer. Report it as percentiles: p50 is the typical request, p99 the slow one that 1 request in 100 sees.
Throughput
Work finished per unit of time: requests per second, rows per second, MB per second. The slowest stage in the path sets the limit.
Little's law
In a stable system, items in the system = arrival rate × average time each one spends there. It turns two numbers you can measure into the third.
Fan-out and the tail
When one request calls many services in parallel, it waits for the slowest. The more calls, the more likely one of them is in its slow tail.
Utilisation and queueing
As a server gets close to 100% busy, waiting time grows sharply, not in a straight line. At 50% busy a request waits about one service time; at 90% it waits about nine.

Reglas de diseño

Measure the p99, not the mean
The mean hides the slow requests that users remember. Record latency as histograms, report p50 and p99 per route, and alert on the p99.
One setting per path, never one for both
Batching and deep queues buy throughput by spending latency. Give the interactive path small batches and short timeouts, the bulk path large batches, and never share one setting.
Keep utilisation below about 70%
Waiting time explodes as a server nears 100% busy. Leave headroom on anything a user waits for, and add capacity before the p99 climbs, not after.
Set a latency budget per hop
Split the page's target across its calls. A 300 ms page might give 50 ms to auth and 150 ms to search. Each hop's timeout comes from its budget, not from a default.

Errores habituales

  • Averaging percentiles across servers. The mean of ten p99s is not the p99 of the fleet; merge the histograms instead.
  • Reporting only the mean. A 50 ms average can hide a p99 of 2 seconds that one request in a hundred hits.
  • Raising the pool size to fix latency. If the database is the limit, more connections add queueing there and make it worse.
  • Load tests that wait for each slow response before sending the next (coordinated omission). They never measure the queue users would see.