The cache is on another machine

A prompt cache lives in the memory of the machine that built it. Put a load balancer in front of a few replicas and the hit rate you measured on one box quietly goes away. This is why that happens, how a gateway can route on a hash of the stable part of the prompt to get it back, what breaks when a replica dies, and when the honest answer is not to bother.

16 min read

turn 1 · one replica0 from cache · 8,100 paid in full
turn 1 · four replicas, round robin · lands on replica 10 from cache · 8,100 paid in full

In the previous essay I treated the prompt cache as a single thing. A request arrives, the provider looks up the longest prefix it has seen recently, and bills you a fraction for that part. Lay the prompt out well, keep the volatile bits at the end, read the usage block, done. On one machine that story is complete. The thing I skipped is that the cache is a pile of key and value tensors in the memory of a particular GPU, and a request can only use them if it is served by that GPU.

Most systems that serve language models have more than one GPU. A hosted provider has a fleet. A team running vLLM or SGLang on their own hardware has a few replicas behind a load balancer. An internal platform has a gateway, which has its own replicas, in front of all of that. Every one of those layers is a place where the next request from a conversation can land somewhere other than where the last one did, and when it does, the cache it needs is on another machine.

I learned this the usual way. A caching change that cut the input bill by three quarters in a single-instance staging environment did almost nothing in production. Nothing was broken. The usage blocks showed small reads and large writes, request after request, which is exactly what you see when every request is a stranger to the machine that serves it. The figures below run in the browser with no API calls. The fleet is a toy, the prices are real as of November 2026, and the thing to watch in each one is which layer of the prompt is still being served from cache.

One cache per machine

The reason the cache is per machine is the reason it exists at all. The keys and values for a prefix are the output of running the model over that prefix, and they are large: tens of kilobytes per token on a big model, multiplied by thousands of tokens. They live in the GPU’s high-bandwidth memory because that is the only place the attention kernels can read them fast enough to be worth having. Moving them to another GPU over a network is possible, and some systems do it, but it is slow enough and awkward enough that the default everywhere is to not.

So each serving engine keeps its own cache. vLLM hashes each block of tokens together with the hash of the block before it, and keeps the blocks in a pool that belongs to that engine process. SGLang keeps a radix tree of prefixes per worker. TensorRT-LLM reuses blocks of 128 tokens inside one executor, and only after the request that computed them has finished. Three different designs, one scope: the process. Whatever sits in front of them, a Kubernetes service, an ALB, nginx, a homegrown proxy, has no idea any of this is happening. It sees stateless HTTP requests and spreads them out, because spreading requests out is what load balancers are for.

Figure 01Where the cache lives
clientone conversation, turn after turnturns 1, 2, 3, 4 …gateway / load balancersees stateless HTTP and spreads it outturn 1replica 1its own GPUKV cache holdssystem prompttenant blockhistory: turn 1nothing elseturn 2replica 2its own GPUKV cache holdssystem prompttenant blockhistory: turns 1–2nothing elseturn 3replica 3its own GPUKV cache holdssystem prompttenant blockhistory: turns 1–3nothing elseturn 4replica 4its own GPUKV cache holdssystem prompttenant blockhistory: turns 1–4nothing else
Figure 01One conversation, four turns, dealt round robin by a balancer to four replicas. Each replica keeps its own key and value cache in its own GPU memory, and it holds only what it has served itself. Turn 5 goes back to the first replica, which last saw this conversation at turn 1 and has to compute everything since.

The arithmetic that follows is unforgiving. If a conversation’s turns are dealt round-robin to N replicas, the replica that gets turn k last saw this conversation at turn k minus N, if ever. It can serve the prefix up to that point and has to compute everything since. The shared system prompt survives, because every replica sees it within its first few requests. The conversation’s history, which in a long agent session is most of the prompt, is what goes first. A per-customer block sits in between: a session’s own turns warm it on every replica they touch, so it holds up while the fleet is smaller than a session is long, and then it goes too.

Figure 02Fan-out
Hit rate
57%
Tokens read
2,251,400
Tokens written
1,665,400
Busiest replica
1.0× its share
Input tokens billed on each replica, 384 requests from 48 sessions of 8 turns
4848484848484848
read from this replica’s cachecomputed and written
How deep each request’s hit went
nothing (2%)system prompt only (43%)system and tenant block (20%)into the session history (34%)

