November 25, 2026

Where should the bytes live?

Every storage system has a few fast places and many slow ones: memory and disk, a cache and the database behind it. The fast places are small, so something has to decide which data gets them.

This post works that decision out on the smallest system I could draw: 24 pieces of data, five tiers of storage, and a path to them one byte wide. The figures are live. They come from a system design course I'm building in S4mple, and each one is the same simulator at a different point in the argument.

A piece of data

Start with one piece of data. I'll call it a byte, though it stands for anything a system stores and fetches as a unit: a row, a page, a file. Its label is its name.

Twenty-four of them

Here are 24. They drift, because nothing yet says where any of them belongs. No byte matters more than another until something reads them.

The queries

What reads them is a workload: here, ten queries. Each query is a short list of bytes that have to arrive together. Under the bytes is the list, a card for each query.

Press play. The queries are read one after another, some far more often than others. Each one lights the bytes it reads, and bytes that are read together pull toward each other. After a few dozen reads the 24 have gathered into three groups. Nobody designed the groups. They are what the workload looks like.

One byte at a time

Now put the bytes where storage keeps them: in a line, one slot each. The bar over a byte is how often it has been read, 300 queries in.

Assume the path to them is one byte wide: each fetch brings back one byte, and the next fetch starts when it is done. That is the bandwidth, one byte at a time. A query that reads four bytes waits for four fetches, one after another, and nothing else is fetched meanwhile. Everything this post adds up rests on that.

For now they sit in the order of their names, which is no order at all. The tall bars are scattered along the line.

Fast slots and slow ones

The slots are not equal. The line is cut into five tiers. The first holds 4 bytes and costs 1 to fetch from. After it come 6 slots that cost 4, 6 that cost 12, 6 that cost 40, and a last tier of 2 that costs 200.

Think of the first tier as memory and the last as something you would rather not wait for. The unit doesn't matter. What matters is that the last tier is 200 times slower than the first, and that the first has room for four.

What a query costs

A query has to fetch every byte it reads, and they come one at a time, so a query costs the sum of its bytes' tiers. Averaged over the workload, that comes apart byte by byte: each byte's share of the reads, times the cost of the tier it sits in, added up. The sum is written under the line, a group for each tier.

With the bytes in name order it comes to 50.7. The average query pays fifty times what the fast tier charges, because popular bytes are sitting in slow slots.

The same number sets the throughput. The path is busy with one query until that query's last byte arrives, so with queries waiting back to back it finishes one every 50.7, on average.

Counting is enough

The fix is the one you would guess. Press by count. The most-read byte takes the cheapest slot, the next most-read takes the one after, and so on down the line. The sum falls from 50.7 to 10.4, so the same path now serves nearly five times as many queries.

Is there anything better? Press best mean, which searches for the placement with the lowest average. It finds nothing better. With the workload held still, sorting by count is the best there is for the average.

The reason is in the sum. Only each byte's own count appears in it. Which bytes are read together never does. Under the sum is what each query costs, drawn as a curve, and the sum is that curve's mean.

What an observer sees

An average hides the queries, so here they are. Under the curve each query is a block: as wide as how often it is read, as tall as what it costs, built from the tier costs of its bytes. The curve above is these heights, counted.

After 300 reads the mean is 10.4 and most of the blocks are low. One is not. The dearest query reads seven bytes together and costs 35, more than three times the mean.

The tail

Suppose the number you answer for is not the average but the slow end: the cost that 95 queries in 100 come in under. Press best p95.

The search moves one byte. 01, which the dearest query reads, goes into the cheapest tier, and 0A, the most-read byte of all, gives up its slot. The tail comes down from 35 to 32. The mean goes up from 10.4 to 11.1.

That is the whole trade in one move. Counts set the mean. Which bytes are read together sets the tail. The placement that is best for one is not the best for the other, and you only find out which one you have by looking at the queries, not at the bytes.

What this leaves out

One byte at a time is the easy case. The fetches queue, so a query costs a sum, and a sum does not care what is read with what. Give the system width, several paths that fetch at the same time, one per machine say, and a query waits for its slowest path instead. The sum becomes a maximum, and which bytes are read together becomes the whole question. That is where this goes next.