Prefix-Cache-Aware Request Router for LLM Replicas

Last updated: November 29, 2026

Quick Overview

Build a router that sends each prompt to the LLM replica most likely to already hold its prefix in KV cache, using chained hashes of fixed-size token blocks, while never exceeding a per-replica load cap. Replicas keep a bounded LRU of block hashes, and requests release load when they finish.

Baseten
Coding & Algorithms
Software Engineer
Baseten
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Build a router that sends each prompt to the LLM replica most likely to already hold its prefix in KV cache, using chained hashes of fixed-size token blocks, while never exceeding a per-replica load cap. Replicas keep a bounded LRU of block hashes, and requests release load when they finish.

Many Baseten customers send long shared system prompts or multi-turn chats, and routing a request to a replica that already cached that prefix skips most of the prefill cost. The interviewer is checking whether you can combine prefix matching, bounded state and load balancing without one hot replica absorbing all traffic.

What the Interviewer Expects
  • Hashes blocks with a chain so a block hash identifies the whole prefix up to it
  • Matches only a contiguous prefix of blocks, stopping at the first miss
  • Balances cache affinity against load with a hard cap and a clear tie-break
  • Bounds per-replica state with LRU eviction of block hashes
  • Falls back to least-loaded routing when every replica is saturated
  • Discusses the router's view drifting from the engine's actual cache
Key Topics to Cover
prefix caching
KV cache
load balancing
hashing
LRU
LLM routing
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • The router's view of each cache is approximate. How would replicas report evictions back?
  • How would you replace the hard cap with a score that trades matched blocks against queue depth?
  • How do you keep routing consistent across several router instances?
  • What happens to affinity when the autoscaler adds or removes replicas?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

route(tokens) returns a replica index; finish(r) decrements its load. Prompts are split into blocks of block tokens; only full blocks are cacheable. Pick the replica under max_load with the mo...

High-Level Design

Chained hashing (h_i = H(h_{i-1} || block_i)) means the i-th hash only matches if every earlier block matched too, mirroring how engines like vLLM and TensorRT-LLM key reusable KV blocks. After rout...


Submit Your Answer
Markdown supported

Related Questions