Each request carries a 2,500-token shared system prompt, a 5,000-token tenant block and 600 tokens per turn of history, all inside the cache lifetime. Every replica has its own cache. With one replica the hit rate is what you measured in development. Drag the slider right and watch which layer dies first.

Figure 02Forty-eight sessions from a configurable number of tenants, each session eight turns, dealt to a fleet by the gateway. Every replica has its own cache. Start at one replica, drag right, and watch the hit rate fall and the depth bar drain from “into the history” to “system prompt only”. Then switch the routing strategy and watch it come back, and watch what it costs in evenness.

Two things in that figure are worth sitting with. First, the collapse is not to zero. The shared system prompt keeps hitting on every strategy, because it is the same bytes for everyone and every replica writes it once. If your prompt is mostly a big shared system prompt and the per-user part is small, round robin is nearly fine, and that is the honest reason a lot of teams never notice this. Second, the strategies that fix it are not free. Sticky-by-session keeps each conversation on one machine and copies every tenant’s block onto every machine. Hashing on the tenant keeps each tenant in one place and lets one busy tenant pin one replica while the rest sit idle. The busiest-replica number at the top is the price of the hit rate underneath it.

Figure 03The whole curve
0%25%50%75%100%the shared system prompt alone, 25% of the input12481216replicas behind the gatewayround robin, 1 replica: 91% hit rateround robin, 2 replicas: 83% hit rateround robin, 3 replicas: 77% hit rateround robin, 4 replicas: 72% hit rateround robin, 5 replicas: 67% hit rateround robin, 6 replicas: 64% hit rateround robin, 7 replicas: 60% hit rateround robin, 8 replicas: 57% hit rateround robin, 9 replicas: 56% hit rateround robin, 10 replicas: 53% hit rateround robin, 11 replicas: 51% hit rateround robin, 12 replicas: 51% hit rateround robin, 13 replicas: 50% hit rateround robin, 14 replicas: 47% hit rateround robin, 15 replicas: 45% hit rateround robin, 16 replicas: 42% hit ratesticky by session, 1 replica: 91% hit ratesticky by session, 2 replicas: 90% hit ratesticky by session, 3 replicas: 89% hit ratesticky by session, 4 replicas: 88% hit ratesticky by session, 5 replicas: 88% hit ratesticky by session, 6 replicas: 88% hit ratesticky by session, 7 replicas: 88% hit ratesticky by session, 8 replicas: 88% hit ratesticky by session, 9 replicas: 88% hit ratesticky by session, 10 replicas: 87% hit ratesticky by session, 11 replicas: 87% hit ratesticky by session, 12 replicas: 87% hit ratesticky by session, 13 replicas: 87% hit ratesticky by session, 14 replicas: 87% hit ratesticky by session, 15 replicas: 87% hit ratesticky by session, 16 replicas: 87% hit ratehash of the stable prefix, 1 replica: 91% hit ratehash of the stable prefix, 2 replicas: 91% hit ratehash of the stable prefix, 3 replicas: 91% hit ratehash of the stable prefix, 4 replicas: 91% hit ratehash of the stable prefix, 5 replicas: 91% hit ratehash of the stable prefix, 6 replicas: 90% hit ratehash of the stable prefix, 7 replicas: 91% hit ratehash of the stable prefix, 8 replicas: 90% hit ratehash of the stable prefix, 9 replicas: 90% hit ratehash of the stable prefix, 10 replicas: 90% hit ratehash of the stable prefix, 11 replicas: 90% hit ratehash of the stable prefix, 12 replicas: 90% hit ratehash of the stable prefix, 13 replicas: 90% hit ratehash of the stable prefix, 14 replicas: 90% hit ratehash of the stable prefix, 15 replicas: 90% hit ratehash of the stable prefix, 16 replicas: 90% hit rateprefix hash 90%sticky session 87%round robin 42%
round robinsticky by sessionhash of the stable prefix

The same workload as the figure above, 48 sessions from 24 tenants with 8 turns each, run once for every fleet size. One replica is the number you measured in development, 91%. The routing key for the hash is the system prompt and the tenant, nothing deeper.

