Sizing and TCO

Appendix D · Units, and the conversions that bite

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.

CalculationThe unitsResultWhat happened
a request rate, for a durationrequest/second × secondrequestthe seconds cancel: a rate times a duration is an amount
a request rate, times a plain numberrequest/second × dimensionlessrequest/secondnothing cancels: a plain number leaves a rate as it was
disk per host, times the hostsTB/host × hostTB
the hosts cancel: an amount per host times a count of hosts is an amount
raw data, divided by disk per hostTB ÷ TB/hosthost
the terabytes cancel and the hosts come up: the last step of a sizing chain
CPU time per request, times the request ratesecond*core/request × request/secondcore
the requests and the seconds both cancel, leaving cores busy (ch05)
watts per host, times the hosts, times a building multiplierW/host × host × dimensionlessW
the hosts cancel and the multiplier changes nothing but the size
watts, times the hours in a yearW × hour/yearkWh/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 itkWh/year × USD/kWhUSD/year
the kilowatt-hours cancel: energy times a price is money per year
a running cost, over the horizonUSD/year × yearUSD
the years cancel: a rate of spending over a duration is an amount of money
a per-host licence, times the hostsUSD/host/year × hostUSD/yearthe hosts cancel and the years stay: still a running cost
a link rate, for a duration, read in bytesMbit/second × secondMB, 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 hostsGiB/host × hostTB, 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 monthUSD/TB/yearUSD/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 yearyear ÷ yeardimensionless
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:

CaseFormulaNode declaresVerdict
a request rate, for a durationrate * window, rate in request/second, window in secondrequestaccepted 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-yearprice in USD/TB/yearUSD/TB/monthconverted: the build divides by 12
watts, times the hours in a yearpower * hours, power in W, hours in hour/yearkWh/yearconverted: the build divides by 1,000
a drive as the sheet quotes itdrive in TBTiBconverted: the build multiplies by 0.909495
a link rate, for a durationlink * window, link in Mbit/second, window in secondMBconverted: the build divides by 8
bytes per span, times spans per requestsize * spans, size in byte/span, spans in span/requestbyte/requestaccepted as written
bytes per span, times spans per requestsize * spans, size in byte/span, spans in span/requestbyte/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 binaryraw + spare, raw in TB, spare in TiBTB
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:

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.

ModelNodeFormula producesNode declaresThe build
mixed_poolenergy per yearUSD*hour*watt/kilowatt_hour/yearUSD/yeardivides by 1,000
mixed_poolmemory the service can use, per new hostgibibyte/hostTB/hostmultiplies by 0.00107374
mixed_poolmemory the service can use, per old hostgibibyte/hostTB/hostmultiplies by 0.00107374
observabilitylogs ingestbyte/secondMB/sdivides by 1,000,000
observabilitylogs storedday*megabyte/secondTBmultiplies by 0.0864
observabilitymetrics ingestbyte/secondMB/sdivides by 1,000,000
observabilitymetrics storedday*megabyte/secondTBmultiplies by 0.0864
observabilitytraces ingestbyte/secondMB/sdivides by 1,000,000
observabilitytraces storedday*megabyte/secondTBmultiplies by 0.0864
web_servicecost per million requestsUSD/requestUSD/megarequestmultiplies by 1,000,000
web_servicecost per stored TB per monthUSD/terabyte/yearUSD/TB/monthdivides by 12
web_servicefacility powerwattkWdivides by 1,000
web_servicememory the service can use, per hostgibibyte/hostTB/hostmultiplies 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
By Chris Snow, in collaboration with Claude (Anthropic) · Prose and figures: CC BY-NC 4.0 · Code and models: Apache 2.0