Comparison
SGLang vs vLLM: Prefix Caching and When to Pick Each
Pick SGLang when your requests share long prefixes, such as agents with fixed tool schemas, multi-turn chat, or RAG with a common preamble: its radix-tree cache is designed around reusing them. Pick vLLM as a general-purpose server for mixed or unique prompts. Both expose OpenAI-compatible APIs, and both cache prefixes; they index the cache differently.
Last updated · By Rohit Ghumare
SGLang vs vLLM at a glance
| SGLang | vLLM | |
|---|---|---|
| What its docs call it | An inference framework meant for production-level serving | A fast and easy-to-use library for LLM inference and serving |
| Start a server | sglang serve MODEL_PATH --host 0.0.0.0 --port 30000 | vllm serve <Hugging Face model ID> (port 8000 by default) |
| API | OpenAI-compatible APIs, plus an Anthropic-compatible /v1/messages | OpenAI-compatible server, plus Anthropic Messages API and gRPC |
| Headline techniques | RadixAttention, prefix caching, multi-GPU parallelism | PagedAttention, continuous batching, chunked prefill, prefix caching |
| How the prefix cache is indexed | A radix cache: a tree of cached prefixes | A hash per cache block, chained to the hash of the block before it |
| Cache controls in the docs | Eviction policy (--radix-eviction-policy: lru by default; also lfu, slru, priority, tlru, state_cost, joint_lru); a session-aware mode (--enable-session-radix-cache) | Cache salts to isolate tenants; a choice of hash algorithm (--prefix-caching-hash-algo); only full blocks are cached |
| Request scheduling | Cache-aware in the SGLang paper: requests with longer matched prefixes go first | --scheduling-policy is fcfs (first come, first served) by default; priority is the other choice |
| Hardware named in the docs | NVIDIA, AMD, Intel Xeon, Google TPU, Ascend NPU, Moore Threads | NVIDIA and AMD GPUs, x86/ARM/PowerPC CPUs, and hardware plugins |
| Install notes | Quickstart targets Linux with an NVIDIA GPU; pip installs also need the CUDA 13 Toolkit and a C/C++ compiler, or use the Docker image | uv pip install vllm; the quickstart lists Linux as the prerequisite OS |
Every row comes from the SGLang and vLLM documentation listed in this page's sources, read on 2026-12-04, except SGLang's scheduling, which comes from the SGLang paper (arXiv:2312.07104). Both projects also document structured outputs and LoRA serving. This page gives no throughput figures: the gain from prefix caching depends on your own hit rate.
When to choose each
Choose SGLang when
- Most requests start with the same long text: a system prompt, tool schemas, few-shot examples, a retrieval preamble.
- You run agents or multi-turn chat, where each turn repeats the whole conversation so far.
- You want control over what the cache keeps under memory pressure. SGLang documents several eviction policies and a session-aware mode.
- You can enforce a fixed prompt order across every client. Without that, the shared prefix is not there to reuse.
Choose vLLM when
- Prompts are mostly unique: single-shot generation, code completion, open-ended chat with a short system prompt.
- You want one general-purpose server for mixed traffic. The curriculum treats vLLM as the broad production default.
- You serve several tenants and want cache isolation between them. vLLM documents cache salts for that.
- Your team already runs vLLM. Its prefix caching is on by default for models that support it (checked in the v0.34.0 source, November 2026). Confirm it is active by watching
vllm:prefix_cache_hitsagainstvllm:prefix_cache_queries, and measure before switching engines.
Measure before you decide
The two are not strict competitors, and both change quickly. The honest test is your own traffic: replay a sample of real prompts through each engine at your expected concurrency, and compare time to first token, time between tokens and the cache hit rate.
Code: one example per side
Server commands come from each project's quickstart. Model names, ports and defaults are as the quickstarts showed them on 2026-12-04.
sglang serve Qwen/Qwen3-0.6B --host 0.0.0.0 --port 30000
curl http://localhost:30000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model": "Qwen/Qwen3-0.6B", "messages": [{"role": "user", "content": "What is the capital of France?"}]}'vllm serve Qwen/Qwen2.5-1.5B-Instruct
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model": "Qwen/Qwen2.5-1.5B-Instruct", "messages": [{"role": "user", "content": "What is the capital of France?"}]}'The two cache designs, as toys in standard-library Python. Each caches one request (a 40-token system prompt plus a question) and then counts how many tokens of a second request it can reuse. These illustrate the two index structures. They are not measurements of SGLang or vLLM, and each printed line says so. The second toy's result depends on its block size, which is set to 16 tokens here.
Radix tree: match token by token down the tree.
class Node:
def __init__(self):
self.children = {}
root = Node()
def insert(tokens):
node = root
for token in tokens:
node = node.children.setdefault(token, Node())
def match(tokens):
node, depth = root, 0
for token in tokens:
if token not in node.children:
break
node, depth = node.children[token], depth + 1
return depth
system = [f"sys{i}" for i in range(40)]
insert(system + ["question", "one"])
request = system + ["question", "two", "please"]
print(f"toy radix index: matched {match(request)} of {len(request)} prompt tokens (not an engine measurement)")Toy output, not SGLang:
toy radix index: matched 41 of 43 prompt tokens (not an engine measurement)Chained block hashes: only full 16-token blocks whose hash, including everything before them, is already cached.
BLOCK = 16
def block_hashes(tokens):
hashes, parent = [], None
for start in range(0, len(tokens) - len(tokens) % BLOCK, BLOCK):
parent = hash((parent, tuple(tokens[start:start + BLOCK])))
hashes.append(parent)
return hashes
system = [f"sys{i}" for i in range(40)]
cached = set(block_hashes(system + ["question", "one"]))
reused = 0
for block_hash in block_hashes(system + ["question", "two", "please"]):
if block_hash not in cached:
break
reused += BLOCK
print(f"toy block-hash index, {BLOCK}-token blocks: matched {reused} of 43 prompt tokens (not an engine measurement)")Toy output, not vLLM:
toy block-hash index, 16-token blocks: matched 32 of 43 prompt tokens (not an engine measurement)The 41 against 32 gap comes from the toy's 43-token prompt and 16-token block, not from either engine. vLLM's default block is 64 tokens in the v0.34.0 source; set BLOCK = 64 and this toy prints 0 of 43. On a real prompt of thousands of tokens, a partial block at the end is a small share of the total. What decides production results is whether the cached prefix is still in memory when the next matching request arrives, which is a scheduling and eviction question.
Common mistakes
- Inconsistent prompt order. If some requests send system, tools, context and others send system, context, tools, the cache sees two different sequences. Fix the order.
- Dynamic content near the top. A timestamp or user ID in the first lines makes every prompt unique from that point on.
- Benchmarking with unique prompts. That measures neither cache. Use prompts with the prefix sharing your real traffic has.
- Assuming vLLM has no prefix cache. It does, and it is on by default in current releases for models that support it. Its docs call it Automatic Prefix Caching.
- Quoting a published speed-up as your own. Gains from prefix caching depend on your hit rate. Measure it.
- Turning on every optimization at once. Features interact, and what composes changes between releases. Check the docs for your exact version.
- Comparing different weights. Use the same model and the same quantization on both sides.
- Reading only average throughput. Check time to first token and the tail of time between tokens under concurrent load.
Frequently asked questions
Is SGLang faster than vLLM?
It depends on how much of each prompt repeats, so no single number answers it. SGLang is built around reusing the cache for shared prompt prefixes, so the more your requests share, the more it can skip. With unique prompts there is little to reuse, and the curriculum expects the two to perform similarly.
Does vLLM support prefix caching?
Yes. vLLM calls it Automatic Prefix Caching, and in vLLM v0.34.0 (November 2026) it is on by default for models that support it. vLLM hashes each cache block together with all the tokens before it. A new request reuses the longest run of full blocks at the start of its prompt that is already cached.
What is RadixAttention?
RadixAttention is SGLang's way of indexing the attention cache. Token sequences are stored in a radix tree, and each node owns the cache for the tokens on its path. A new request walks the tree and reuses every matching node, so only the new part of the prompt is computed.
Can I switch between SGLang and vLLM without rewriting my client?
Mostly. Both document OpenAI-compatible APIs, so a client using chat completions needs a new base URL and model name. On 4 December 2026 the quickstarts used port 8000 for vLLM and port 30000 for SGLang. Engine-specific options, such as SGLang's session ID for its session-aware cache, do not carry over.
How do I get a high prefix-cache hit rate on either engine?
Treat your prompt template as a cache key. Put everything that never changes first: system prompt, tool definitions, schemas. Put retrieved context next and the user's question last. Never place a timestamp, request ID or user name near the top, because everything after the first difference is recomputed.
Learn it by building
These free lessons build the mechanisms as toys you can run:
- Serving Engine Internals — PagedAttention, Continuous Batching, Chunked Prefill: vLLM's allocator and scheduler, with a toy continuous batcher.
- Prefix-Cache Serving — RadixAttention and KV Reuse: a toy radix-tree cache and a cache-aware scheduler.
- Self-Hosted Serving Selection — Matching Engine to Hardware and Scale: choosing by hardware, scale and workload.
- Inference Metrics — TTFT, TPOT, ITL, Goodput, P99: the numbers to compare.
- Load Testing LLM APIs — Why k6 and Locust Lie: how to run the test.
Sources
- docs.vllm.ai/en/latest
- docs.vllm.ai/en/latest/getting_started/quickstart
- docs.vllm.ai/en/latest/features/automatic_prefix_caching
- docs.vllm.ai/en/latest/design/prefix_caching
- docs.vllm.ai/en/latest/cli/serve
- github.com/vllm-project/vllm/blob/v0.34.0/vllm/config/cache.py
- github.com/vllm-project/vllm/blob/v0.34.0/vllm/engine/arg_utils.py
- arxiv.org/abs/2312.07104
- docs.sglang.io
- docs.sglang.io/docs/get-started/quickstart
- docs.sglang.io/docs/get-started/install
- docs.sglang.io/docs/advanced_features/radix_eviction_policy
- docs.sglang.io/docs/advanced_features/session_radix_cache
- docs.sglang.io/llms.txt