Sub topic 14
Metrics That Show Saturation
This chapter assumes you know Little's law, bounded queues, and basic HTTP status codes. It covers which metrics reveal that a service is saturated or overloaded, and which ones hide it.
Why averages and CPU are not enough
A service can be overloaded while CPU sits at 40%. Workers may be blocked on a database, a lock, or a connection pool. Average latency also hides the problem, because a few very slow requests barely move the mean. You need metrics that measure waiting, not only working.
The core signals
| Metric | What it shows | Overload warning sign |
|---|---|---|
| Queue depth | Work waiting for a worker | Grows steadily and does not drain |
| Queue wait time (age of the oldest item) | How long work waits before it starts | Approaches the client timeout |
| In-flight requests | Concurrency, the L in Little's law | Sits at its limit |
| Latency percentiles (p50, p99) | What users experience | p99 rises long before p50 |
| Rejection rate | Requests shed or rate limited | Nonzero for long periods |
| Goodput | Requests that finished in time and were useful | Falls while throughput stays high |
| Retry rate | Duplicate load from clients | Rises after errors begin |
Queue wait time is often more useful than queue depth. A depth of 100 is fine if workers clear it in 50 ms. It is a failure if it takes 30 seconds. Wait time is also the number you compare against a deadline.
Throughput versus goodput
Throughput counts all completed work. Goodput counts only work whose result was still wanted. Under overload, a service may keep finishing requests after the client has given up. Throughput looks healthy, but goodput collapses. Track both, and count a response as good only if it arrived before the client's deadline and returned a success status.
Saturation, utilization, and errors
A useful checklist is utilization, saturation, and errors for each resource. Utilization is how busy a resource is. Saturation is how much work is waiting for it. Errors include timeouts and rejections. Saturation is the signal that matters most here, since a resource at 100% utilization with no queue is coping, while one with a growing queue is not.
Measuring queue wait in code
Record the enqueue time with each item, then measure the wait when a worker takes it.
import asyncio
import time
async def worker(queue: asyncio.Queue, waits: list) -> None:
while True:
enqueued_at, item = await queue.get()
waits.append(time.monotonic() - enqueued_at)
await asyncio.sleep(0.01) # simulate work
queue.task_done()
async def main() -> None:
queue = asyncio.Queue(maxsize=50)
waits: list = []
task = asyncio.create_task(worker(queue, waits))
for i in range(50):
await queue.put((time.monotonic(), i))
await queue.join()
task.cancel()
waits.sort()
print(f"p50 wait: {waits[len(waits) // 2]:.3f}s")
print(f"max wait: {waits[-1]:.3f}s")
print(f"depth now: {queue.qsize()}")
asyncio.run(main())The last item waits about 0.5 seconds, even though each job takes only 10 ms. That gap is the queue at work.
Common mistakes
- Alerting on average latency instead of percentiles.
- Watching CPU only, which misses blocked workers.
- Exporting queue depth without wait time.
- Counting rejected requests as errors in the service's own success rate, so shedding looks like an outage. Report shed requests separately.
- Sampling gauges too slowly. A queue can fill and drain between 60-second scrapes, so also record a maximum over each interval.
Exercise
A service shows 45% CPU, steady throughput, and a p50 latency of 20 ms. Its p99 latency has risen from 200 ms to 8 seconds, and its in-flight count sits at its limit of 50. Which metric would you check next to confirm saturation, and why?
Show answer
Check queue wait time (the age of the oldest queued item). The in-flight count at its limit and the rising p99 suggest requests are waiting for a worker. Low CPU means the workers are probably blocked on something else, such as a database. Queue wait time confirms the waiting directly and can be compared against client timeouts.