Skip to content

Bottlenecks

Water leaves a bottle at the rate that its neck allows, however large the bottle is. A system works the same way. A request passes through several components, and each can serve only so many requests per second. The one with the lowest capacity, or more exactly the one that reaches its capacity first as load rises, sets what the whole path can do. That component is the bottleneck.

Mental model. The throughput of a path is the lowest capacity on it. Making any other part faster or bigger does not raise it. The only change that helps is one at the bottleneck, and once it is lifted, the next lowest capacity becomes the new limit.

Two consequences matter in practice. Spending on a part that is not the bottleneck is wasted, as the scalability lesson showed with API instances. And a system always has a bottleneck: it can be a long way off, but there is always some component that will be the first to run out.

The slowest part is not always the bottleneck

Section titled “The slowest part is not always the bottleneck”

The checkout request from the first module reaches seven components: the checkout service, which calls the login service, the inventory database, the payment provider, the recommendations service and an order queue, and an email worker that reads from the queue.

CustomerHealthy<1%CheckoutserviceHealthy2%Login serviceHealthy1%InventorydatabaseHealthy1%PaymentproviderHealthy7%RecommendationsserviceHealthy5%Order queueHealthy<1%Email workerHealthy24%
CustomerHealthy<1%CheckoutserviceHealthy2%Login serviceHealthy1%InventorydatabaseHealthy1%PaymentproviderHealthy7%RecommendationsserviceHealthy5%Order queueHealthy<1%Email workerHealthy24%
Text version of the diagram
  • Customer sends requests to Checkout service.
  • Checkout service sends requests to Login service, Inventory database, Payment provider, Recommendations service, Order queue.
  • Login service does not call another component.
  • Inventory database does not call another component.
  • Payment provider does not call another component.
  • Recommendations service does not call another component.
  • Order queue sends requests to Email worker.
  • Email worker does not call another component.

A common guess for “what is the bottleneck?” is the slowest one. The payment provider takes 120 ms per request, the longest of any component, and half as long again as the email worker’s 80 ms. But the time a request takes is not the same as how many requests can be served. The capacity of a component is its slots, the instances times the requests each can work on at once, divided by the time each request takes.

Component Slots Time per request Capacity
Email worker 1 × 10 = 10 80 ms 125 per second
Payment provider 1 × 50 = 50 120 ms 417 per second
Recommendations service 1 × 20 = 20 30 ms 667 per second
Checkout service 2 × 16 = 32 25 ms 1,280 per second
Inventory database 1 × 20 = 20 6 ms 3,333 per second
Login service 2 × 16 = 32 8 ms 4,000 per second
Order queue 1 × 50 = 50 2 ms 25,000 per second

The payment provider is the slowest per request, but it has 50 slots. The email worker takes less time per request, 80 ms, but it has only 10 slots, so it has the lowest capacity: 125 requests per second. At 100 requests per second the worker is at 80%, the payment provider at 24%, the recommendations service at 15%, the checkout service at 8%, and the rest at 3% or less. What is slow is not what is limiting.

Suppose the team gives the worker four times the slots, 40. Its capacity becomes 500 per second, and the payment provider, at 417, is the new limit. The whole path went from 125 to 417 per second, 3.3 times as much and not four times, because lifting one bottleneck reveals the next one.

Finding it: utilisation, saturation, errors

Section titled “Finding it: utilisation, saturation, errors”

Guessing from the architecture is a starting point. Finding the real one means measuring. The USE method, from Brendan Gregg, says to check three things for every resource: its utilisation, how busy it is; its saturation, how much work is waiting that it cannot serve; and its errors. The bottleneck is usually the resource with the highest utilisation, and the clearest sign is saturation: a queue that is growing, or requests that wait for it.

Two points make this reliable. Look at every resource on the path and compare them, because a bottleneck is only meaningful relative to the others: a component at 60% is the bottleneck if every other one is below that. And look at it at the load you care about, because the bottleneck at 100 requests per second may not be the one at 1,000.

Try it: the same system under a different workload

Section titled “Try it: the same system under a different workload”

The lab below is the web system from the scalability lesson: a load balancer, API instances, a cache and a database, at 12,000 requests per second. It starts with 90% of reads served from the cache. Lower the Cache hit rate and watch the tiles for CPU, which is the API’s utilisation, and Database load.

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.

12,000 req/s
API instances
3
90%
40
0.3 ms0.2 ms0.5 ms0.5 msClientsHealthy<1%Load balancerHealthy<1%APIHealthy25%CacheHealthy14%DatabaseHealthy46%
0.3 ms0.2 ms0.5 ms0.5 msClientsHealthy<1%Load balancerHealthy<1%APIHealthy25%CacheHealthy14%DatabaseHealthy46%

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.

0 ms
Metrics
ComponentStateUtilisationOffered (req/s)Served (req/s)Rejected (req/s)p50p95p99
ClientsHealthy<1%12K12K00 ms0 ms0 ms
Load balancerHealthy<1%12K12K00.03 ms0.15 ms0.23 ms
APIHealthy25%12K12K02.8 ms12 ms18 ms
CacheHealthy14%11K11K00.14 ms0.6 ms0.92 ms
DatabaseHealthy46%2.3K2.3K05.5 ms24 ms37 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.

With the cache at 90%, the API is at 25%, the database at 46% and the 99th percentile is 28 ms. At 70% the API is still at 25%, and the database is at 89%, with a 99th percentile of 35 ms. At 50% the database is at 132%, 13.1% of requests fail, and the 99th percentile is 202 ms. At 99% the database is at 26%.

The traffic is the same and the number of API instances is the same. Only the share of reads that the cache answers changed, and it moved all the load onto the database. The bottleneck depends on the workload as well as on the design. A change that sends more work to a component, such as a cache that was cold, flushed or too small, can make that component the bottleneck without any change to the component.

At a hit rate of 50% the system fails 13.1% of requests and the 99th percentile is 202 ms. The API is at 25%, and a team that reads “latency is high, the API is the front of the system” adds instances. Raise API instances from 3 to 10: the API falls to 8%. The failures are still 13.1%, and the 99th percentile is still 202 ms. Cutting the API to 1 instance does not change those two figures either, with the API at 75%.

The database is the bottleneck, so only a change there matters. Raise Database connections from 40 to 80: the database falls to 66%, nothing fails, and the 99th percentile is 38 ms. At 120 connections it is at 44%. Raising the hit rate back toward 90% helps the database in the same way, by sending it less work.

The lesson is the order of the steps: measure before spending. The cheapest change that lifts the bottleneck is nearly always found by looking at the utilisation of every component, not by deciding in advance where the problem must be.

  • The bottleneck is the part that reaches its capacity first. It sets the throughput of the whole path, and a system always has one.
  • The slowest part is not necessarily the bottleneck: the capacity of a component is its slots divided by the time per request. In the checkout path the payment provider is the slowest per request, and the email worker, with 125 per second, is the limit.
  • Lifting a bottleneck reveals the next one. Quadrupling the worker’s slots raises the path from 125 to 417 per second, not to 500.
  • Find it by comparing utilisation and saturation across every resource at the load you care about.
  • The bottleneck depends on the workload: in the lab, lowering the cache hit rate from 90% to 50% leaves the API at 25% and takes the database from 46% to 132%.
  • Spending anywhere else changes nothing: 10 API instances instead of 3 leaves the failures at 13.1%.

Next: CPU, memory and I/O, and what a saturated resource of each kind looks like.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.