Highlights

Contents

Part VII
07

Bottlenecks and scaling

We change the component that limits useful work. Scaling every box wastes time and can add coordination. We will inspect resource limits, cache loss, queue recovery, and key skew.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Requirements gathering2Back-of-the-envelopeestimation3API design4Data model5High-level architecture6Critical request flows7Bottlenecks and scaling8Failures and consistency9Trade-offs and the finalanswer

#Section 25Bottleneck identification

Read delay rises while CPU is quiet. The service can be waiting for storage or connections rather than computation. Capacity is sustainable useful work under a specified workload and objective, not a hardware label.

Inspect CPU work, storage operations, network bytes, pool acquisition, and queue age. Name the limiting unit, such as queries per second at a latency target or bytes per second on a link. A benchmark from a different record size or query mix does not establish Heron’s capacity. Change one relevant mechanism and verify the result under the same workload. Vertical resources can help until the workload’s serial or external limit dominates; horizontal copies help only the work that can be distributed safely.

A slow read may wait for a pool slot, execute an expensive query, or spend time delivering its response. Each case predicts different evidence and suggests a different change. Record acquisition and execution separately, inspect request traces, and compare useful completion under the same offered workload. A larger pool can shift waiting into the database; another API instance can multiply source concurrency; a cache can reduce repeated reads while increasing cold-path risk. Explain the measurement expected to improve after the chosen change. That prediction makes the architecture decision reviewable rather than a list of familiar scaling products.

FIND THE LIMITING RESOURCE, CHANGE THAT MECHANISM, RERUN THE SAME WORKLOADFIND THE LIMITING RESOURCE, CHANGE THAT MECHANISM, RERUN THE SAME WORKLOADsymptomread delay atthe user boundaryevidencepool, disk or CPU:name the limitchangethe relevant mechanism,same test workloada larger instance that does not touch the limit changes nothing
Figure 21.25Illustrative bottleneck identification. The chosen scaling change addresses an identified waiting resource. Orange marks a mechanism change assessed under the same workload.

Adding workers can increase contention at an already saturated database and reduce useful throughput.

#Section 26Normal capacity and failure headroom

Assume five API instances can each sustain 300 requests per second on this workload. Normal capacity is 1,500. With one unavailable, four survivors supply 1,200 against the 1,000 peak, leaving 200 of arithmetic headroom.

Failure headroom is spare sustainable capacity after the promised loss. It is not the difference between normal capacity and normal demand. The calculation assumes load can be redistributed, warm survivors can accept it, and shared dependencies still work. A rolling deployment or zone failure can remove more than one instance. State the failure domain and the required surviving capacity. Use admission limits if offered traffic can exceed the protected downstream rate.

The illustrated normal fleet has 1,500 requests per second of sustainable capacity, but the failure decision uses the 1,200 that remains after losing an instance. Compare that surviving rate with the same 1,000-request offered population, retaining the shown 200 of spare capacity. Routing must actually reach the survivors and respect their individual limits; total capacity can conceal uneven placement. Also separate request-rate capacity from connection or memory capacity. A replacement instance may begin cold and temporarily increase source misses. Failure headroom therefore needs the altered workload as well as an arithmetic count of the surviving instances.

FAILURE HEADROOM IS CAPACITY THAT REMAINS AFTER A LOST OWNERFAILURE HEADROOM IS CAPACITY THAT REMAINS AFTER A LOST OWNER300 req/s300 req/s300 req/s300 req/s300 req/snormal: 5 × 300 = 1,500 requests/safter loss: 4 × 300 − 1,000 = 200 requests/s spare
Figure 21.26Illustrative capacity after failure. Five instances provide 1,500 requests/s; four surviving instances provide 1,200 requests/s, leaving 200 requests/s above the stated demand.

Five instances in one failing zone do not provide capacity after that zone is lost. Failure domains matter as much as counts.

#Section 27The cache outage calculation

At the illustrative peak and a 0.9 hit fraction, 100 of 1,000 reads reach the database. If the cache disappears and every read falls through, the source receives 1,000, ten times its normal miss load.

A cache failure policy determines which work is admitted when the fast copy cannot serve it. Use bounded fallback, request coalescing, admission limits, and allowed stale results to protect the authority. The policy must honor the freshness contract, so a stale score can be allowed for one display and forbidden for a scorer confirmation. A cache is an optimization only when its absence has a designed behavior; otherwise it is an undocumented capacity dependency.

At the illustrated normal hit rate, the source sees 100 reads per second. If the cache vanishes and every request immediately falls through, that same viewer demand offers 1,000 source reads per second. Extra API instances do not restore source capacity and can admit still more competing work. Add a bounded fallback gate, request coalescing where safe, and a declared stale or rejection policy. Recovery warming also consumes source work, so place it within the same budget. A candidate should show what happens to the caller under that gate rather than claiming cache failure is transparent.

AT 90% HITS THE SOURCE SEES 100 READS/S; WITHOUT THE CACHE IT SEES 1,000, 10×AT 90% HITS THE SOURCE SEES 100 READS/S; WITHOUT THE CACHE IT SEES 1,000, 10×normal1,000 × 0.1 =100 source reads/scache goneall reads fall through:1,000 source reads/samplification10×1,000 ÷ 100 = 10×more API instances do not add source capacity
Figure 21.27Illustrative the cache outage calculation. Cache loss transfers the entire read stream to a source normally serving misses. Orange marks fallback amplification when all reads reach the source.

A timeout followed by unrestricted fallback can overload the source while callers continue spending time on the failed cache.

#Section 28Queue recovery and backpressure

Workers complete 1,000 tasks per second while arrivals reach 1,200 for 60 seconds. The backlog grows by 200 per second, ending at 12,000. If arrivals later fall to 600, spare drain is 400 per second and recovery takes 30 seconds.

Backpressure makes upstream work respond to downstream limits through reduced admission, slowed production, or an explicit rejection. A queue buys time and decouples bursts; it does not create infinite throughput. Bound queue bytes, age, retries, and each task’s work. Keep dead-letter and recovery paths for poison inputs, and count successful effects separately from attempts. Google’s overload chapter discusses overload controls and the danger of cascading work.

The illustrated burst leaves 12,000 tasks, and ordinary input later consumes part of the worker rate while that backlog remains. The available 400 tasks per second for old work gives the stated 30-second drain. If successful service falls below arrivals, no finite drain exists under unchanged conditions. A deeper queue postpones rejection but increases wait and retained memory. Bound admissions or task expiry from the business deadline, and preserve durable work where the contract requires it. Count useful completions separately from retry attempts so a poison task cannot make the queue appear to recover while effects stay stalled.

200/S GROWTH FOR 60 S = 12,000 QUEUED; 400/S SPARE CLEARS IT IN 30 S200/S GROWTH FOR 60 S = 12,000 QUEUED; 400/S SPARE CLEARS IT IN 30 Sburst1,200 in − 1,000 out= 200/s growthafter 60 s12,000 taskswaitingrecovery1,000 − 600 = 400 spare:12,000 ÷ 400 = 30 sif service stays below arrivals, there is no finite drain
Figure 21.28Illustrative queue recovery and backpressure. Queue recovery uses spare service after ongoing input, rather than total service rate. Orange marks recovery using spare service after continuing arrivals.

Unbounded retries can turn one task into repeated offered work and prevent the queue from ever recovering.