Skip to content

Commit c2dbe97

Browse files
committed
Update, add a diagram
1 parent 2595221 commit c2dbe97

File tree

1 file changed

+42
-36
lines changed
  • docs/proposals/005-prefix-cache-aware-routing-proposal

1 file changed

+42
-36
lines changed
Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
# Prefix Cache Aware Routing
22

3-
## Background
3+
## Overview
44

5-
Prefix caching is a well-known technique in LLM inference to save duplicate tensor computation for prompts with the same prefixes, and is available in many model servers, as well as inference frameworks.
5+
Prefix caching is a well-known technique in LLM inference to save duplicate tensor computation for prompts with the same prefix tokens, and is available in many model servers or model as a service providers. Leveraging prefix caching can significantly boost system performance, especially the time to first token (TTFT). Given that EPP has a global view of requests and model servers in the `InferencePool`, it can schedule requests intelligently to maximize the global prefix cache hit rate.
6+
7+
### Goals
8+
9+
Implement a prefix aware scheduling algorithm on EPP to maximize the cache hit rate on the model servers.
10+
11+
### Non-goals
12+
13+
* Change how model server manages prefix caches, or add any prefix cache APIs.
14+
* Coordinate cache beyond accelerator HBM cache, such as remote caches.
15+
16+
## Existing Solutions
617

