Add approximate prefix routing, and find out it did nothing

Tier 1 of plan §7.5: a radix tree of request prefixes that estimates which
replica has already prefilled one. Bytes, not tokens — needing the engine's
exact tokenizer is what makes tier 2 a separate project.

It went in inert, and the decision metric is what caught it: 144 lookups, zero
hits. The cause was a rule I had written and tested for. A node's label is the
first request that created it, so the second request for the same system
prompt diverges in its last few bytes — a new question after the same
preamble, the single most common shape there is. Requiring the whole label to
match threw away the 700 bytes they shared. What matters is the shared head,
because that is what the engine cached, and a partial label match now counts.
Two unit tests asserted the wrong rule and have been inverted.

With that fixed, measured end to end against engines whose caches are too
small for the working set: 22.5% cache hits without, 81.7% with, and the
breakdown shows the misses are exactly the first round, when the tree is
empty.

The scenario matters as much as the number. With a handful of prompts and a
large cache, scattering is free — every replica ends up holding everything —
and prefix routing can only add constraint. The first version of the test
measured that case and showed a loss, which was a true statement about a
situation the feature is not for.

Two things keep it from being the disaster prefix routing usually is. The
imbalance guard, which needs both an absolute and a relative gap before it
fires, because either alone misfires — absolute on an idle cluster, relative
on a busy one. And it is a scorer rather than a filter: affinity is a vote the
queue and inflight scorers can outvote, not a rule.

The tree records the replica the picker actually chose, not the one the scorer
proposed, and a node prefers the replica it has seen most rather than most
recently — a count is stable under a randomizing picker, where recency just
chases whatever the picker last did.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5 committed 2026-09-16 12:13:19 +02:00
1 parent de21649ad6
commit a075ff4120
13 files changed
+1207 -7

No files matched your search

+18
View File
@@ -68,12 +68,27 @@ func withLoadedEngines() func(*config.Server) {
return nil // marker; see start()
}
// tightPrefixCache shrinks the engines' prefix cache so the working set does
// not fit in any one of them.
//
// That condition is the whole reason tier 1 exists. With a cache large enough
// for every prompt, scattering is free — every replica ends up holding
// everything — and prefix routing can only add constraint. It is worth knowing
// the feature does nothing there rather than assuming it helps everywhere.
var tightPrefixCache = func(c *sim.Config) { c.PrefixCacheBlocks = 96 }
// start brings up n sim-engines behind a router.
//
// A nil tune function is the "small engines" marker, which keeps the call sites
// readable without a second variadic parameter.
func start(t *testing.T, n int, tune ...func(*config.Server)) *harness {
t.Helper()
return startTuned(t, n, nil, tune...)
}
// startTuned is start with the sim-engines themselves adjusted.
func startTuned(t *testing.T, n int, engineOpts []engineOpt, tune ...func(*config.Server)) *harness {
t.Helper()
small := false
kept := tune[:0]
@@ -99,6 +114,9 @@ func start(t *testing.T, n int, tune ...func(*config.Server)) *harness {
sc.DecodeTokensPerSec = 800
sc.BatchContention = 0.005
}
for _, opt := range engineOpts {
opt(&sc)
}
e := sim.New(sc)
srv := httptest.NewServer(sim.NewServer(e, slog.New(slog.DiscardHandler)))
h.engines = append(h.engines, engine{eng: e, srv: srv})