Appendix D · Units, and the conversions that bite
What this holds
| Purpose | TB against TiB, bits against bytes, month lengths, and why the build converts |
| Source | sizing/units.py, sizing/evaluate.py |
A sizing model is a chain of multiplications. One way for it to be wrong is to multiply two quantities that should never have met: series by requests, bytes by seconds, a per-node figure by a per-core one. A spreadsheet cannot see any of these: a cell holds a number, and the number carries no unit. Every node in this book’s models declares its unit, and the build works out the unit each formula produces and compares it with what was declared. It accepts a unit that matches, converts one that differs only in size, and refuses one that is a different kind of quantity. This check is why the book has a build step.
How units combine and cancel
One rule does all of the work: a unit in the denominator of one quantity cancels the same unit in the numerator of another, and nothing else cancels. Requests per second times seconds is requests, because the seconds cancel. Requests per second times a plain number is still requests per second, because a plain number has nothing to cancel with.
The table applies that rule to the combinations a sizing model is made of. Every result in it was worked out by the registry the build uses, not typed, so the page cannot show a combination the toolkit would disagree with.
| Calculation | The units | Result | What happened |
|---|---|---|---|
| a request rate, for a duration | request/second × second | request | the seconds cancel: a rate times a duration is an amount |
| a request rate, times a plain number | request/second × dimensionless | request/second | nothing cancels: a plain number leaves a rate as it was |
| disk per host, times the hosts | TB/host × host | TB | the hosts cancel: an amount per host times a count of hosts is an amount |
| raw data, divided by disk per host | TB ÷ TB/host | host | the terabytes cancel and the hosts come up: the last step of a sizing chain |
| CPU time per request, times the request rate | second*core/request × request/second | core | the requests and the seconds both cancel, leaving cores busy (ch05) |
| watts per host, times the hosts, times a building multiplier | W/host × host × dimensionless | W | the hosts cancel and the multiplier changes nothing but the size |
| watts, times the hours in a year | W × hour/year | kWh/year, and the build divides by 1,000 | power times time is energy, and the build converts to the unit the price is in |
| energy, times the price of it | kWh/year × USD/kWh | USD/year | the kilowatt-hours cancel: energy times a price is money per year |
| a running cost, over the horizon | USD/year × year | USD | the years cancel: a rate of spending over a duration is an amount of money |
| a per-host licence, times the hosts | USD/host/year × host | USD/year | the hosts cancel and the years stay: still a running cost |
| a link rate, for a duration, read in bytes | Mbit/second × second | MB, and the build divides by 8 | the seconds cancel, and the link was quoted in bits: eight to a byte |
| memory per host as the sheet quotes it, times the hosts | GiB/host × host | TB, and the build multiplies by 0.00107374 | the hosts cancel and the build converts binary gibibytes to decimal terabytes |
| a price per terabyte-year, read per month | USD/TB/year | USD/TB/month, and the build divides by 12 | nothing cancels and nothing is wrong: the same dimensions, a twelfth of the size |
| the horizon, divided by one year | year ÷ year | dimensionless | the years cancel to a plain number, which is the only thing an exponent may be |
Source — sizing/units.py, which worked out every row
Two rows matter more than the rest. A rate times a duration is an amount. A rate times a plain number is still a rate. A formula that treats it as an amount is the mistake ch02 shows the toolkit refusing.
A node declares the unit it means to produce. The build works out the unit its formula produces and compares the two. It does one of three things:
| Case | Formula | Node declares | Verdict |
|---|---|---|---|
| a request rate, for a duration | rate * window, rate in request/second, window in second | request | accepted as written |
| a request rate, times a plain number | rate * copies, rate in request/second, copies in dimensionless | request | refused: the formula makes a rate and the node declares an amount, and no factor turns one into the other |
| a price per terabyte-year | price in USD/TB/year | USD/TB/month | converted: the build divides by 12 |
| watts, times the hours in a year | power * hours, power in W, hours in hour/year | kWh/year | converted: the build divides by 1,000 |
| a drive as the sheet quotes it | drive in TB | TiB | converted: the build multiplies by 0.909495 |
| a link rate, for a duration | link * window, link in Mbit/second, window in second | MB | converted: the build divides by 8 |
| bytes per span, times spans per request | size * spans, size in byte/span, spans in span/request | byte/request | accepted as written |
| bytes per span, times spans per request | size * spans, size in byte/span, spans in span/request | byte/second | refused: the formula makes a ratio and the node declares a rate, and no factor turns one into the other |
| raw data, plus a spare drive quoted in binary | raw + spare, raw in TB, spare in TiB | TB | refused: two units of one kind meet in a sum, and the build converts a formula’s result once, not each number inside it |
a working set over memory per host, rounded up to whole hosts | ceil(working_set / memory), working_set in TB, memory in GiB/host | host | refused: the formula rounds before the build converts, so it rounds the wrong number |
Source — sizing/evaluate.py’s unit check, which reached every verdict
The build accepts the formula as written, converts it to the declared unit, or refuses. It accepts when the formula makes exactly the unit declared. It converts when the formula makes the same kind of quantity as declared, in a different size — one factor is recorded for the node, and the whole result is converted by it.
A formula that the unit check can work through is still refused for one of two reasons. First, it
may make a different kind of quantity from the one declared: a rate where the node declares an
amount. No factor turns one into the other, and the refusal names both kinds. Second, two numbers of
the same kind in different units may meet in a sum, a difference, a min or a max, or one may be
rounded by ceil or floor before it is converted, so the build would round the wrong number. The
rest of this page names the refusals that come before the formula: units the build does not accept
at all, and quantities used where only a plain number will do.
One factor applied to a whole result works for products and quotients, where factors multiply through. It cannot work for a sum. Adding a number in terabytes to a number in tebibytes adds them as if the units matched. Scaling the total afterwards does not repair it. The refusal tells you to declare both in one unit, or convert one of them in a node of its own.
The first two rows are ch02’s example and its mistake. The last two rows are the second kind of refusal. The converted rows are what the rest of this appendix is about: the same kind of quantity in a different size, which is the error that looks right.
Counting units are units
The build’s unit registry treats things that are counted as units of their own, not as plain numbers: requests, spans, samples, series, log lines, queries, hosts, nodes, cores, labels, drives, failures, and money in each currency the toolkit knows.
#: The currencies a model may price in, by ISO 4217 code. Each is a dimension of its own, so a
#: model that adds dollars to terabytes is refused, and so is one that adds dollars to euros: there
#: is no exchange rate in the registry, and a model that needs one declares it as a node in its own
#: unit (``USD/EUR``) with a source, like any other conversion. A model's ``currency:`` field names
#: the one its answers are in, and ``scripts/verify-models.py`` refuses an output in any other.
CURRENCIES: tuple[str, ...] = (
"USD",
"EUR",
"GBP",
"JPY",
"CHF",
"CAD",
"AUD",
"NZD",
"CNY",
"HKD",
"SGD",
"INR",
"KRW",
"SEK",
"NOK",
"DKK",
"PLN",
"CZK",
"BRL",
"MXN",
"ZAR",
)
#: Quantities that are counted rather than measured, each its own dimension.
COUNTING_UNITS: tuple[str, ...] = (
"USD = [currency_usd] = usd = dollar",
*(f"{code} = [currency_{code.lower()}]" for code in CURRENCIES if code != "USD"),
"request = [request] = req",
"span = [span]",
"sample = [sample]",
"series = [series]",
"line = [line]",
"query = [query]",
"host = [host]",
"node = [node]",
"core = [core]",
"label = [label]",
"drive = [drive]",
"failure = [failure]",
)
Without these units, spans per request and bytes per span are both plain numbers. Multiply the wrong
pair and you get a plausible answer with no complaint. With them, the formula
request/second × span/request × byte/span produces byte/second, and no other product of those
three is well formed.
Currencies are on the list for the same reason. Adding dollars to euros is as wrong as adding spans
to requests, and without a unit for each currency nothing in the registry would notice. A model
answers in one currency, the one its currency key names, and the build refuses an output in any
other. A price quoted in another currency is converted by an exchange rate: an input of its own, in
a unit such as USD/EUR, with a source.
Each conversion between two counting units costs you one node, whose unit is the conversion itself: spans per request, samples per series, log lines per request, cores per host. That node is an input like any other, so it must say where its number came from:
spans per request (observability model): a measured constant, belonging to one instrumented application at one version; nobody has measured it here.
one sample per series (observability model): a definition — one scrape takes one sample from one series.
log lines per request (observability model): an assumption, from reading the logs.
cores per host (web service model): a vendor’s claim, from the spec sheet.
Only the first is a measured constant in the sense ch03 describes. A counting unit does not make a model conditional; a measured constant does (ch01). What the unit system does is make you write the conversion down as a node with a stated source. A rule in a style guide cannot make you do that.
Dimensions are not enough
Most of what the check does is not refusing. It is converting: two units of the same kind with different sizes. A unit’s dimensions are the kind of thing it measures (a length of time, or money) without its size; terabytes and tebibytes have the same dimensions. The table below lists every conversion the build applies in the book’s models.
| Model | Node | Formula produces | Node declares | The build |
|---|---|---|---|---|
mixed_pool | energy per year | USD*hour*watt/kilowatt_hour/year | USD/year | divides by 1,000 |
mixed_pool | memory the service can use, per new host | gibibyte/host | TB/host | multiplies by 0.00107374 |
mixed_pool | memory the service can use, per old host | gibibyte/host | TB/host | multiplies by 0.00107374 |
observability | logs ingest | byte/second | MB/s | divides by 1,000,000 |
observability | logs stored | day*megabyte/second | TB | multiplies by 0.0864 |
observability | metrics ingest | byte/second | MB/s | divides by 1,000,000 |
observability | metrics stored | day*megabyte/second | TB | multiplies by 0.0864 |
observability | traces ingest | byte/second | MB/s | divides by 1,000,000 |
observability | traces stored | day*megabyte/second | TB | multiplies by 0.0864 |
web_service | cost per million requests | USD/request | USD/megarequest | multiplies by 1,000,000 |
web_service | cost per stored TB per month | USD/terabyte/year | USD/TB/month | divides by 12 |
web_service | facility power | watt | kW | divides by 1,000 |
web_service | memory the service can use, per host | gibibyte/host | TB/host | multiplies by 0.00107374 |
Source — models/
Every row is a formula whose result is the right kind of quantity at the wrong size. Dollars per terabyte per year and dollars per terabyte per month have identical dimensions. A check that compared dimensions alone would pass a unit cost twelve times too large — the cost per stored TB per month that ch17 works with, a figure likely to be quoted in a meeting.
The units library the build uses defines a bit as a plain number, so every amount of data would be a plain number too. A growth factor raised to the power of fifteen terabytes would then pass the unit check. The build’s registry changes that one definition, making an amount of data a kind of quantity of its own, counted in bits. The unit check refuses a formula that makes a plain number for a node declared in TB, a ceiling in TB with a plain number for its limit, and terabytes used as an exponent or inside a logarithm.
Six places a unit goes wrong
Decimal against binary. A drive is sold in decimal terabytes. An operating system reports tebibytes, which are larger, so the same drive shows fewer of them. The gap is nearly a tenth of the capacity and runs in the direction that makes a fleet’s disks smaller than the spreadsheet said. The gap is a fixed fraction of whatever it applies to, not a growing fraction. The web service model’s disk-per-host input says decimal in its provenance for this reason.
Memory runs the other way. A spec sheet says gigabytes and means gibibytes. The web service model
declares memory per host in GiB/host, and its provenance says so; the build converts it to decimal
terabytes per host where memory meets the data. The unit tells the build which of the two you meant.
It cannot tell you which one the vendor meant. The build converts a TiB result into a TB node without complaint.
Bits against bytes. Network is quoted in bits per second and storage in bytes per second, and the factor of eight between them sits at the boundary between two teams. A model that multiplies a link rate by a duration and compares the result to a volume has to get this right once; a model that does not declare units has to get it right every time anybody edits it.
Months. The registry has one, and it is a twelfth of a year, which is no month that has ever appeared on a calendar. That is the right convention for a price per terabyte-month, where nobody means February, and the wrong one for anything that has to reconcile against a billing period. The distinction is invisible until finance does the reconciling, which is why the conversion appears in the table above rather than in the modeller’s head.
Years. A year is not exactly three hundred and sixty-five days. The web service model’s
hours per year input carries the quarter-day. It changes the energy bill by the same small
fraction every year, because energy cost is hours times power times price, so the fraction does
not grow over a longer horizon. It is smaller than the model’s other errors, and free to get right.
Exponents are pure numbers. A duration cannot be an exponent. Compounding growth over a horizon
needs the horizon divided by one period first, which is why both reference models carry a node that
is just one year. It looks like ceremony until the build refuses a growth factor raised to the
power of five years.
Offset and logarithmic scales are refused. The build converts a node by multiplying by one factor. Nought degrees Celsius is not zero temperature, so Celsius converts with an offset as well as a factor. A decibel is a logarithm of a ratio. Neither converts by one factor, so the build refuses both as units. A model that needs one works in a unit that does, such as kelvin, or a plain ratio.
Where Pint runs, and where it does not
Pint is the units library the build uses. It is used by the unit check and nowhere else in the code that works a model out. The check runs before any number is computed — on every model on every push, and again each time the toolkit evaluates a model. It hands back one factor per node, then the units are stripped and the arithmetic is done on plain numbers, each node’s result converted by its factor. The model viewer on the published pages applies the same factors in the browser, with no units library at all.
Pint is kept out of the arithmetic for two reasons. First, the toolkit reruns each model many times
to see how far the answer can move, and numbers that carry units are slow to work with at that
scale. Second, the code that reruns the model (sizing/mc.py) is written to be read end to end,
and Appendix B reads it. A units library inside it would add code
with nothing to do with what that module is for. The check runs once per evaluation, before the reruns begin, so it stops a wrong model without slowing a right one.
Why the check needs a magnitude
Pint works out the unit of a formula by doing the arithmetic on numbers that carry units. So the
check has to give every node a number, not only a unit. The obvious number is one for every node,
but that breaks sound formulas. The web service model works out hosts for storage as
ceil(raw_data / (disk_per_host * (1 - disk_margin))). With every node at one, 1 - disk_margin
becomes zero, and the division fails.
The check then reports an error in a formula whose units are right: a false alarm. That false alarm is how the problem showed up, on a model that was correct. So the check gives each node its real value where one can be worked out. A measured constant with no value, or an input with no point value, is given the value one. If a node’s arithmetic then fails, it is given a value that cancels nothing. A node that still cannot be worked out is reported for what it is, not as a unit error:
def plausible_magnitudes(model: Model) -> dict[str, float]:
"""A realistic number for every node, to do the dimensional pass with.
Not cosmetic. Pint finds a formula's unit by doing its arithmetic, so every node needs a
number. The number one for every node looks reasonable and is wrong. The web service model's
``hosts_for_storage`` is ``ceil(raw_data / (disk_per_host * (1 - disk_margin)))``. With every
node at one, ``1 - disk_margin`` is zero, the divisor is zero, and the check reports a unit
error in a formula whose units are fine. So the arithmetic has to be done on numbers that are
not all the same number.
So each node gets its real value where one can be worked out, and those numbers cannot be
degenerate in a way the model itself is not. Measured constants with no value and inputs with
no point value each get 1.0, and a formula downstream is worked out from that 1.0. If working
out a formula fails, the node is given a value that cancels nothing. If a node still cannot be
evaluated, :func:`check_units` reports it for what it is, not as a units problem.
"""
magnitudes: dict[str, float] = {}
for name in model.order:
node = model.nodes[name]
try:
if isinstance(node, Input):
resolved = point_value_of_input(node, None)
magnitudes[name] = 1.0 if resolved is None else resolved
elif isinstance(node, Measured):
magnitudes[name] = 1.0 if node.value is None else float(node.value)
elif isinstance(node, Derived):
magnitudes[name] = float(_walk(node.formula, magnitudes, SCALAR_FUNCTIONS))
else:
magnitudes[name] = float(_walk(node.of, magnitudes, SCALAR_FUNCTIONS))
except Exception:
# 0.37 rather than 1.0 or 0: it cancels nothing, it keeps `1 - x` positive, and it
# leaves a logarithm defined. Reached when a node's formula cannot be evaluated on the
# numbers above it, such as the 1.0 given to an unmeasured constant.
magnitudes[name] = 0.37
return magnitudes
Running it
python3 scripts/verify-models.py # every formula, in units, on every model
python3 -m pytest tests/test_models.py tests/test_dsl_rules.py tests/test_mixed_units.py # every model typechecks, and each rule on this page