Figure 03The same forty-eight sessions as the figure above, run once for every fleet size from one replica to sixteen. Round robin falls towards the share of the input that is the shared system prompt, because that is the only part every replica ends up holding. Sticky sessions and the prefix hash stay within a few points of the single-replica number.

Key idea

A prefix cache is scoped to the process that built it. A load balancer that does not know that will spread a conversation across every replica, and the hit rate for everything below the shared prefix falls towards zero as the fleet grows.

Routing on the prefix

The fix is to make the thing in front of the replicas care about the thing behind them. There are two broad ways to do it, and they differ in how much the router has to know.

The first is to route on a hash. Pick some stable piece of the request, hash it, and use the hash to choose a replica, so that requests sharing that piece share a machine. The classic structure is a consistent-hash ring: each replica owns a handful of points on a circle, a key is placed on the circle by its hash, and it belongs to the next replica point clockwise. The property that makes this worth the trouble is what happens when the fleet changes. Add a replica and only the keys between its new points and their neighbours move; everything else stays where it was and keeps its cache. A plain modulo would move almost everything.

The question a hash-based router cannot answer for you is what to hash. Hash the session identifier and each conversation sticks to one replica, which is what the “sticky session” option in most proxies does. Hash the tenant and all of a tenant’s conversations share a machine, so the tenant’s manifest is written once instead of once per replica. Hash the system prompt alone and you have sent your whole fleet’s traffic to one box. The deeper the key, the more evenly load spreads and the less each replica’s cache is shared; the shallower, the better the sharing and the hotter the hot shard. Bounded-load variants of consistent hashing soften the worst of this by spilling a key to the next replica clockwise when its home is over some cap, at the cost of a second copy of that key’s prefix.

The second way is to route on what the replicas actually hold. SGLang’s router keeps a picture of each worker’s cache, built from events the engines publish as they fill and evict, and sends a request to the worker with the longest matching prefix, provided the match is at least a thousand tokens or so and that worker is not badly overloaded, in which case it falls back to picking the less loaded of two random workers. NVIDIA’s Dynamo does the same with a cost that trades prefix overlap against queue depth. The Kubernetes inference gateway projects do it as one scorer among several, next to queue length and KV memory pressure. These are better than hashing because they see reality, including evictions and restarts, and they are harder to run because the router is now stateful and has to be told the truth by every engine.

Figure 04The ring
5 replicas
sys|t0 → replica 5sys|t1 → replica 1sys|t2 → replica 2sys|t3 → replica 5sys|t4 → replica 3sys|t5 → replica 1sys|t6 → replica 3sys|t7 → replica 4sys|t8 → replica 5sys|t9 → replica 2sys|t10 → replica 1sys|t11 → replica 5sys|t12 → replica 2sys|t13 → replica 5sys|t14 → replica 5sys|t15 → replica 1sys|t16 → replica 4sys|t17 → replica 5sys|t18 → replica 3sys|t19 → replica 5sys|t20 → replica 2sys|t21 → replica 5sys|t22 → replica 2sys|t23 → replica 524 keys
Share of traffic on each replicaeven would be 20%
1
20%
2
19%
3
12%
4
7%
5
42%
Busiest replica
42%
Copies of a tenant block
1.0 avg, 1 max
replica point on the ringrouting key, sized by its trafficthe hot replica

Ticks on the ring are replica points; dots are routing keys, sized by traffic, each owned by the first replica point clockwise of it. A key hashed on the system prompt alone has one owner and sends the whole fleet’s traffic to it. Keyed on the tenant, each tenant’s block lives on exactly one replica, so a busy tenant is a busy replica. Keyed on the session, the load evens out and every tenant’s block is written on several replicas. The hash itself is fnv1a: 0xee596861 for the first tenant.

Figure 04A consistent-hash ring. Ticks are replica points, dots are routing keys sized by traffic and coloured by owner. Change the depth of the key from system prompt to tenant to session and watch the load bars and the “copies of a tenant block” count pull in opposite directions. Add a replica and see how few keys move. Turn on bounded load to spill the hot tenant’s key, at the price of a second copy.

I find the ring useful less as an algorithm than as a way of seeing the trade. Every routing key is a statement about which requests you want to share a cache, and the statement has a cost on the other axis. There is no key that is both perfectly even and perfectly shared, because the two are the same quantity measured from opposite ends.

