Sub topic 1
Latency, throughput and concurrency: what each one measures
Every target you set later in this tutorial is built from three quantities: latency, throughput and concurrency. Teams often mix them up. A statement like "the system must be fast" can mean any of the three, and each one leads to different design choices. This chapter defines each quantity, shows how they relate, and points out the measurement mistakes that cause trouble later.
Prerequisites
This chapter assumes you already know:
- Basic client-server and request/response concepts. A client sends a request, a server does some work, and a response comes back.
- Common backend components such as load balancers, application services and databases.
- How to read simple Python code and do arithmetic on rates and durations, such as requests per second and milliseconds.
You do not need any statistics or queueing theory yet. Later chapters cover percentiles, Little's law and queueing in detail.
The three quantities
Latency is the time one request takes from start to finish. It is measured per request, in units of time (usually milliseconds). Lower is better.
Throughput is the number of units of work completed per unit of time. It is measured across many requests, in units such as requests per second (RPS), messages per second or megabytes per second. Higher is better, up to what you need.
Concurrency is the number of requests being processed at the same moment. It is a count, measured at an instant or averaged over a window. It is also called "in-flight requests" or "requests in the system".
| Quantity | Question it answers | Unit | Measured over |
|---|---|---|---|
| Latency | How long does one request take? | milliseconds | a single request |
| Throughput | How much work finishes per second? | requests per second | a time window |
| Concurrency | How many requests are in progress right now? | requests (a count) | an instant or a window average |
A road gives a useful picture. Latency is how long one car takes to drive the road. Throughput is how many cars pass a marker each minute. Concurrency is how many cars are on the road at one moment. A road can have short trips and low traffic, or long trips and heavy traffic, and the two measures are independent. Concurrency links them, as the next sections show.
How the three relate
The three quantities are tied together by a simple relationship. In a stable system, the average number of requests in flight equals the arrival rate times the average time each request spends in the system:
concurrency = throughput x latencyThis is Little's law. A later chapter covers it in depth. For now, use it to build intuition with three examples:
- 2,000 requests per second at 50 ms each means about 100 requests in flight on average (2000 x 0.05).
- The same 100 requests in flight with latency growing to 200 ms means throughput can only be 500 requests per second (100 / 0.2).
- If you want 2,000 requests per second but each request takes 200 ms, you need about 400 requests in flight, which means 400 workers, connections or threads.
So latency and throughput are not opposites, but they compete for the same limited concurrency. If a resource caps how many requests can be in flight (a thread pool of 100, a database pool of 50 connections), then throughput is bounded by that cap divided by latency. When latency rises, throughput falls unless you raise the cap.
A worked example
The following program simulates a service where each request takes 50 ms of work. It sends 100 requests at once to a pool of workers and records three times per request. The wait is the time before a worker picks the request up. The service time is the time spent doing the work. The total is what a client experiences. It then reports throughput for the whole run.
import time
from concurrent.futures import ThreadPoolExecutor
SERVICE_TIME_S = 0.05 # each request needs 50 ms of work
def handle(submitted_at):
started = time.perf_counter()
time.sleep(SERVICE_TIME_S) # simulated work
finished = time.perf_counter()
wait = started - submitted_at
service = finished - started
total = finished - submitted_at
return wait, service, total
def run(workers, total_requests):
t0 = time.perf_counter()
with ThreadPoolExecutor(max_workers=workers) as pool:
futures = [
pool.submit(handle, time.perf_counter())
for _ in range(total_requests)
]
results = [f.result() for f in futures]
elapsed = time.perf_counter() - t0
waits, services, totals = zip(*results)
n = len(results)
print(f"workers={workers}")
print(f" throughput : {n / elapsed:7.1f} requests/s")
print(f" mean wait : {1000 * sum(waits) / n:7.1f} ms")
print(f" mean service : {1000 * sum(services) / n:7.1f} ms")
print(f" mean total : {1000 * sum(totals) / n:7.1f} ms")
for workers in (5, 20, 100):
run(workers, total_requests=100)Typical results on a laptop look like this (your numbers will differ slightly):
| Workers | Throughput | Mean wait | Mean service | Mean total |
|---|---|---|---|---|
| 5 | about 100 per second | about 480 ms | about 50 ms | about 530 ms |
| 20 | about 400 per second | about 100 ms | about 50 ms | about 150 ms |
| 100 | about 1,500 to 2,000 per second | near 0 ms | about 50 ms | about 50 ms |
Three observations follow from this table:
- Service time stays at 50 ms in every row. The work itself did not get faster.
- Throughput scales with workers while there are more requests than workers. The ceiling is workers divided by service time. With 5 workers that is 5 / 0.05 = 100 requests per second.
- The latency a client sees is wait plus service time. With few workers, almost all of it is waiting in line, not working.
The last point is the most useful one in practice. Adding concurrency raised throughput and also cut client-visible latency, because requests stopped queueing. This is why "throughput and latency trade off" is only partly true. It holds once you are at the limit of a resource, and it does not hold when you are short on concurrency.
Where you measure latency matters
"Latency" has no single value for a request. It depends on the points where you start and stop the clock.
- Service time is the time a server spends actively working on a request, from the moment it starts to the moment it finishes.
- Queueing delay is the time a request waits before work begins.
- Response time is queueing delay plus service time, measured at the server edge.
- Client-observed latency is response time plus network travel, DNS lookups, connection setup, TLS handshakes and any client-side retries.
A server's own logs usually report only service time or response time. They miss time spent in the load balancer's queue, in kernel accept queues and on the network. If a user's page loads in 900 ms but your service reports 60 ms, the difference is real latency that your server metric hides.
When you set a target, state where it is measured. "p99 under 300 ms at the API gateway" and "p99 under 300 ms in the user's browser" are very different promises. Later chapters build on this when they split a latency budget across the request path.
Different kinds of throughput
Throughput is easy to quote and easy to misread. Check these points whenever you see a throughput number:
- The unit of work. Requests per second, transactions per second, rows per second and bytes per second are different things. One "request" might read one row or scan a million.
- Success only or everything. Raw throughput counts all responses, including errors. Goodput counts only the useful, successful ones. A system under overload can have high raw throughput made mostly of fast error responses.
- Average or peak. Throughput changes through the day. A system that handles 500 requests per second on average may need to handle 2,000 for a short burst. The chapter on load estimation covers this.
- Capacity or demand. Throughput is what the system completes. Demand is what clients attempt. When demand exceeds capacity, throughput levels off at capacity and the excess queues, fails or is dropped.
Different kinds of concurrency
Concurrency is also used for several things, and mixing them leads to wrong sizing:
- Concurrent users is how many people are using the product. Most of them are idle most of the time, so this is a poor stand-in for load. 10,000 users who each send a request every 100 seconds produce only 100 requests per second.
- Concurrent requests is how many requests are in flight. This is the number in the formula above.
- Concurrency limit is a configured cap, such as a thread pool size, a connection pool size or a rate limiter's maximum in-flight count.
- Parallelism is how many things execute at literally the same instant on hardware, such as CPU cores. A service with 8 cores and 400 requests in flight has high concurrency but parallelism of 8. Most of the 400 are waiting on I/O or in a queue.
When you size a pool, you are choosing a concurrency limit. When you reason about users, you must convert them to a request rate first. The chapters on load estimation and Little's law show how.
Common mistakes
- Treating the three as one "performance" number. A change that improves throughput, such as batching, often raises latency for individual requests. Decide which one the product needs.
- Quoting average latency alone. An average hides slow requests. The next chapters replace it with percentiles.
- Measuring throughput without checking errors. A high rate of fast failures looks like a healthy system.
- Confusing user count with concurrency. Convert users to request rate and latency first.
- Ignoring where the clock starts. Server-side timers miss queueing and network time.
- Assuming more workers always help. Past the point where requests stop queueing, extra workers only add memory use and contention. Beyond a shared resource's own limit, such as a database that handles 50 connections well, extra concurrency makes latency worse.
Summary of the working model
- Latency is per-request time. Throughput is completed work per second. Concurrency is how much work is in progress.
- Concurrency is roughly throughput times latency, so any cap on concurrency bounds throughput.
- Client-visible latency includes queueing, so a service with fixed service time can still feel slow if it lacks capacity.
- A target is only meaningful when it states its unit, its measurement point and its time window.
Exercise
A service has a pool of 40 workers. Each request takes 80 ms of service time, and there is no other bottleneck. (a) What is the maximum sustainable throughput? (b) If 600 requests per second arrive, what happens to client-visible latency?
Show answer
(a) Maximum throughput is workers divided by service time: 40 / 0.08 = 500 requests per second. (b) Demand of 600 per second exceeds the 500 per second capacity, so the surplus of about 100 requests per second accumulates in a queue. Queueing delay grows steadily, so client-visible latency keeps rising above 80 ms until requests time out, are dropped, or demand falls. Throughput stays at about 500 per second.
Exercise
A product team says "we need to support 20,000 concurrent users with fast responses." Each user sends one request every 40 seconds on average, and the average request takes 100 ms. Roughly how many requests per second does this imply, and how many requests are in flight on average?
Show answer
Request rate is 20,000 / 40 = 500 requests per second. Average in-flight requests is throughput times latency: 500 x 0.1 = 50. The 20,000 concurrent users produce only about 50 concurrent requests on average, which is why user count is a poor measure of concurrency.