prefix-routing -- the load balancer that knows which replica holds your prefix

A busy service runs many identical copies of a model, and something has to decide which copy answers you. The obvious choice is whichever is least busy, but each copy separately remembers work it has already done, so a request whose long standing instructions were already read by copy 2 gets sent to copy 3 and pays to read them all over again. A router that instead sends you where most of your text is already known avoids that — until it becomes a trap: everyone sharing one popular set of instructions is sent to the single machine holding them, which forms a queue while the others sit idle. What beats both is a router willing to abandon the shortcut when a machine gets busy.

Interactive page: how a serving runtime picks which replica serves a request when several replicas of one model each keep their own prefix cache -- and why a router that only follows the cache is worse than round-robin.

Anchor: A4 KV cache / runtime shape (Family J, serving time).

The layer this page sits on

radix-attention shows prefix reuse inside one replica: two requests that begin with the same tokens share one KV path in a radix tree, so the second one prefills only its suffix. This page is the layer above it -- reuse across replicas, where the router has to choose.

The reuse radix-attention buys is per replica, so it only exists if the request arrives at the replica that already holds the prefix. Round-robin does not know that: a request whose long system prompt is already computed on replica 2 lands on replica 3 and recomputes every block of it, and the tree on replica 2 ages out unused. A prefix-aware router hashes the prompt's leading blocks and sends the request where the matched prefix is longest. Same mechanism, one level up.

What it shows

A seeded stream of requests arriving at N replicas. Each request is a run of fixed-size blocks: the leading blocks are a shared system prompt drawn from a skewed popularity distribution, the trailing blocks are that request's own text and are shared with nobody. Each replica has a FIFO queue and an LRU block cache. Three panels:

The failure, which is the point

The routing score is

score(replica) = w · matched-prefix-fraction − (1 − w) · relative-backlog

and w is a knob the reader drags. Both ends are reachable and both are bad:

The popularity skew is the second draggable, and it is what decides whether affinity is worth anything at all: with many equally popular distinct prompts there is no hot prefix to follow, the green curve flattens, and affinity only costs balance. With one dominant prompt affinity pays the most and pins the hardest.

Every number -- hit rate, mean TTFT, tail (p95) TTFT, throughput, per-replica utilisation -- comes out of the simulated run and is recomputed from scratch whenever a control moves. Ratios are reported as a percent of the round-robin baseline, which is the same arrival stream routed k mod N ("TTFT 71% of round-robin", lower is better; "throughput 103% of round-robin", higher is better). Nothing on the page is annotated.

Sources for the mechanism

Both public:

Render tier

T1 (Canvas2D throughout -- the lesson is a scheduling decision, not pixel scale).

Wiring

layout.mount + controls (replicas, w, skew, prompts, capacity, arrivals, seed) + a one-step-per-arrival Transport (autoplay, loop), onPointer drag on the ◆ weight marker and the ▲ skew handle, hover-to-inspect on replica cards / arrival dots / the chart / the popularity histogram, three challenges, and an A/B compare over w = 0 against w = 1. URL hooks: every control (?w, ?skew, ?replicas, ?prompts, ?capacity, ?arrivals, ?seed) plus ?step=N, ?hover=x,y, ?play=1 -- seeded, so one URL replays exactly one arrival stream at exactly one router setting. Source: page.js.