Use prefix cache-aware routing in estimation mode
With the Gateway with Inference Extension add-on, you can specify different load balancing policies for inference service routing based on the usage scenarios of generative AI inference services. Use the Gateway with Inference Extension add-on to configure prefix cache-aware routing in estimation mode.
Before you read this topic, make sure that you understand the concepts of InferencePool and InferenceModel.
This topic requires version 1.4.0 or later of the Gateway with Inference Extension add-on.
Background information
Automatic prefix caching in vLLM
vLLM supports the automatic prefix caching (APC) feature. APC caches the KV Cache of requests that vLLM has already computed. When a new request shares the same prefix with a previous request, vLLM can directly reuse the existing KV Cache. The new request then skips the KV Cache computation for the shared prefix, which accelerates the processing of LLM inference requests.
Prefix cache-aware routing in estimation mode
Prefix cache-aware routing in estimation mode is a load balancing policy that sends requests sharing the same prefix content to the same inference server Pod whenever possible. The Gateway with Inference Extension add-on tracks the requests routed to each inference server and estimates the prefixes that each inference server has cached, which improves the prefix cache hit ratio of the inference engine.
When APC is enabled on the model server, prefix cache-aware routing in estimation mode can maximize the prefix cache hit ratio and reduce request response time. This policy mainly applies to scenarios with a large volume of requests that share prefixes. Evaluate whether it fits your business scenario.
Typical use cases:
Long-document queries: A user repeatedly queries the same long document, such as a software manual or an annual report, with different questions.
Multi-turn conversations: A user may interact with an application multiple times within the same chat session.
Prerequisites
You have created an ACK managed cluster with a GPU node pool. You can also install the ACK Virtual Node add-on in the ACK managed cluster to use ACS GPU computing power.
You have installed version 1.4.0 of the Gateway with Inference ExtensionGateway with Inference Extension add-on and selected Enable Gateway API Inference Extension. For instructions, see Install Component.
For the image in this article, use A10 cards on Container Service for Kubernetes (ACK) clusters and the L20(GN8IS) card type on Alibaba Cloud Container Compute Service (ACS).
Because the LLM image is large, transfer it to Container Registry (ACR) and pull it using an internal network address. Pulling from the public network can be slow, as the speed is limited by the bandwidth of the cluster's elastic IP address (EIP).
Procedure
Step 1: Deploy the sample inference service
Create a file named vllm-service.yaml.
Deploy the sample inference service.
kubectl apply -f vllm-service.yaml
Step 2: Deploy the inference route
This step creates an InferencePool resource and an InferenceModel resource.
Create a file named inference-pool.yaml.
apiVersion: inference.networking.x-k8s.io/v1alpha2 kind: InferencePool metadata: annotations: inference.networking.x-k8s.io/routing-strategy: "PREFIX_CACHE" name: vllm-qwen-pool spec: targetPortNumber: 8000 selector: app: qwen extensionRef: name: inference-gateway-ext-proc --- apiVersion: inference.networking.x-k8s.io/v1alpha2 kind: InferenceModel metadata: name: inferencemodel-qwen spec: modelName: /model/qwen criticality: Critical poolRef: group: inference.networking.x-k8s.io kind: InferencePool name: vllm-qwen-pool targetModels: - name: /model/qwen weight: 100In the InferencePool resource, set the
inference.networking.x-k8s.io/routing-strategy: "PREFIX_CACHE"annotation to enable prefix cache-aware routing in estimation mode for the Pods in the InferencePool.Deploy the inference route.
kubectl apply -f inference-pool.yaml
Step 3: Deploy the gateway and gateway route rules
This step creates a gateway with ports 8080 and 8081. On port 8081, an HTTPRoute resource sets the gateway routing backend to the InferencePool provided by the inference extension. Inference requests are routed to the Pod set specified by the InferencePool. On port 8080, an HTTPRoute resource sets the gateway routing backend to a Service. Inference requests are routed to the same Pod set through the standard HTTP least-request load balancing policy.
Create a file named inference-gateway.yaml.
apiVersion: gateway.networking.k8s.io/v1 kind: GatewayClass metadata: name: qwen-inference-gateway-class spec: controllerName: gateway.envoyproxy.io/gatewayclass-controller --- apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: qwen-inference-gateway spec: gatewayClassName: qwen-inference-gateway-class listeners: - name: http protocol: HTTP port: 8080 - name: llm-gw protocol: HTTP port: 8081 --- apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: qwen-backend spec: parentRefs: - name: qwen-inference-gateway sectionName: llm-gw rules: - backendRefs: - group: inference.networking.x-k8s.io kind: InferencePool name: vllm-qwen-pool matches: - path: type: PathPrefix value: / --- apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: qwen-backend-no-inference spec: parentRefs: - group: gateway.networking.k8s.io kind: Gateway name: qwen-inference-gateway sectionName: http rules: - backendRefs: - group: "" kind: Service name: qwen port: 8000 weight: 1 matches: - path: type: PathPrefix value: / --- apiVersion: gateway.envoyproxy.io/v1alpha1 kind: BackendTrafficPolicy metadata: name: backend-timeout spec: timeout: http: requestTimeout: 1h targetRef: group: gateway.networking.k8s.io kind: Gateway name: qwen-inference-gatewayDeploy the gateway.
kubectl apply -f inference-gateway.yaml
Step 4: Verify the route rules
Create round1.txt and round2.txt. Both files contain the same
contentsection. Send round1.txt and then round2.txt as the body of an LLM request. Then check the logs of the intelligent routing extensionRef to verify whether prefix cache-aware routing in estimation mode is triggered.round1.txt:
echo '{"max_tokens":24,"messages":[{"content":"Hi, here'\''s some system prompt: hi hi hi hi hi hi hi hi hi hi.For user 3, here are some other context: hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi.I would like to test your intelligence. for this purpose I would like you to play zork. you can interact with the game by typing in commands. I will forward these commands to the game and type in any response. are you ready?","role":"user"}],"model":"/model/qwen","stream":true,"stream_options":{"include_usage":true},"temperature":0}' > round1.txtround2.txt:
echo '{"max_tokens":3,"messages":[{"content":"Hi, here'\''s some system prompt: hi hi hi hi hi hi hi hi hi hi.For user 3, here are some other context: hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi hi.I would like to test your intelligence. for this purpose I would like you to play zork. you can interact with the game by typing in commands. I will forward these commands to the game and type in any response. are you ready?","role":"user"},{"content":"Hi there! It looks like you're setting up a fun test. I'm ready to play Zork! You can","role":"assistant"},{"content":"% zork\nWelcome to Dungeon. This version created 11-MAR-91.\nYou are in an open field west of a big white house with a boarded\nfront door.\nThere is a small mailbox here.\n>","role":"user"},{"content":"Great!","role":"assistant"},{"content":"Opening the mailbox reveals:\n A leaflet.\n>","role":"user"}],"model":"/model/qwen","stream":true,"stream_options":{"include_usage":true},"temperature":0}' > round2.txtGet the public IP address of the Gateway.
export GATEWAY_IP=$(kubectl get gateway/qwen-inference-gateway -o jsonpath='{.status.addresses[0].value}')Send two session requests to simulate a multi-turn conversation.
curl -X POST ${GATEWAY_IP}:8081/v1/chat/completions -H 'Content-Type: application/json' -d @./round1.txt curl -X POST ${GATEWAY_IP}:8081/v1/chat/completions -H 'Content-Type: application/json' -d @./round2.txtCheck the logs to confirm whether prefix cache-aware routing in estimation mode is in effect.
kubectl logs deploy/epp-default-inference-gateway-ext-proc -n envoy-gateway-system|grep "Do prefix"Expected output:
2025-05-23T03:33:09Z INFO scheduling/prefixcache_filter.go:311 Do prefix-aware routing! {"request": "v68m4zx472", "matching ratio": " 0.54 > 0.50"}The log entry
Do prefix-aware routing!indicates that prefix cache-aware routing in estimation mode is in effect.
(Optional) Step 5: Evaluate inference service performance with a multi-turn conversation test
This step uses an ACK cluster as an example to run a multi-turn conversation test with a benchmarking tool. The test compares the prefix load balancing effects of the standard HTTP route and the inference route.
Deploy the llm-qa-benchmark benchmarking tool.
kubectl apply -f- <<EOF apiVersion: apps/v1 kind: Deployment metadata: labels: app: llm-qa-benchmark name: llm-qa-benchmark spec: replicas: 1 selector: matchLabels: app: llm-qa-benchmark template: metadata: labels: app: llm-qa-benchmark spec: containers: - command: - sh - -c - sleep inf image: registry-cn-hangzhou.ack.aliyuncs.com/dev/llm-qa-benchmark:v0.1 imagePullPolicy: IfNotPresent name: llm-qa-benchmark terminationMessagePath: /dev/termination-log terminationMessagePolicy: File restartPolicy: Always EOFGet the internal IP address of the Gateway.
export GW_IP=$(kubectl get svc -n envoy-gateway-system -l gateway.envoyproxy.io/owning-gateway-namespace=default,gateway.envoyproxy.io/owning-gateway-name=qwen-inference-gateway -o jsonpath='{.items[0].spec.clusterIP}')Run the benchmark test.
ImportantThe following test results are generated in a test environment. Your actual results depend on your environment.
Standard HTTP route
kubectl exec -it deploy/llm-qa-benchmark -- env GW_IP=${GW_IP} python3 multi-round-qa.py \ --num-users 8 \ --num-rounds 15 \ --qps 0.1 \ --shared-system-prompt 100 \ --sharegpt \ --user-history-prompt 2000 \ --answer-len 100 \ --model /model/qwen \ --time 600 \ --base-url http://${GW_IP}:8080/v1Expected output:
==================== Performance summary ====================== QPS: 0.1000 reqs/s Processing speed: 0.1080 reqs/s Requests on-the-fly: 0 Input tokens per second: 259.0703 tokens/s Output tokens per second: 4.8576 tokens/s Average generation throughput (per request): 26.6710 tokens/req/s Average TTFT: 0.3669s Time range: 1748231183.2753935 - 1748231766.4799275 (583.20s) ===============================================================Inference service route
kubectl exec -it deploy/llm-qa-benchmark -- env GW_IP=${GW_IP} python3 multi-round-qa.py \ --num-users 8 \ --num-rounds 15 \ --qps 0.1 \ --shared-system-prompt 100 \ --sharegpt \ --user-history-prompt 2000 \ --answer-len 100 \ --model /model/qwen \ --time 600 \ --base-url http://${GW_IP}:8081/v1Expected output:
==================== Performance summary ====================== QPS: 0.1000 reqs/s Processing speed: 0.1081 reqs/s Requests on-the-fly: 0 Input tokens per second: 259.3009 tokens/s Output tokens per second: 4.8548 tokens/s Average generation throughput (per request): 26.9300 tokens/req/s Average TTFT: 0.2761s Time range: 1748231885.874972 - 1748232468.5918882 (582.72s) ===============================================================The
Average TTFTof the inference service route shows a significant improvement over the HTTP route.