A worked example

Here is the shape I keep meeting. A multi-tenant agent backend: one product, many customer organisations, each with their own tools and policies, each with many users having long conversations. The prompt has three layers that change at three different rates. A shared system prompt, a few thousand tokens, changes when we ship. A per-tenant block, often bigger than the system prompt because it carries the tool manifest with all its JSON schemas, changes when the tenant edits their configuration. And the conversation, which changes every turn. The backend runs as a gateway in front of N replicas, where a replica is a vLLM or SGLang instance, or a process that itself calls a hosted API.

Figure 05Three layers, one key
Shared system prompt
2,500 tokens, the same for everyone
changes when we ship
Tenant block
5,000 tokens: tool manifest, policies, glossary
changes when the tenant edits their configuration
hashed into the routing key
The same bytes for every turn of every conversation a tenant has, so they all get the same key and the same replica.
The conversation so far
600 tokens a turn, and growing
changes every turn
left out of the key
Put this in and there is one key per request, which is round robin with extra steps.
Figure 05The three layers of that prompt, drawn to their token counts, and how often each one changes. The routing key hashes the top two and leaves the conversation out, so every turn of every conversation for a tenant lands on the same replica, and the key only changes when something that is cached would have changed anyway.

The request is below in both dialects. On Anthropic’s API the layers are marked explicitly: a breakpoint on the last tool definition and one on the shared system block, both with the one-hour lifetime because they change rarely; a five-minute breakpoint on the tenant block; and the top-level automatic breakpoint to follow the growing conversation. Longer lifetimes have to come before shorter ones, which this order respects. On OpenAI’s API nothing is marked and matching is automatic above 1,024 tokens, but there is a prompt_cache_key that steers which of their machines sees the request, which is the same idea as this essay with the gateway on their side.

Figure 06Request and key
{
  "model": "claude-opus-5-5",
  "max_tokens": 4096,
  "tools": [
    { "name": "search_orders", "input_schema": { ... } },
    { "name": "issue_refund", "input_schema": { ... },
      "cache_control": { "type": "ephemeral", "ttl": "1h" } }
  ],
  "system": [
    { "type": "text",
      "text": "You are the support agent for ...",   // 2,500 tokens, same for everyone
      "cache_control": { "type": "ephemeral", "ttl": "1h" } },
    { "type": "text",
      "text": "Tenant: acme-ltd. Policies: ... Tool manifest: ...",   // 5,000 tokens, per tenant
      "cache_control": { "type": "ephemeral" } }
  ],
  "messages": [
    { "role": "user", "content": "Where is order 4471?" },
    { "role": "assistant", "content": [ { "type": "tool_use", ... } ] },
    { "role": "user", "content": [ { "type": "tool_result", ... } ] },
    ...
    { "role": "user", "content": "Refund it, please." }
  ],
  "cache_control": { "type": "ephemeral" }   // automatic breakpoint on the last block
}
Which parts does the gateway hash into the routing key?
routingKey = fnv1a("tools@sha256:9f1c…|system@sha256:4be0…|tenant:acme-ltd") = 0xcb2afdfc → replica 5 of 8
Distinct keys across the fleet
200
Of
200 tenants · 200,000 sessions · 2,000,000 requests

One key per tenant: each tenant’s manifest and all of its sessions live on one replica. This is the key the rest of the essay uses.

Figure 06The request as the gateway sees it, and the routing key derived from it. The highlighted parts are hashed into the key; the greyed parts are not. Add the session history to the key and there is one key per session. Add the user turn and there is one key per request, which is round robin with extra steps. Hover a part to find it in the request.

The routing key is a hash of the stable blocks, not of the whole prompt. That sentence does most of the work in this essay. The gateway hashes the model name, a digest of the serialised tool list, a digest of the shared system prompt and the tenant identifier, and nothing else. The conversation is deliberately left out, so every turn of every conversation for a tenant lands on the same replica, and that replica’s cache fills up with the tenant’s manifest once and each conversation’s history once. If a tenant is large enough to be its own hot shard, the key can gain a session component for that tenant only, which is the hash equivalent of sharding a big customer across machines.

