---
title: "Serving Engine Internals: vLLM vs. SGLang vs. llama.cpp | Yuvraj Garg"
seo_title: "Serving Engine Internals: vLLM vs SGLang vs llama.cpp | Yuvraj Garg"
slug: "serving-engine-internals"
canonical_url: "https://www.yuvraj97.com/deep-dives/serving-engine-internals"
raw_markdown_url: "https://www.yuvraj97.com/deep-dives/serving-engine-internals.md"
author: "Yuvraj Garg"
author_title: "AI Systems & Infrastructure Architect"
author_linkedin: "https://www.linkedin.com/in/yuvraj97"
article_section: "Runtime Engines"
date_published: "2025-02-14"
date_modified: "2025-02-28"
reading_time_minutes: 6
target_audience: ["AI Agents", "LLM Inference Engineers", "Infrastructure Leads"]
keywords:
  - "vLLM vs SGLang"
  - "llama.cpp production"
  - "RadixAttention"
  - "PagedAttention"
  - "CUDA Graph hang"
  - "KV cache fragmentation"
  - "FlashInfer leak"
metrics: "Comparative audit across 10 production model architectures"
---

# Serving Engine Internals: vLLM vs. SGLang vs. llama.cpp

> Source-level comparison of vLLM, SGLang, and llama.cpp across 10 models: quantization tradeoffs, memory behavior, cold starts, and which engine wins for which workload.

- **Human Web View:** [https://www.yuvraj97.com/deep-dives/serving-engine-internals](https://www.yuvraj97.com/deep-dives/serving-engine-internals)
- **Raw Markdown URI:** [https://www.yuvraj97.com/deep-dives/serving-engine-internals.md](https://www.yuvraj97.com/deep-dives/serving-engine-internals.md)

---

## TL;DR & Core Architectural Milestones
- **Engine Comparison:** Evaluated vLLM, SGLang, and llama.cpp across 10 production model deployments, analyzing quantization tradeoffs, memory leaks, and parallelization capabilities.
- **vLLM Snapshot Bug:** Discovered that vLLM's default CUDA Graph capture caused infinite hangs upon container snapshot restoration. Fixed by forcing eager execution (`enforce_eager=True`).
- **SGLang Memory Leak:** Identified and mitigated a slow VRAM leak in SGLang's FlashInfer allocation during tensor parallel sharding by capping prefill sizes and forcing PyTorch caching allocator garbage collection.
- **llama.cpp Limitations:** Built a custom source-compiled Docker pipeline for llama.cpp to support new GGUF architectures, but determined it is unsuitable for high-concurrency production due to its lack of dynamic continuous batching.

---

## 1. Engine Architecture & Feature Comparison

| Architectural Dimension | vLLM (v0.7.2) | SGLang (v0.4.3) | llama.cpp (b8765) |
| :--- | :--- | :--- | :--- |
| **Primary Backend** | PyTorch / Triton / C++ Kernels | PyTorch / FlashInfer / Triton | Pure C / C++ (GGML) |
| **Quantization Formats** | FP8, AWQ, GPTQ, INT4 | FP8, AWQ, GPTQ | GGUF (Q4_K_M, IQ4_NL, etc.) |
| **Attention Engine** | PagedAttention, FlashAttention-2 | RadixAttention, FlashInfer | Ring Buffer, FlashAttention |
| **KV Cache Reuse** | Block-level Prefix Caching | Radix Tree Cache (dynamic LRU) | Smart KV Cache shift |
| **Multi-GPU Parallelism** | TP + PP (Highly stable) | TP (Stable), PP (Experimental) | Row/Column splitting (Single node) |
| **Continuous Batching** | Iteration-level scheduling | Native continuous batching | Limited slot-based batching |

---

## 2. When to Use Which Engine

### 1. vLLM: High-Concurrency Multi-GPU Throughput
vLLM is the gold standard for high-throughput multi-GPU production clusters. Its PagedAttention algorithm eliminates internal memory fragmentation, while tensor parallelism is rock solid across multi-node setups.
- **Critical Quirk:** When taking Linux CRIU snapshots on Modal, vLLM's CUDA Graph warmup captures hardware-bound pointer offsets that hang indefinitely upon restore. Always pass `enforce_eager=True` when snapshotting.

### 2. SGLang: Multi-Turn Agents & Structured JSON
SGLang's RadixAttention retains KV caches across multi-turn agent conversations via an LRU radix tree. For agentic workflows with long system prompts, it delivers up to **3x faster time-to-first-token (TTFT)** than vLLM.
- **Critical Quirk:** FlashInfer memory workspaces slowly fragment VRAM during long-running tensor-parallel sessions. Mitigate by setting `--mem-fraction-static 0.85`.

### 3. llama.cpp: Edge, CPU, & Heterogeneous Hardware
llama.cpp is unmatched for low-resource environments and CPU execution using GGUF quantization. However, it lacks dynamic continuous iteration batching, making it suboptimal for multi-user cloud serving.

---

## 3. Frequently Answered Architecture Questions (FAQ)

### Q: What is the core difference between vLLM, SGLang, and llama.cpp?
vLLM excels at multi-GPU continuous batching and throughput; SGLang wins on agentic workflows and structured outputs due to RadixAttention; llama.cpp is the premier choice for GGUF quantization on CPU and consumer GPUs.

### Q: Why does vLLM hang on memory snapshot restore?
vLLM's CUDA Graph capture records GPU execution addresses that become invalid when restored on a new container instance. Setting `enforce_eager=True` bypasses CUDA Graphs and allows clean CRIU snapshots.

### Q: How does RadixAttention in SGLang improve latency?
Instead of discarding KV caches after each inference pass, SGLang stores cached token prefixes in a radix tree. When a client sends a multi-turn query sharing the same prefix, the engine reuses the KV cache directly, bypassing prompt prefill compute.
