A store asks us for something specific. One wants search that still works when a shopper misspells a word. One wants similar chandeliers shown under the chandelier a shopper is looking at. One wants to help people choose a size. Building the machine learning behind any one of these usually takes a team a few months. I’m working on getting it down to hours. Our best internal drill so far took 9.5 hours from the request to a model serving shoppers, and the goal is three.
I wrote about what the shopper sees in Personalization on Demand. This post is about what is behind it. It covers four things we changed: where the ranking is computed, how a new version goes live, how we decide a model is good, and how I work with the coding agents that write most of the code.
The stores in the examples are made up, and the numbers in the figures are examples I chose to show how each thing works.
1. Rank ahead of time, check the price at the last moment
Two things about a product change at very different speeds. What a shopper is likely to want changes slowly. I can learn it from the last few weeks of visits, and it will still be true an hour from now. The price, and whether the product is in stock, can change at any minute.
The answer also has to be fast. My target is that nine calls in ten finish within 300 milliseconds. If our answer comes late, the store’s own products paint first and then ours replace them under the shopper’s thumb. A late answer is worse than no answer.
Our first version did all of its work when the request arrived, in a Python service. Every request paid for the whole computation, and a change for one store waited behind changes for the others.
So we split the work by how fast it changes. The ranking is computed ahead of time. For each common search prefix, each collection and each product, we save the ordered list of products we would show. When a shopper types “lam”, the engine looks up the saved list for “lam” instead of computing one. Then, for each product it is about to show, it reads the current price and stock from the store. If a price we would show doesn’t match the store’s, that card is left out.
The engine is written in Rust and runs on Cloudflare Workers, which puts it close to the shopper. Every lookup has its own deadline. If a lookup is late, we return nothing and the store shows its own ordering.
What the shopper sees after typing “lam”
- Brass table lamp$249
- Linen drum lamp$189Left out: the store says $169
- Arched floor lamp$310
The store’s own ordering
The answer is ready well before the 300 ms target. The shopper sees our three lamps.
What the shopper has done in this visit travels with the request, so the ranking can use a product they looked at five seconds ago. What they did on earlier visits is stored per shopper, and reading it has a deadline too. If that read is late, we rank from this visit alone.
2. One unit for each API, and a release is one pointer
Each store wants something a little different. A lighting store wants chandeliers that go with the one on the page. A clothing store wants to help people choose a size. A cooking school wants to suggest classes.
At first each store had its own folder of code in one shared repository. Every change for one store went through the same pull requests and the same release as every other store, so a small change for one could wait behind a large change for another.
Now the backend is split into units, one for each API of each store. A store’s search is one unit and its product page recommendations are another. A unit is a record that says what it serves, which features it uses, which model, and how that model is trained. It starts from one of five templates (search, collection page, item recommendations, home page, content), or from its own Rust code where no template fits. When we fix something in a template, every unit of that kind gets the fix.
A version of a unit is a build: the code, the model and the saved tables, stored together and named by a hash of their contents. A build can’t change after it is made. What shoppers get is whichever build the unit’s pointer names. Releasing a new version is moving that pointer, and rolling back is moving it back. No store’s release waits for another’s.
Unit: search for a lighting store
- Serves
- The search box’s drop-down
- Features
- Words typed, product titles, this visit’s views
- Model
- Gradient-boosted trees
- Training
- The last eight weeks of visits
Builds
- livea3f9c2Code, model and saved tables
- live7d41e8Code, model and saved tables
- no hashSample check fails: a price arrives as the text “$449”. Never trained.
The pointer names build a3f9c2, so shoppers get it. Build 7d41e8 is made and waiting.
Two checks sit on either side of a build. Before training, the recipe has to work on a small sample within ten minutes. That catches a broken data feed, such as prices that arrive as the text “$449” where a number should be, in minutes. Without it we would find out hours later, after paying for the full training run. After the build is made, it runs next to the live one on copies of real calls, then takes a growing share of visitors. It rolls back on its own if timeouts or empty answers rise.
3. Which model, and what it learns from
I don’t want one kind of model for everything. Gradient-boosted trees are hard to beat for ranking products by expected revenue from a table of features. A two-tower network helps with a search like “pants I can wear on a long flight”, where none of the words appear in a product’s title. A sequence model can use the order of what a shopper looked at across visits. Sometimes a table of counts is enough, and it answers in microseconds.
Whatever the model is, it has a simple rule to beat. For recommendations, that might be best sellers or the products people often view together. A new model goes live only if it scores above both the rule and the model it replaces, on shoppers it wasn’t trained on. If the simple rule works better, we ship the rule.
The model learns from what shoppers did. A purchase counts more than adding to the cart, which counts more than a click, and a purchase is weighted by its revenue. That weight needs a cap. Without one, a single buyer who orders forty dining chairs in one checkout would outweigh hundreds of ordinary orders, and the model would show dining chairs to everyone for weeks.
One week of orders, by revenue
120 ordinary orders, $18 to $190 each
What the model ranks first
- Oak dining chair
- Walnut dining chair
- Cane dining chair
- Upholstered dining chair
- Oak dining armchair
- Brass table lamp
- Wool area rug
- Linen throw
- Oak dining chair
- Ceramic vase
Cap off: the chairs order would count as $7,200, as much as about 100 ordinary orders. It would be 46% of the week’s weight, and dining chairs would fill the top five.
A store with too few orders gets the simple rule until it has enough for a model to learn from.
4. Why a better model can lose the test
We measure everything with a live test: our version against the store’s own, on real shoppers, and I read each test as the chance that our version beats the store’s. A test can come back negative when the model is better. One way that happens is the script that puts our results on the page, and the arithmetic is short. Say 4% of visitors open search, and our search makes those visitors 14% more likely to buy. Say our script also loads on every page, makes each page a little slower, and makes the other 96% of visitors 3% less likely to buy.
- Searchers: 0.04 × 14% = +0.56%
- Everyone else: 0.96 × −3% = −2.88%
- Whole site: −2.32%
The test says we lost. The small cost falls on 24 times as many people as the large gain.
- Searched: 14% more likely to buy
- Did not: 3% less likely to buy
- Searchers: 0.04 × 14% = +0.56%
- Everyone else: 0.96 × −3% = −2.88%
Whole site: −2.32%
Two habits came out of this. Our script does nothing until the shopper touches the thing it serves, so a visitor who never opens search never pays for it. And before I decide that a model lost, I look at the visitors who used it and the visitors who didn’t, separately.
5. The agent that did the work doesn’t get to say it’s done
A small team and a lot of agents are building this. The agents write code, run training and review each other’s work. We set the goals, check the results and decide what goes live. I work by telling the agents what I want, and I don’t write the code by hand.
An instruction moves through a few fixed roles. One agent records it and picks the one case that has to pass in production. One assigns the work. One owner does the work from start to finish: it writes, tests, merges, releases and watches production. I’ve found that one owner who knows the whole piece does better than several agents handing notes to each other.
The part that mattered most is the last step. Agents often said work was done when it wasn’t. A test had only run against a stand-in, or it had never run at all. So every “done” now has to come with a check that can be run, and a plain script reruns that check. No model is involved in the rerun. The work counts as done only if the rerun passes. A check only counts after it has failed on a bad change and passed on a good one.
- MeI say what I want.
- An agentRecords it and picks the case that must pass.
- An agentAssigns it.
- The ownerDoes the work and says it’s done.Back to the owner
- A scriptReruns the owner’s check.Passes
The work merges and goes live.
The rerun passes, so the work counts as done. It merges and goes live.
6. Five rules that always hold
Five rules apply no matter what I ask for. A test launches only when the person responsible for it approves. Only one agent can change a live test. One store’s data never goes to another store. Keys and passwords are never printed. Code merges only through the merge queue, after tests and a security review.
Everything else has to earn its place. A check that blocks work is kept only if it has caught a real fault. When a check has gone a month without catching anything, we remove it, because each one slows down every owner every day.
7. When agents get in each other’s way
Two problems showed up once many agents were merging changes every day.
The first is files that everyone edits. A long list of registered units in one file, or one big test file, gets changed by pull requests that have nothing to do with each other, and they collide. Nobody owns a shared file, so it never gets split up on its own. We give each file one owner, and we turn a list in one file into a folder with one file per entry.
The second is notes that go stale. Agents leave notes for later sessions, and after a while the notes describe paths and workarounds that no longer exist. A new session reads them and repeats an old mistake. We keep the shared notes short, put each repeatable procedure in its own short file, and delete notes that nothing has used for a while.
What isn’t done
The slowest tenth of our calls still take longer than 300 ms, and that is the number I’m working on now. The time from a request to a serving model is 9.5 hours at best, against a goal of three. I’ll write again when those change.