gateway/route.ts
import { createHash } from 'node:crypto';

type Inbound = {
  model: string;
  tools: Tool[];          // serialised canonically: sorted keys, fixed order
  system: string;         // the shared prompt, frozen per release
  tenantId: string;
  messages: Message[];    // history and this turn: deliberately not hashed
};

const digest = (x: unknown) =>
  createHash('sha256').update(JSON.stringify(x)).digest('hex').slice(0, 16);

// Only the parts that every request we want sharing a cache has in common.
export function routingKey(r: Inbound) {
  return [r.model, digest(r.tools), digest(r.system), r.tenantId].join('|');
}

// Consistent hashing with a bounded load. The ring maps a key to a home
// replica; if that replica is over the cap we walk clockwise to the next.
export function pickReplica(key: string, ring: Ring, inflight: Map<string, number>) {
  const cap = Math.ceil(1.25 * mean(inflight.values()));
  for (const replica of ring.walkFrom(key)) {
    if ((inflight.get(replica) ?? 0) < cap) return replica;
  }
  return ring.lookup(key);
}

Two details in that code are the ones that go wrong in practice. The tool list must be serialised the same way every time, with sorted keys and a fixed order, or two gateway instances will derive two keys for one tenant and split its traffic. And the digest of the system prompt is in the key for a reason: when a release changes the prompt, every tenant’s key changes and every tenant moves to a new home. That is a deliberate cold start, and better than the alternative, which is new and old prompts interleaving on the same replica and competing for its cache during a rolling deploy.

Now the money. I have priced the tokens as if each replica billed like the Claude API, full price for a miss, the cache-read price for a hit, because those are the numbers everyone knows and the ratio between the rows is what matters. A self-hosted fleet bills in GPU-seconds and time to first token instead, and the same misses cost the same proportion of those.

Figure 07Three bills
RoutingHit rateReadWrittenInput billvs uncached
No caching at all0%0108,000,000$432—
Naive round robin63%67,842,66740,157,333$214−50%
Hash of the stable prefix92%99,419,0008,581,000$62.79−85%
Hash plus a warm-up fan-out93%99,944,0008,056,000 +483,333$62.69−85%

1,000 sessions of 10 turns, a 2,500-token system prompt, 5,000-token tenant manifests and 600 tokens per turn, all within the cache lifetime. Priced at Claude Opus 5.5 rates as of November 2026: $4.00/M uncached, $0.200/M for a cache read, writes at 1.25×. The warm-up row pre-writes the system prompt on every replica, and the 20 busiest tenants’ manifests on the replica each one hashes to, at start and after each restart; the grey figure is what that costs. On a self-hosted fleet the dollars become GPU-seconds, and the ratios stay.

Figure 07One thousand ten-turn sessions through the same fleet, routed three ways, with a configurable number of replica restarts during the run. The warm-up row pre-writes the shared prompt on every replica and the busiest tenants’ manifests on their home replicas, at start and after each restart. Prices from the Claude developer platform documentation as of November 2026.

The gap between the first two rows is the essay. The gap between the last two is close to nothing, and I have left the row in because that is the honest result. A warm-up fan-out, where the gateway sends one request per replica carrying the shared prefix before letting traffic through, moves the cold writes earlier. It does not remove them. Each token it writes is a token the first real request would have written anyway, so the bill is a wash to within a per cent either way, whatever you do to the restarts slider. What it buys is latency, and a cheap defence against the stampede in the next section: the first request after a deploy is not the slow one, and it is not forty slow ones at once.

When machines come and go

Everything so far assumed the fleet holds still. It does not. Replicas restart for deploys, get preempted, fail health checks, scale up on Tuesday morning and down on Tuesday night. Each of these is a cache event, and the ring makes the shape of each one legible.

A replica dies and its cache dies with it. Its keys move to the neighbours clockwise, which write those tenants’ prefixes from nothing. That is the first burst of misses. It comes back a few minutes later, cold, and its keys move back, and the neighbours’ copies are orphaned while the returning replica writes everything again. That is the second burst. A single restart is two cold starts, not one, and a rolling deploy of N replicas is 2N of them in a row. Cache-aware routers handle this better than hashing because they route to the copy that actually exists rather than to the one that is supposed to; a hash-based gateway can approximate the same by holding a key on its spilled replica for a while after the home comes back.

