CPU, memory and I/O
One question per resource
Section titled “One question per resource”The last lesson said to find the bottleneck by looking at every resource. Four of them cover most cases, and each runs out in its own way. For each, the USE method asks for its utilisation, its saturation and its errors.
| Resource | Utilisation | Saturation | What the user sees |
|---|---|---|---|
| CPU | The share of time the cores are busy | Work waiting for a core: a run queue longer than the number of cores | Latency rises as the cores approach 100% |
| Memory | The share of memory in use | The system is swapping, or scanning for pages to free | Slow or failing: at the limit, a process can be killed |
| Disk I/O | The share of time the device is busy | Requests queueing for the device, and a long wait per operation | Reads and writes slow down while the CPUs sit idle |
| Network | The bandwidth used, as a share of the link’s maximum | Dropped packets and retransmissions | Throughput capped by the link, and delays from loss |
The checklist behind this table, Brendan Gregg’s for Linux, names the tools for each: vmstat for the run queue and for swapping, iostat for the disk’s queue and waits, and counters of drops and retransmissions for the network. Their field names vary between versions, so read the manual of the one you have.
Challenge
Which resource is the limit?
A slow system is slow because one resource has run out of something. CPU, memory, disk and network each show it differently. Put each symptom under the resource it points to.
Sorting needs JavaScript.
Categories
- CPU
- Are the cores out of time?
- Memory
- Is there room left for what is held?
- Disk I/O
- Can the device keep up with the reads and writes?
- Network
- Can the link carry what is sent?
Check your answers at least once before looking at the answers.
A rate and a stock
Section titled “A rate and a stock”CPU, disk and network share a property. Each has a capacity per second, and a request uses some of it and gives it back. Their limits follow the queueing curve of the earlier lesson: latency is gentle at low utilisation and steep near 100%.
Memory is different. It is not used up and renewed each second. It holds things, such as buffers, caches and the state of each request in progress, until they are released. Utilisation is how much is held, and the limit is a size, not a rate. Little’s Law says how much a service holds: the requests in flight are the arrival rate times the time each stays, and each one holds some memory.
Mental model. CPU, disk and network are used up and renewed every second, and they slow down gradually as they fill. Memory is held until it is released, and it can fail abruptly when it runs out.
The comparison with the table from the first lesson shows why running out is so costly. A read from main memory takes about 100 ns, a random read from an SSD about 150 µs and a seek on a spinning disk about 10 ms. When a system swaps, it uses the disk as overflow for memory: a spinning disk’s seek is 100,000 times the time of a memory read. Those figures are dated, so take the size of the gap and not the digits.
Try it: memory follows the requests held
Section titled “Try it: memory follows the requests held”The lab below is the web system from before, and this time watch the Memory tile. It shows what one API instance uses, out of a limit of 2,048 MB. The model gives each instance 400 MB of its own and 256 KB for every request it holds, whether the request is being served or waiting.
Requests pass a load balancer to three API instances. The API reads through a cache and falls back to a database whose connection pool is sized for far less traffic than the API can take. Change the traffic, the number of instances, the cache hit rate, and the database connections, and watch the cause and effect play out.
Each dot stands for many requests. The numbers show the real rate.
No bottleneck: every component has headroom.
Key figures
- Traffic
- 12,000 req/s
- CPU
- 25%HealthyAPI utilisation
- Memory
- 20%Healthy404 MB of 2,048 MB per API instance
- Latency
- p50 4.5 msp95 19 msp99 28 msHealthyWhole request, all requests
- Error rate
- 0%HealthyRequests that fail
- Database load
- 46%HealthyDatabase utilisation
- Cache load
- 14%HealthyCache utilisation
Cause and effect
Each step leads to the next. A step that is changing right now is marked.
- Traffic12K req/sNormal steady
- Database utilisation46%Normal steady
- Database queuenone waitingNormal steady
- Latency (p99)28 msNormal steady
- Timeouts and rejections0%Normal steady
- Errors0%Normal steady
| Component | State | Utilisation | Offered (req/s) | Served (req/s) | Rejected (req/s) | p50 | p95 | p99 |
|---|---|---|---|---|---|---|---|---|
| Clients | Healthy | <1% | 12K | 12K | 0 | 0 ms | 0 ms | 0 ms |
| Load balancer | Healthy | <1% | 12K | 12K | 0 | 0.03 ms | 0.15 ms | 0.23 ms |
| API | Healthy | 25% | 12K | 12K | 0 | 2.8 ms | 12 ms | 18 ms |
| Cache | Healthy | 14% | 11K | 11K | 0 | 0.14 ms | 0.6 ms | 0.92 ms |
| Database | Healthy | 46% | 2.3K | 2.3K | 0 | 5.5 ms | 24 ms | 37 ms |
- Traffic
- 12,000 req/s
- Successful
- 12,000 req/s
- Error rate
- 0%
- Mean latency
- 6.3 ms
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Requests arrive at random, as a Poisson process, at a constant average rate.
- Each node is one queue, served first-come first-served by several identical slots (instances × concurrency).
- Service times are random with the stated mean; by default they are exponentially distributed.
- Each node is analysed separately, with the traffic it receives treated as Poisson.
- Nine in ten requests are reads. The cache answers the share set by the hit rate; the rest, and every write, go to the database.
Simplifications
- Above capacity there is no steady state. The model switches to a fluid approximation: the node serves at capacity, its queue grows until it reaches its limit, and further requests are rejected.
- Percentiles assume an exponential tail. A service-time variability other than exponential changes the mean wait but not the shape of the tail.
- A node frees its slot as soon as it has done its own work, before waiting on the services it calls. Real thread-per-request servers hold the slot, so a slow dependency exhausts them faster than this model shows.
- A request calls its dependencies one after another. Parallel fan-out is not modelled.
- When a caller times out and gives up, the callee still finishes the work, so abandoned requests keep consuming capacity.
- Retries add load. Their backoff and jitter decide when that load arrives, which only the per-request simulation shows.
- Retry feedback is solved from the healthy starting point, so it finds one stable outcome. Systems that stay stuck in overload after the cause is gone are not shown.
- A throughput limit (for example maximum writes per second) is modelled as slower service, not as a separate rate limiter.
- A caller’s timeout counts the mean time its dependencies take, not their full distribution.
- Latency percentiles for a whole request are estimated, over every request, including those that are rejected and fail quickly. The mean is exact, and the percentiles come from a gamma distribution that has that mean and the variance of the time spent in each component.
- Memory is modelled simply: a fixed base for each instance, plus a fixed amount for every request the instance holds, whether it is being served or waiting.
- Each database connection serves one operation at a time, about 8 ms each, so 40 connections can serve about 5,000 operations per second. Reads and writes cost the same here; real databases treat them differently, and writes are usually the scarcer resource.
Parameter values are illustrative. They are not benchmarks of any real product.
At 12,000 requests per second the tile reads 404 MB, which is 20%. Little’s Law explains the figure. Three instances share the traffic, so each receives 4,000 requests per second. Each request takes about 4 ms, so each instance holds 4,000 × 0.004 = 16 requests, which is 16 × 256 KB = 4 MB, on top of the 400 MB.
Raise the traffic. At 30,000 it reads 410 MB and at 40,000 it reads 413 MB, with the CPU tile, which is the API’s utilisation, at 83%. Memory has barely moved, because the requests are served as fast as they come. At 48,000 requests per second the API reaches 100% and the tile jumps to 2,016 MB, which is 98% of the limit. Requests now wait in the API’s queue instead of being served, and each one holds memory while it waits. The API’s capacity is 3 instances × 64 slots ÷ 4 ms = 48,000 requests per second, and at that rate the line between a few megabytes of requests and a full queue is crossed.
What happens if a service runs out of memory?
Section titled “What happens if a service runs out of memory?”At 48,000 requests per second the API instances are full. What happens next depends on the system, and two outcomes are common. The operating system can start swapping, which makes every request on the machine slower, including the ones that were not the problem. Or it can kill a process, and the instance disappears.
In the second case the lab’s arithmetic carries on. With one instance gone, the other two have a capacity of 2 × 64 ÷ 4 ms = 32,000 requests per second, and the load is 48,000, which is 150% of it. Their queues are full, and they sit at the same memory limit that killed the first one. The system’s memory limit has turned into a loss of instances, and the loss raises the load on those that remain. The failure modules return to this chain, and to the limits and timeouts that stop it.
CPU does not do this. At 100% a CPU does not fail outright; it serves as much as it can, and the latency is what rises. A rate resource saturates, and a stock resource overflows.
Disk and network, in numbers
Section titled “Disk and network, in numbers”Two short calculations give the other limits in the same terms.
Disk. If every read needs a seek of about 10 ms, one device can do 1 ÷ 0.010 = 100 random reads per second. A database that needs 1,000 random reads per second from such a disk will not get them, and the queue at the device is the sign. The fixes are to read less (a cache), to read differently (sequentially, or from a device with a shorter access time), or to add devices.
Network. A link of 1 gigabit per second carries at most 125 megabytes per second, because a byte is 8 bits. A service whose responses are 500 KB, half a megabyte, can send at most 125 ÷ 0.5 = 250 responses per second over that link, and the CPUs can be nearly idle while it does. Raising the CPU count changes nothing. Compressing the responses, or sending less, does.
- CPU, memory, disk and network each have a utilisation and a saturation signal: a run queue longer than the cores, swapping, a queue and long waits at the device, and drops and retransmissions on the link.
- CPU, disk and network are rates, and they slow down gradually along the queueing curve. Memory is a stock: it holds what requests need until they leave, and it can fail abruptly when it runs out.
- Little’s Law predicts the memory a service holds: in the lab, 4,000 requests per second per instance times 4 ms is 16 requests, and 4 MB on top of the base.
- In the lab, memory stays near 404 to 413 MB until the API reaches 100% at 48,000 requests per second, and then it jumps to 2,016 MB, 98% of the limit.
- A killed instance raises the load on the others. Two instances have a capacity of 32,000, and the load of 48,000 is 150% of it.
- A 1 Gbit/s link carries 125 MB per second, which is 250 responses of 500 KB a second, however idle the CPUs are.
Next: the module recap and quiz, which covers all six lessons.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.