Contact
Skip to the page text

Documentation / Concepts

How the Gateway Chooses a Replica

A model deployed with several replicas answers through all of them. For every request the API gateway decides which replica answers. For chat requests it decides by what the replica already has in memory, not at random: a request whose prompt starts the way a recent request started goes to the replica that served that one.

Why It Matters

Before a model writes its first token it reads the whole prompt (the prefill). Reading a 20,000-token prompt takes the GPU real time. A model that keeps prefix caching on remembers the prompts it has read recently: when the next request starts with the same text, it skips that part and reads only what is new.

That memory belongs to one replica. A conversation whose third turn lands on a different replica than its second turn is read from the beginning again. Prompts that repeat their beginning are common:

  • the same system prompt in front of every request of an application,
  • the earlier turns of a chat, sent again with every new message,
  • an agent's instructions and tool definitions, sent again on every step.

Measured on Qwen3.8 27B with four replicas on two inference nodes, eight shared prompts of 12,000 tokens and other traffic at the same time: 97% of the requests of a shared prompt went to the same replica, and 44% of prompt tokens came from the cache. With a random choice, 43% of the requests went to one replica.

How a Replica Is Chosen

  1. The candidates. Every replica of the requested model that is ready to serve. A request addressed to one inference service by its id chooses among that service's replicas only.
  2. The replicas that hold the prompt. The API gateway's routing component keeps only the replicas that recently served most of this prompt's beginning. If none did, it keeps all of them. If the replicas that hold the prompt are so busy that the request would wait clearly longer there than on another replica, it keeps all of them too, so one popular prompt does not overload one replica.
  3. The choice. Among the replicas it kept, the one with the least prompt work in progress answers. The routing component reads each model engine's load once per second. The decision takes well under a millisecond.

The routing component is the llm-d router, an open-source project, running inside the API gateway. It has no port of its own and cannot be reached from outside.

Which Requests Are Routed This Way

RequestHow the replica is chosen
POST /v1/chat/completions, POST /v1/completionsBy prompt and load
POST /v1/messages (Anthropic schema)By prompt and load, read in its Chat Completions form
The same paths through a path prefix or an inference service idBy prompt and load
Every other request (embeddings, OCR, audio, model lists)Any replica, at random

A model with one replica always answers from that replica.

What You Do as a Client

Nothing is required. To get the most from it:

  • Send the parts that do not change first, in the same words every time: the system prompt, then tool definitions, then the conversation so far, then the new message.
  • Do not put a timestamp, a request id or a random value at the start of the prompt. A prompt that differs in its first line shares nothing with the previous one.
  • Send the whole conversation history with each turn, unchanged, as chat clients normally do.

When It Does Not Help

  • The model runs without prefix caching. Then there is nothing to reuse, and only the choice by load helps. Prefix caching is a setting of the model's deploy profile.
  • Hybrid models reuse in large blocks. Models that mix attention with linear layers (Qwen3.8, Qwen3.6, Nemotron 3) cache prompts in blocks of a thousand tokens or more: shorter shared beginnings are not reused.
  • The prompt was evicted. The routing component remembers where it sent a prompt; it does not see a replica drop it from a full cache. The request then goes where the prompt was, and is read in full there, as it would have been anywhere.
  • Several API gateway instances. Each instance remembers only its own requests. A conversation whose turns arrive through different instances is reused less.

When the Routing Component Cannot Decide

If the routing component does not answer within 250 milliseconds, answers with an error, or is not running, the API gateway sends the request to a random replica of the model and serves it. Models keep answering; repeated prompts are slower while this lasts. The API gateway restarts the routing component on its own when it stops.

Each run of such failures writes one error report (domain replica-selection, from the API gateway). It names the reason and carries the routing component's own last log lines. After 5 minutes the warning alert ApiGatewayReplicaSelectionFallback fires. Open Error reports, filter on the API gateway and the domain replica-selection, and send the newest error report to support.

How to See Which Way a Request Went

Every request the API gateway counts in onprem_ai_api_gateway_requests_total carries a replica_selection label:

ValueMeaning
llm_d_routerThe routing component chose the replica.
randomA request type the routing component does not score. Normal.
random_fallbackThe routing component should have chosen and could not. An error report says why.
directThe request named the replica itself, on a debug path (/_debug/replicas/...); nothing was chosen.

A count of random_fallback above zero is the first thing to look at when repeated prompts of a model with several replicas get slower.

Found a mistake, or is something missing? Suggest an improvement to this page on GitHub.

Platform release 5.10.4, . Source of this page at v5.10.4. This page as Markdown.