Skip to main content
RadixAttention is SGLang’s core attention mechanism that enables automatic detection and reuse of common token sequence prefixes across different requests. This dramatically improves throughput and reduces memory usage for workloads with shared context.

What is RadixAttention?

RadixAttention combines two key innovations:
  1. Radix Tree Data Structure: Organizes KV cache as a tree where common prefixes are shared
  2. Attention Mechanism: Efficiently computes attention over the shared tree structure
This allows multiple requests to share the same cached key-value states for identical token sequences, significantly reducing both memory usage and computation.

The Radix Tree

The RadixCache in python/sglang/srt/mem_cache/radix_cache.py implements a prefix tree (trie) data structure:
Reference: python/sglang/srt/mem_cache/radix_cache.py:117-146

Example Tree Structure

Consider three requests:
  • Request 1: “What is the capital of France?”
  • Request 2: “What is the capital of Germany?”
  • Request 3: “What is the weather today?”
The radix tree would look like:
The shared prefix “What is the” is stored only once in memory and reused by all three requests.

Key Operations

Prefix Matching

When a new request arrives, RadixCache finds the longest cached prefix:
Reference: python/sglang/srt/mem_cache/radix_cache.py:371-441
Prefix matching happens at page granularity when page_size > 1. If page_size=16, prefixes are aligned to 16-token boundaries.

Prefix Insertion

After a request completes, its tokens and KV cache are inserted into the tree:
Reference: python/sglang/srt/mem_cache/radix_cache.py:443-457

Node Splitting

When a match ends in the middle of a node, the node is split:
Reference: python/sglang/srt/mem_cache/radix_cache.py:687-706

Memory Management

Reference Counting

Each node maintains a reference count:
Reference: python/sglang/srt/mem_cache/radix_cache.py:607-620

Eviction Policies

When memory is full, nodes are evicted based on configurable policies:
  • LRU (Least Recently Used): Evicts oldest accessed nodes
  • LFU (Least Frequently Used): Evicts least accessed nodes
  • FIFO (First In First Out): Evicts oldest inserted nodes
  • MRU (Most Recently Used): Evicts most recent nodes
  • FILO (First In Last Out): Stack-based eviction
  • Priority: Evicts based on request priority
Reference: python/sglang/srt/mem_cache/radix_cache.py:578-605

Attention Computation

The RadixAttention layer (python/sglang/srt/layers/radix_attention.py) computes attention over the tree structure:
Reference: python/sglang/srt/layers/radix_attention.py:99-135
The attention backend (FlashInfer, FlashAttention, etc.) handles the actual computation, accessing cached KV states through the indices provided by RadixCache.

Performance Benefits

Memory Savings

With shared prefixes, memory usage scales with unique content rather than total tokens:
  • Without prefix caching: 3 requests × 1000 tokens = 3000 tokens in memory
  • With prefix caching: 800 shared + 3 × 200 unique = 1400 tokens (53% savings)

Computation Savings

Cached KV states don’t need to be recomputed:
  • Prefill time: Only compute attention for new (uncached) tokens
  • TTFT (Time to First Token): Dramatically reduced for requests with cached prefixes
RadixAttention is most effective for workloads with:
  • Long system prompts
  • Few-shot examples
  • Document-based QA
  • Multi-turn conversations

Integration with Scheduler

The scheduler automatically uses RadixCache during request processing:
Reference: python/sglang/srt/managers/schedule_policy.py:182-240

Page Alignment

For efficient memory management, prefixes can be aligned to page boundaries:
Reference: python/sglang/srt/mem_cache/radix_cache.py:110-114
Partial pages at the end of sequences are not cached. This ensures page-granular sharing and efficient memory allocation.

Configuration

Configure RadixAttention behavior:

Limitations

  1. Exact Match Required: Token sequences must match exactly (including special tokens)
  2. Memory Overhead: Tree structure adds small overhead vs flat arrays
  3. Eviction Cost: Finding eviction candidates requires heap operations
  4. Locking Granularity: Entire paths are locked, not individual nodes