Figure 08Two cold starts
020k40kreplica 3 downminute 0: 24,300 tokens writtenminute 1: 24,300 tokens writtenminute 2: 18,600 tokens writtenminute 3: 26,100 tokens writtenminute 4: 27,900 tokens writtenminute 5: 20,400 tokens writtenminute 6: 29,700 tokens writtenminute 7: 22,200 tokens writtenminute 8: 31,500 tokens writtenminute 9: 24,000 tokens writtenminute 10: 25,800 tokens writtenminute 11: 25,800 tokens writtenminute 12: 10,800 tokens writtenminute 13: 10,800 tokens writtenminute 14: 10,800 tokens writtenminute 15: 10,800 tokens writtenminute 16: 10,800 tokens writtenminute 17: 10,800 tokens writtenminute 18: 10,800 tokens writtenminute 19: 10,800 tokens writtenminute 20: 38,400 tokens written, 27,600 of them because of the restartminute 21: 10,800 tokens writtenminute 22: 10,800 tokens writtenminute 23: 10,800 tokens writtenminute 24: 10,800 tokens writtenminute 25: 10,800 tokens writtenminute 26: 51,300 tokens written, 40,500 of them because of the restartminute 27: 10,800 tokens writtenminute 28: 10,800 tokens writtenminute 29: 10,800 tokens writtenminute 30: 10,800 tokens writtenminute 31: 10,800 tokens writtenminute 32: 10,800 tokens writtenminute 33: 10,800 tokens writtenminute 34: 10,800 tokens writtenminute 35: 10,800 tokens writtenminute 36: 10,800 tokens writtenminute 37: 10,800 tokens writtenminute 38: 10,800 tokens writtenminute 39: 10,800 tokens writtenminute 40: 10,800 tokens writtenminute 41: 10,800 tokens writtenminute 42: 10,800 tokens writtenminute 43: 10,800 tokens writtenminute 44: 10,800 tokens writtenminute 45: 10,800 tokens writtenminute 46: 10,800 tokens writtenminute 47: 10,800 tokens writtenminute 48: 10,800 tokens writtenminute 49: 10,800 tokens writtenminute 50: 10,800 tokens writtenminute 51: 10,800 tokens writtenminute 52: 10,800 tokens writtenminute 53: 10,800 tokens writtenminute 54: 10,800 tokens writtenminute 55: 10,800 tokens writtenminute 56: 10,800 tokens writtenminute 57: 10,800 tokens writtenminute 58: 10,800 tokens writtenminute 59: 10,800 tokens written0102030405060minutedies; neighbourswrite its tenantsback, cold;writes them all again
written with nothing restartingextra writes caused by the restartreplica 3 down

Six replicas routed by tenant hash, thirty-six sessions from twelve tenants, a turn every two minutes, one-hour entries so nothing lapses on its own. The first twelve minutes are the fleet filling for the first time. After that every minute writes only the new turns, until replica 3 goes at minute 20 and returns at minute 26: 27,600 extra tokens written on the way out, 40,500 on the way back.

Figure 08Written tokens per minute on a six-replica fleet routed by tenant hash, with one replica taken down at minute 20 and brought back at minute 26. The grey bars are the same hour with nothing restarting. The two dark bursts are the two cold starts: the neighbours writing the dead replica’s tenants, then the returning replica writing them all again.

Lifetimes interact with all of this. A hosted cache entry lives five minutes from the start of the request that touched it, or an hour if you paid for that. A self-hosted engine evicts least-recently-used blocks when it needs the memory, which under load can be well under five minutes. Either way, a tenant whose users reply slowly has entries lapsing between turns on a perfectly healthy fleet, and that looks in the logs exactly like a routing problem. The two bursts of misses in the figure below are the same colour whether a replica restarted or a user went to lunch.

The last failure is the stampede. An entry is not readable until the response that writes it has started streaming, or on some engines until that request has finished. If forty sessions sharing one prefix arrive in the same second at a cold replica, all forty compute the prefix and all forty write it. The hosted fix and the self-hosted fix are the same: let one request through, wait for its first token, then release the rest. A gateway is the natural place to put that gate, keyed on the same routing key it already has.

