ch10 · Three chains, and the binding constraint
The question
When three chains each demand a different size, which one are you buying?
ch09 followed one chain from stored bytes to hosts. There are two more: one from the requests, one from the working set. The three disagree with each other, and the disagreement is not to be averaged away.
The material
Three chains from one workload
A web service has to serve its requests, keep its working set in memory, and hold its records on disk. Those are three requirements with three different arithmetics, and none of them can be derived from the others.
The request chain runs from the busy hour, through the cost of a request and the queueing margin, to a number of hosts.
The memory chain runs from the records held, through the share of them a busy hour touches, to the hosts whose memory can hold that with room to spare.
The disk chain is ch09’s.
The chains share inputs. One growth factor multiplies both the peak request rate and the records held. The memory and disk chains both start from the records held. So in a future where the service grows fast, all three chains ask for more, and they tend to rise and fall together. They still do not move in step, because each chain has inputs the others do not have: the CPU time per request for the request chain, the share of records touched for the memory chain, and replication, compression and index overhead for the disk chain.
The table shows what each chain asks for, and what the model recommends, at the point estimate: every input at the middle of its range, and the arithmetic done once.
| Output | What the model says | Unit |
|---|---|---|
| hosts for requests | 50 | host |
| hosts for memory | 54 | host |
| hosts for storage | 49 | host |
| hosts the model recommends | 54 | host |
Source — web_service_binding-reference · the model to explore; each input with a range has a slider
The three counts are close together, and the memory chain asks for the most. A small move in almost any input can change which chain is largest.
The three figures that follow show each chain’s host count across all the model’s futures, one figure per chain.
The request and memory spreads almost coincide. The disk spread stops lower at the top. Three separate spreads cannot say which chain is the largest in any one future, and that is the question the purchase turns on. The table in the next section answers it future by future. None of the three is the answer.
Buy the largest, not the average, the usual winner or the sum
The count that satisfies all three is the largest of them, and problem 10.1 is that one function call. There are three wrong answers, and each fails in its own way.
Binding constraint. The constraint in your model that decides the answer: the chain asking for the most hosts, because every other constraint must also be satisfied. Which constraint binds can shift as your assumptions change.
The average. It is never below all three chains, because it is at least the smallest. But it falls short of the largest chain whenever the three differ, in almost every future, and which chain is the largest changes from one future to the next.
The usual winner. The memory chain asks for the most more often than the others, but still wins outright less than half the time. A fleet sized on it alone is too small whenever another chain asks for more, which happens more often than not. The table in the next section shows both: “The working set decides it” and “Sized on the memory chain alone, too small”.
The sum. It buys a fleet for a workload that does not exist. The three chains describe the same hosts doing three things, not three sets of hosts. So the sum buys more than any chain asked for, in every future.
How often each one wins
| The request rate decides the host count | 36% of futures |
| The working set decides it | 42% of futures |
| The data on disk decides it | 19% of futures |
| Two chains ask for the same count | 3% of futures |
| Sized on the request chain alone, too small | 62% of futures |
| Sized on the memory chain alone, too small | 56% of futures |
| Sized on the disk chain alone, too small | 79% of futures |
| Median gap between the winner and the runner-up | 14 hosts |
| Gap exceeded in one future in twenty | 81 hosts |
| Median of the request chain alone | 50 hosts |
| Median of the memory chain alone | 53 hosts |
| Median of the disk chain alone | 49 hosts |
| Median of the largest of the three | 68 hosts |
Source — binding-constraint · sizing.evaluate
No chain decides most of the time. The working set wins more often than the others. The request rate is close behind. The disk chain, the one ch09 spent a chapter on, wins least. Then read the three rows after the tie. Size on any one chain alone, even the usual winner, and the fleet is too small more often than not. Wins most often is a fact about a three-way race. Too small is a fact about losing to any one of the other chains.
Median. The middle answer: put every outcome in order and take the one in the middle; half the outcomes come in above it.
In each future, the gap is the difference between what the winning chain asks for and the runner-up; the row “Median gap between the winner and the runner-up” is the middle of those gaps. Set the median gap against the three Median of the … chain alone rows. The gap is a sizeable share of what any one chain asks for. These are not three estimates of the same thing that differ slightly: they are three different questions with three different answers. The row “Gap exceeded in one future in twenty” shows how wide the gap gets: in one future in twenty it exceeds that value.
The row “Median of the largest of the three” sits above every one of the three chain medians because the largest of three uncertain counts usually sits above where any one of them usually does. So the fleet the model recommends is bigger than any chain’s typical answer. That is not waste. It is what buying for three requirements at once costs, when each of them is uncertain on its own.
All three chains are in the model below, with the node that takes the largest. To read the whole file, press Expand and choose Model file: three nodes ask for a host count, and a fourth, hosts the model recommends, takes the largest. Drag CPU time per request up and watch the Outputs list: at the book’s value the memory chain asks for the most. As you drag up, only hosts for requests rises, and a little above the book’s value it passes hosts for memory, pulling hosts the model recommends with it. Dragging down lowers hosts for requests, but hosts the model recommends stays where it is, because the memory chain already asks for more. The viewer greys the rows the input cannot move: hosts for memory and hosts for storage.
The model with three chains and the node that takes the largest. Click hosts the model recommends in the Outputs list, and the Details panel shows its formula and the three chains that feed it under Fed by.
The shortfall, and the chain that usually wins
Sizing on the memory chain is the obvious choice, because it wins more often than either other chain. What it costs comes down to two numbers: how often the fleet is too small because another chain asked for more, and by how much when it is. The table above shows how often: the row “Sized on the memory chain alone, too small”, which is more often than not. How much depends on which futures you count: in every future where the memory chain asks for at least as much as each other chain, the shortfall is zero.
The table below has two rows for a fleet sized on the memory chain alone: “Median shortfall across all futures” and “Average shortfall across all futures”, both counting every future including the zeros.
Median shortfall across all futures, sized on the memory chain alone | 4 hosts |
Average shortfall across all futures, sized on the memory chain alone | 17 hosts |
Source — binding-constraint · sizing.evaluate
The median across all futures is small next to “Median of the memory chain alone” (it looks like a rounding error) because the zeros take the lower part of the shortfalls and the middle value lands just past them. The average across all futures is larger, pulled up by a minority of futures where another chain asks for far more than the memory chain. So “median across all futures” makes the shortfall look harmless, but “averaged over every future” does not.
At the point estimates in the first section, the three chains ask for nearly the same count. So which chain wins changes with small moves in the inputs, and there is nothing unusual about a neglected chain winning. When it does, it can win by a lot: each chain’s count has a long upper tail, with a small share of futures running on to a count far past the rest.
Count only the futures where the fleet is short, and the zeros drop out. Problem 10.2 asks for the median counted that way. The summary of sizing on one chain is two numbers: how often it is wrong, and how badly when it is; one of them alone is a way of not answering.
Why this gets worse with more chains
The web service has three. A real service has more: a database’s connection limit, a cache’s eviction rate, the network between the hosts, a licence tier. Each is another chance for the answer to be set by something nobody was watching.
The chance that some constraint binds unexpectedly rises with the number of chains, even while the chance of any particular one doing so stays small. Say six chains, each of which surprises you one time in ten. If the surprises are unrelated to each other, the chance that none of them happens is nine-tenths multiplied by itself six times, which is a little over a half. So a model with six such chains spends about half its life with something nobody was watching in charge, and every one of the six looked safe on its own.
But that arithmetic treats every chain as a separate gamble. The chains in this service share the growth factor, so they tend to surprise you in the same futures. The chance that none surprises you is then higher than the arithmetic gives. It still does not follow that more chains are safe: adding a chain never makes it less likely that some chain surprises you.
The observability model in Appendix F also has three chains, one each for metrics, logs and traces. There the chains are different workloads that share one ingest pipeline and one store, so their demands add. Adding is right there, wrong here: here the three chains are three demands of one workload on the same hosts. What carries over is the ceilings: its three tiers carry four ceilings, each checked on its own. No single number summarises them, and a model that produced one would hide the thing you needed.
Two generations in one pool
The web service model buys one kind of host. In practice, the fleet you plan often joins hosts you already run: last generation’s machines, bought for the same job and still in service. The question changes. You no longer ask how many hosts; you ask how many new hosts, given that the old ones remain.
A second model file, models/mixed_pool/model.yaml, asks that question. Its demand is the
web service’s own chain, copied node for node with the same ranges, so its answers sit beside
this chapter’s. The old hosts have fewer cores and more memory than the new ones, and the same
disk. How many old hosts stay in service to the horizon is a decision in the file.
Each chain first counts what the old hosts already give: cores, memory or disk. It takes that from what the chain needs, and divides what is left by what one new host gives below its margin. If the old hosts cover the whole need, that chain asks for no new hosts. Routed by capacity, you buy the largest of the chains, as before. Routed equally, the chains are not independent: once any chain buys a new host, that host is in the pool, every host is held to the smallest host’s cores, and the request chain has to be worked out again on that basis.
new_for_requests:
kind: derived
unit: host
label: new hosts the requests need, routed by capacity
formula: ceil((max(busy_cores / (1 - queueing_margin), old_cores) - old_cores) / cores_per_new_host)
| Chain | All new hosts | Old hosts kept, routed by capacity | Old hosts kept, routed equally |
|---|---|---|---|
| Requests | 50 | 35 | 70 |
| Memory | 54 | 0 | 0 |
| Disk | 49 | 19 | 19 |
| New hosts to buy | 54 | 35 | 70 |
Source — mixed-pool, mixed_pool-reference and mixed_pool-replace · sizing.evaluate
The first column shows an all-new fleet, which is this chapter’s answer. The second shows the mixed pool with requests routed by capacity. In the first column memory asks for the most new hosts. In the second, the old hosts’ extra memory covers the working set, so memory asks for no new hosts, and requests decide instead. Old hosts rich in one resource move the binding constraint to another.
The third column changes one thing: how requests are spread across the hosts. Routed by capacity, the load balancer sends each host requests in proportion to its cores, so every host is equally busy and the old and new hosts’ cores add up. Routed equally, every host gets the same share. A host’s utilisation is its share of the busy cores divided by its own cores (ch05), so the smallest host is the busiest. It reaches its margin first while the new hosts still have cores to spare. The pool then carries only as much as all its hosts times the smallest host’s cores, and the request chain asks for more new hosts.
Routed equally, keeping the old hosts means you buy more new hosts than replacing them all. The table shows it: the last row of the third column asks for more than the last row of the first column. Every host, including every new one, is held to the old host’s cores, so each new host counts for less.
Which routing you run is a fact about your load balancer, not a choice the model can make for you. Find out before you buy. Data is placed by capacity in both mixed-pool columns; only requests change. ch11 checks the pool as bought against both.
The mixed pool model, with the old hosts kept. Drag old hosts still in service at the horizon to zero and watch the Outputs list under the graph. With no old hosts the two routings ask for the same count, because every host in the pool is then the same size. The graph also holds the ceilings ch11 checks and the costs ch22 compares; those come later.
What this cannot tell you
Whether there are only three chains. This model has the three chains the book chose. A chain that is not in the model cannot bind inside it, however often it binds outside. For this service the likeliest missing chain is network bandwidth: ch09’s erasure coding saves disk by requiring each read to touch more machines, and the read traffic each host can carry would give a host count of its own. This model has no network chain. The missing chain is ch20’s subject, and nothing here addresses it.
What a request costs. The request chain rests on the CPU time one request costs, which the model
file declares an assumption, not a measurement, because no reference machine is declared. You can
read the declaration by clicking CPU time per request and opening In the file under Details. A
measured figure would be a rig result nobody has taken—the kind quoted from a bench with nothing
else running.
Whether another host adds a whole host. The request chain divides the cores busy at the busy hour by what one host can carry, assuming each added host adds a whole host of work—that is, a straight line. However, ch07 showed this assumption does not hold. The model checks the fleet you decide on against coordination in the ceiling utilisation, counting coordination, but the chain that recommends a count leaves it out. So wherever coordination costs something, the request chain asks for fewer hosts than the requests need.
Any feedback between the chains. The chains share inputs: the growth factor, and for memory and disk the records held, but in this model none acts on another. In a real service, a working set that no longer fits in memory turns memory reads into disk reads, raising the cost of a request and moving the request chain. The model flags this with the ceiling working set against memory, but nothing carries that into the CPU time per request. That is ch08’s regime change running through the sizing arithmetic.
Which chain binds for you. The shares above come from one model’s uncertainty over one stated workload. A service that serves small records to many users and one that holds large records for a few are the same model with different inputs and opposite answers.
Pools of more than two generations, or hosts shared by containers. The mixed pool holds an old generation and a new, each host doing the whole job. Pools with additional generations would require each generation’s nodes to be written out separately, because a model file has no lists. Hosts carved into containers of different sizes pose a packing problem, and this model does not pack them.
Key takeaways
Three chains size the same fleet, and none of them is the answer. Requests, memory and disk each ask for a host count from a different arithmetic. They share the workload and its growth, so they tend to rise and fall together, but none can be worked out from the others.
Buy the largest, not the average, the usual winner or the sum. The average falls short of the largest chain whenever the three differ. The usual winner falls short whenever another chain asks for more, which is more often than not. The sum buys more than any chain asked for, in every future.
No chain wins most of the time. Size on any single chain, even the one that wins most often, and the fleet is too small more often than not.
The summary of sizing on one chain is two numbers. How often it is wrong, and by how much when it is. Either one alone is a way of not answering.
Old hosts rich in one resource move which chain binds. Routed equally, every host counts only up to the smallest host’s cores, so the new hosts’ extra cores are wasted, and keeping the old hosts can mean buying more new hosts than replacing them. Find out which routing you run before you buy.
More chains mean more chances to be caught out. The chance that some constraint binds unexpectedly rises with their number, even while each one’s chance stays small.
Problems
Five problems. The first three have tests, in tests/bandwidth_and_the_binding_constraint/. The last two have no test.
10.1 — Three chains, one purchase.
The function size_for_all receives the three chains’ host counts, one count per future in each. It
returns one count for every future, the count that satisfies all three chains in that future—not one
number for the whole set. One function call. Work out what happens under each of the three obvious
wrong answers before writing the right one.
def size_for_all(
request_hosts: np.ndarray, memory_hosts: np.ndarray, storage_hosts: np.ndarray
) -> np.ndarray:
"""Problem 10.1 - three chains, one purchase.
Three chains each say how many hosts the workload needs: one from how many requests arrive at
the busy hour, one from how much of the data has to stay in memory, one from how much of it has
to sit on disk. Each argument is an array with one host count per future; position i in each is
the same future. Return an array of the same length: for every future, the count that satisfies
all three chains in it. One number for the whole array is not an answer.
It is one function call and the point is which one. Work out what happens under each of the
obvious wrong answers before you write the right one:
* the **average** of the three falls short of the largest chain whenever the three differ;
* the **working-set** chain, taken because it wins most often, is too small whenever another
chain asks for more - which the next problem shows is more often than not;
* **adding** them buys more than any chain asked for: a fleet for a workload nobody has.
"""
raise NotImplementedError("problem 10.1")The same check at a desk: python3 -m pytest tests/bandwidth_and_the_binding_constraint/test_problem_1_all_three.py -m problem
10.2 — The cost of sizing on the chain that usually wins.
The function cost_of_sizing_on_one receives the chain that usually wins, as chosen, and the
other two, as others. It returns two numbers. The first is the share of futures in which a fleet
sized on chosen alone is too small—a fraction between nought and one, not a percentage. A future
counts as too small only where some other chain asks for strictly more; a tie is not a shortfall.
The second is the median shortfall, in hosts, in those futures only. The first is larger than
“usually wins” suggests, and the second is what stops the shortfall looking harmless.
def cost_of_sizing_on_one(
chosen: np.ndarray, others: tuple[np.ndarray, ...]
) -> tuple[float, float]:
"""Problem 10.2 - what it costs to size on the chain that usually wins.
Suppose you size on ``chosen`` alone - the working-set chain, say, because in more of the
futures than any other it asks for the most. ``others`` is a tuple of the chains you ignored.
Each is an array with one host count per future, all the same length. Return two numbers, as a
tuple:
* the fraction of futures, between 0 and 1, in which that fleet is **too small**, because some
other chain asks for strictly more. A tie is not too small;
* the median shortfall **in those futures only**, in hosts.
A chain that wins more often than either of the others can still lose to one of them more often
than not. "Wins most often" is a statement about a three-way race, and "too small" is a
statement about losing to anybody. A shortfall counted over every future includes a zero for
each future where the chosen chain was big enough, and its median looks like a rounding error.
Counted over the short futures only, it is not one, because a neglected chain that wins often
wins by a lot.
"""
raise NotImplementedError("problem 10.2")The same check at a desk: python3 -m pytest tests/bandwidth_and_the_binding_constraint/test_problem_2_cost.py -m problem
10.3 — Old hosts and new, routed two ways.
The function new_hosts_for_requests receives the cores the busy hour keeps busy, how many old
hosts stay in service, the cores on each old host, the cores on each new host, the queueing margin,
and a routing: "capacity" or "equal". It returns how many new hosts the request chain needs.
Routed by capacity, the two generations’ cores add up. Routed equally, the smallest host in the pool sets the pace. With no old hosts left, the smallest host is a new one. Routed equally, the old hosts carry the load if, on their own, each held to its own cores, they serve the busy hour below the margin; then the answer is no new hosts. The smallest host is a new one only once a new host exists. Either routing, the answer is never a negative number.
The test checks your answer against the mixed pool model’s own nodes: with no old hosts, with some, with the book’s pool, with more old hosts than the load needs, and with old hosts that have more cores than the new ones.
def new_hosts_for_requests(
busy_cores: float,
old_hosts: int,
cores_per_old_host: float,
cores_per_new_host: float,
margin: float,
routing: str,
) -> int:
"""Problem 10.3 - how many new hosts the requests need, when some old ones stay.
``busy_cores`` is how many cores the busy hour keeps busy. ``old_hosts`` are already in
service, each with ``cores_per_old_host`` cores; every new host has ``cores_per_new_host``.
``margin`` is the queueing margin: a share of every host's cores the fleet keeps free, between
0 and 1. Return the number of new hosts to buy, as a whole number, for ``routing``:
* ``"capacity"``: the load balancer sends each host requests in proportion to its cores, so
every host is equally busy and the two generations' cores add up;
* ``"equal"``: every host gets the same share of the requests, so the smallest host in the
pool is the busiest and reaches its margin first. With no old hosts, every host is a new
one. Routed equally, the old hosts carry the load if, on their own, each held to
its own cores, they serve the busy hour below the margin; then the answer is
no new hosts. The smallest host is a new one only once a new host exists.
Either routing, the answer is never a negative number.
"""
raise NotImplementedError("problem 10.3")The same check at a desk: python3 -m pytest tests/bandwidth_and_the_binding_constraint/test_problem_3_mixed_pool.py -m problem
10.4 — Which chain binds for you. No test: which chain binds depends on quantities only you have.
This chapter has three chains because this model has three. Work out the ones for your system: the quantities that each, on their own, decide how many machines you need. Then work out which binds first.
The useful part is the margin. If one chain binds at twice the other, the second is free capacity you are paying for and nobody is counting. If they bind within a few per cent of each other, your sizing is balanced and also brittle. A small change in either moves which one is in charge, and the argument you rehearsed about the first chain stops applying.
A good answer names at least two chains, says which binds, and by how much. If you can only find one chain, you have found an assumption rather than a fact.
10.5 — Your own mixed pool. No test: how your load balancer spreads requests and when your old hosts retire are facts only you have.
List the generations of hosts in service for one job, with each one’s cores, memory and disk per host. Find out how your load balancer spreads requests, by capacity or equally, from its configuration rather than from memory. Name the host with the fewest cores and the host with the least disk. Under equal routing the one with the fewest cores sets the pace, and every shard has to fit on the one with the least disk. For each old generation, write down the date it leaves.
Work out the new hosts each chain asks for under the routing you actually run, twice: with the old hosts in service, and on the day the oldest generation retires.
A good answer names the routing and how you confirmed it, the host with the fewest cores and the host with the least disk, the retirement dates, and the purchase that still holds on the worst day.
What would show it wrong: the load balancer’s configuration says the other policy; a kind of host you left off the list; a retirement date earlier than the one you planned for.
Where to go next
Each chain divides by a capacity less a margin: the request chain by a queueing margin, the memory chain by a cache margin, the disk chain by a disk margin. ch11 sorts margins into three kinds by what they protect: a capacity margin against a cliff, a queueing margin against a slope, a scaling margin against a budget. It also asks what happens when a host dies at the busy hour.
ch12 puts all of Part III together and produces a number.