718
[vLLM](https://docs.vllm.ai/en/latest/features/automatic_prefix_caching.html) has the automatic prefix cache (APC) feature by caching in the accelerator HBM, and uses an LRU cache eviction strategy.
819

@@ -14,17 +25,9 @@ Prefix caching is a well-known technique in LLM inference to save duplicate tens
1425

1526
[KubeAI](https://www.kubeai.org/blog/2025/02/26/llm-load-balancing-at-scale-chwbl/) uses a Consistent Hashing with Bounded Loads (CHWBL) algorithm which hashes request prefixes up to a configurable length (and therefore will lose some accuracy), and use an "overflow" strategy when the server is hot loaded.
1627

17-
## Goals
18-
19-
Implement a prefix aware routing algorithm on EPP to maximize the cache hit rate on the model servers.
20-
21-
### Non-goals
22-
23-
* Change how model server manages prefix caches, e.g., add DRAM cache support or remote cache support.
24-
2528
## Design Options
2629

27-
1. **Session affinity**
30+
### Session affinity
2831

2932
Session affinity is based on client attributes such as IP address. It works well for use cases such as multi-turn conversations, where requests from the same client tend to share the same prefixes. This, of course, highly depends on the nature of the use case.
3033

@@ -38,7 +41,7 @@ Cons:
3841
* Does not exploit prefix cache between different clients
3942
* Using client IP isn't always reliable, will likely need client to provide "session info" for good affinity
4043

41-
1. **Prefix affinity consistent hashing**
44+
### Prefix affinity consistent hashing
4245

4346
This goes a step beyond the session affinity by using a prefix aware hash function to route requests with similar prefixes to the same or similar servers. A naive hash function can be just taking the hash of the first N characters/tokens of the request, and therefore all requests with the same first N characters/tokens will be routed to the same server. The [vLLM production stack](https://github.com/vllm-project/production-stack/issues/59) is exploring this strategy using simhash, and preliminary experiments showed mixed results. KubeAI uses a simple strategy to only hash request prefix up to a configurable `prefixCharLength`. Its effectiveness is likely highly dependent on the input length distribution.
4447

@@ -52,46 +55,41 @@ Cons:
5255
* Highly depends on the effectiveness of the prefix aware hash function.
5356
* Consistent hashing can be challenging to reason about.
5457

55-
1. **Approximate prefix cache on the router**
56-
This builds on the intuition that if `requestA=prefix+XX` was routed to server 1, then routing `requstB=prefix+YY` to the same server will likely hit its prefix cache. Therefore the central router can build an approximate lookup cache of the prefix caches on all the backend servers, by mimicking a similar cache eviction strategy of the model server (e.g., LRU).
57-
58-
Pros:
59-
60-
* Easy to explain (compared to hashing) and likely more effective than hashing strategy.
61-
62-
Cons:
58+
### Report prefix cache indexes on the router
6359

64-
* Relies on knowledge of the cache eviction strategy of the model server, and may need careful tuning for different environments (e.g., model server with different total kv cache space may have different characteristics of cache eviction).
65-
* Complexity in managing cache state (eviction, memory limit)
66-
* An in memory cache is preferred for high performance. However, that means cache need to be rebuilt for restarts. Moreover, cache hit performance decreases with multiple active EPP replicas
67-
68-
1. **Accurate prefix cache on the router**
6960
If the router knows what prefixes are currently cached on each model server replica, it can make the optimal decision. A potential solution is to have the model server (or with a sidecar) report the kv cache indexes to the router.
7061

7162
Pros:
7263

73-
* Best cache hit rate
64+
* Best cache hit rate in theory
7465

7566
Cons:
7667

77-
* Requires adding a sidecar and its integration with the model server
78-
* May face scalability concerns with large number of model server replicas and large number of prefix caches
68+
* Requires API changes on the model servers to report the cache indexes.
69+
* Reporting the cache indexes in real time requires non-trivial network bandwidth.
7970

71+
### Approximate prefix index on the router
8072

81-
## How does prefix cache affinity routing work with LoRA affinity and load-aware routing
73+
This builds on the intuition that if `requestA=prefix+XX` was routed to server 1, then routing `requestB=prefix+YY` to the same server will likely hit its prefix cache. Therefore the central router can build an approximate index table of the prefix caches on all the backend servers, by mimicking a similar cache eviction strategy of the model server (e.g., LRU).
8274

83-
1. Prefix cache needs to be LoRA aware, as different adapters don’t share the same kv cache. Therefore, prefix cache affinity algo should be applied after LoRA affinity, to avoid conflicts. And by doing so, the requests will naturally be colocated by the same LoRA, and further by prefix matching.
84-
1. Only use prefix affinity if the expected gain is high enough. This can be done with a threshold, either on the prefix matching length, or a matching ratio of the entire request, or a combination of both. If the expected gain is below the threshold (e.g, only 10 tokens match), then we ignore prefix affinity and fall back to current load based algo.
85-
1. Prefix affinity needs to be aware of the server load, otherwise we will create hot spots. We can use queue length and k-v cache utilization to understand the server load. This is similar to the [queue depth threshold](https://github.com/kubernetes-sigs/gateway-api-inference-extension/blob/2a615e981228aa6ffc2a89219c986ac863dde776/pkg/epp/scheduling/scheduler.go#L40) for LoRA affinity.
75+
Pros:
76+
77+
* (Compared to the session affinity strategy) Broader application to most use cases and doesn't require any client integration.
78+
* (Compared to the consistent hashing strategy) Easy to implement and explain and more effective.
8679

80+
Cons:
81+
82+
* Relies on knowledge of the cache eviction strategy of the model server, and may need careful tuning for different environments (e.g., model server with different total kv cache space may have different characteristics of cache eviction).
83+
* Complexity in managing cache state (eviction, memory limit)
84+
* An in memory cache is preferred for high performance. However, that means cache need to be rebuilt for restarts. Moreover, cache hit performance decreases with multiple active EPP replicas.
8785

8886
## Proposal
8987

90-
Implement an approximate prefix cache lookup on the EPP.
88+
Based on the above discussion, I propose the "Approximate prefix cache on the router" solution, which has the advantage of fast time to market, automatic prefix cache (without needing client integration), decent performance with the cost of degraded performance when sharded.
9189

92-
A request is broken down into N chunks of the same number of characters (we don’t necessarily need to tokenize). For each chunk we will calculate a hash based on the **content of the chunk + hash of the prefix**: `hash(chunk i) = hash(chunk i content + hash(chunk i-1))`. This is very similar to how vLLM does it.
90+
A request is broken down into N chunks of the same number of characters (we don’t necessarily need to tokenize). For each chunk we will calculate a hash based on the **content of the chunk + hash of the prefix**: `hash(chunk i) = hash(chunk i content + hash(chunk i-1))`. This gives us a nice property that if we find a match of a chunk hash, then we know all its prefix chunk hashes match as well. This is very similar to how vLLM does it.
9391

94-
When we route a request `r1` with `N` chunks to a server `s1`, we update the approximate cache lookup table like so:
92+
When we schedule a request `r1` with `N` chunks to a server `s1`, we update the approximate cache index table like so:
9593

9694
```
9795
hash(chunk 1): append s1
@@ -104,4 +102,12 @@ This means all these N chunks are cached on server `s1`.
104102

105103
When the EPP receives a new request `r2`, we calculate its chunk hashes, and look up the table to find a server with longest prefix matching.
106104

107-
Each entry in the table needs a `lastUpdate` time to allow LRU cache eviction.
105+
<img src="https://docs.google.com/drawings/d/e/2PACX-1vQ9gGbq_vrv46BZpviOUpKCuo_WCo6ANzLoAIP9lo6zrMB9kmVNk4YLKBAoGh3IsZ7mRxDu9pDqukrX/pub?w=1074&amp;h=956">
106+
107+
[Image source](https://docs.google.com/drawings/d/1KL5DKh42Z_XzvcnejUcRymu99_HwW9y8U29IrPzRCss/edit?usp=sharing)
108+
109+
110+
## How does prefix cache affinity routing work with LoRA affinity and load-aware routing
111+
112+
1. Prefix cache needs to be LoRA aware, as different adapters don’t share the same kv cache. Therefore when finding prefix matches, we only match for the same model/adapter.
113+
2. Prefix affinity needs to be aware of the server load and avoid overloading servers. We can calculate a combined weighted score of servers depending on: prefix cache hit ratio, queue length and k-v cache utilization to achieve a good balance between prefix cache affinity and load balancing.

0 commit comments

Comments
 (0)