Figure 09Restarts, lapses and stampedes
Minutes across, replicas down. Click a cell to restart that replica at that minute.990 requests · routed by tenant hash
1
2
3
4
5
6
24300
24300
50400
50400
78300
78300
108000
108000
139500
139500
172800
172800
183600
183600
194400
194400
205200
205200
216000
216000
226800
226800
237600
237600
248400
248400
259200
259200
270000
270000
280800
280800
291600
291600
302400
302400
313200
313200
324000
324000
334800
334800
345600
345600
356400
356400
367200
367200
378000
378000
388800
388800
399600
399600
410400
410400
421200
421200
432000
432000
0 min3060
mostly readsonly the shared prompt readmostly writesreplica downidle
Hit rate
94%
With no restarts or changes
95%
Extra tokens written
+68,100

A restart empties one cache and, because the ring now has a hole, hands that replica’s tenants to its neighbours. Those neighbours write the tenants’ prefixes from scratch, and when the replica returns the tenants move back and write again. With slow users and five-minute entries, the gaps between turns do the same damage without anyone restarting anything. The last preset sends forty sessions with one shared prefix in the same minute: the entry is not readable until the first response starts, so all forty write it.

Figure 09Six replicas over an hour, routed by tenant hash, minutes across. Green cells are reads, dark cells are writes, red is a replica that is down. Click any cell to restart that replica at that minute and watch the misses move to its neighbours and back six minutes later. Switch to slow users with five-minute entries and the same pattern appears with nothing restarting. The last preset is forty sessions with one prefix arriving at once.

What I take from that figure is that the steady state is easy and the transitions are where the bill lives. If I were allowed one metric for a fleet like this, it would not be the hit rate. It would be written tokens per minute, plotted against deploys and scaling events, because that is the line that spikes when a transition goes badly and stays flat when it goes well.

Who does this for you

If you call a hosted provider directly, almost none of this is yours. Anthropic’s cache is addressed by a hash of the prompt up to the breakpoint, scoped to your organisation and, on their first-party API, to the workspace the key belongs to. Which of their machines holds the bytes is not something the API lets you see or affect, and the documentation does not mention machines at all. Your gateway can be as stateless and as round-robin as you like; the routing problem is on their side of the wall. The only distributed-cache mistakes left to make are splitting one prompt’s traffic across workspaces, which writes two entries and reads neither, and the parallel fan-out that writes the same prefix N times.

OpenAI is more candid about the machinery. Their documentation says cached states live on individual machines, that routing within an organisation and processing region depends on load, a hash of the first part of the prompt and any prompt_cache_key you supply, and that traffic above about fifteen requests a minute on one key starts to overflow to other machines. That is this essay’s gateway, running on their side, with a knob exposed to you. The right use of the knob is exactly the routing key above: stable, per tenant, never per request.

Self-hosting, or putting your own gateway in front of either provider, hands the problem back. You own the routing key, the ring or the cache-aware router, the warm-up on deploy, the stampede gate, and the metric that tells you when one of them has quietly stopped working. None of it is hard. All of it is state in a component that was pleasant precisely because it had none.

So the honest last question is whether to bother. I would not, in three situations. When the fleet is small enough, two or three replicas, that sticky sessions on the existing balancer get you most of the way. When the prompt is mostly a shared system prompt and the per-tenant and per-session parts are small, because round robin already keeps the expensive part cached. And when the traffic is so bursty and sparse that entries lapse between turns anyway, because no amount of routing helps a cache that has already expired. In each of those, the complexity buys you a hit rate you were not going to get.

I would bother when the per-tenant block is large, the conversations are long, and the fleet is big enough that round robin leaves most of each prompt on another machine. That is the agent backend above, and it is increasingly the common case. The usage block will tell you which case you are in. Reads should be most of the input on the second turn of a conversation. If they are not, and the prompt is laid out well, the cache is there. It is just on another machine.

Key idea

Route on a hash of the stable prefix, never on the whole prompt. Watch written tokens per minute across deploys. And before building any of it, check whether a hosted provider has already done it for you.

Written by

Dan Farr — a contract product engineer in UK, building product front-ends and whole applications in React, Next.js and TypeScript.

Available for contract work and fixed-scope projects. See what I do.