Phase 03/Memory and scheduling internals/Request scheduling
PHASE 03 · Memory and scheduling internals

Request scheduling

0%
6 h5 subtopics2 lessons · 1 required
In plain English

A busy restaurant kitchen needs a head waiter who decides, every few seconds, which orders go onto the stove next. With a language model server the same role is played by the scheduler. It runs a tiny loop: look at who is waiting, look at how much fridge space (KV memory) is left, decide which new orders to start and which running ones to continue, and if the fridge is full, pull a dish off the stove to make room. Rules like first come first served, or favouring short orders, change who waits and for how long. Concurrent inference is simply many customers in the dining room at once, and the scheduler is what makes that feel fast for everyone.

Why it matters

The scheduler is where throughput, fairness and tail latency are traded off. Most production incidents in LLM serving, such as TTFT spikes under load or requests starving, are scheduling behaviour. Reading vLLM's scheduler in Phase 6 is only possible if you know the policy vocabulary first.

Hiring signal

Skills this topic builds, with the number of the 46 analysed job postings that ask for each. Companies asking: Anthropic, Baseten, Cerebras, Cohere, CoreWeave, Crusoe, Cursor, Databricks, ElevenLabs, Fireworks, Liquid AI, Modal, nvidia, Perplexity, Poolside, Thinking Machines, Together AI, xAI.See all skills →

Learning objectives

  • Describe the per-step scheduler loop in vLLM including waiting and running queues and the token budget.
  • Compare FCFS, shortest-job-first and priority scheduling for TTFT and tail latency.
  • Explain preemption triggers and the recompute versus swap trade-off (vLLM V0 offered both; V1 recomputes only).
  • Extend the batching simulator with a KV block budget and a pluggable scheduling policy.
  • Explain what max-num-seqs, max-num-batched-tokens and the KV watermark do inside the scheduler.

Key concepts

Waiting queue
Requests admitted by the API but not yet given KV blocks; ordered by the policy.
Running queue
Sequences with allocated blocks that receive a token every step.
Token budget
max-num-batched-tokens; the scheduler fills it with decode tokens first, then prefill chunks.
FCFS
First come first served; simple and fair by arrival but lets one long prompt delay many short ones.
Priority scheduling
Requests carry a priority value; higher priority can preempt lower in vLLM when enabled.
Preemption
Removing a running sequence to free blocks for others; vLLM V1 recomputes it when admitted again.
Admission control
Rejecting or queueing requests when the engine is saturated instead of letting latency grow unboundedly.
Goodput
Throughput counting only requests that met their latency SLO, from the DistServe paper.

Subtopics

  1. 1. The scheduler step

    Each iteration vLLM schedules running sequences first, then admits waiting ones while blocks and token budget remain, then runs the model once.

  2. 2. Scheduling policies

    FCFS default, priority with preemption, shortest-first heuristics, and why output length is unknown at admission.

  3. 3. Preemption and recovery

    Watermark checks, choosing a victim (usually most recent), recompute versus swap (V1 only recomputes), and the latency cost to the victim.

  4. 4. Concurrency limits and admission

    max-num-seqs, token budget, HTTP concurrency, queueing at the API layer and backpressure to clients.

  5. 5. Disaggregated prefill and decode

    Running prefill and decode on separate GPUs so each can be scheduled for its own SLO; the DistServe idea and vLLM's KV transfer connector.

Resources

Hands-on lessons

L1

Add a KV budget and scheduling policies to your simulator

CPU · your Macscript120 minrequired

Goal · A scheduler simulator with a block budget, preemption and three swappable policies, reporting TTFT p50/p99 and goodput for each.

  1. 1Import your BlockManager from p3-paged-attention-l1 and the request trace generator from p3-batching-l1.
  2. 2Implement a Scheduler with waiting and running lists and a step() that continues running sequences, then admits waiting ones while blocks and a 2048-token budget remain.
  3. 3On block exhaustion preempt the most recently admitted running sequence, free its blocks and requeue it at the front (recompute).
  4. 4Implement policies as a sort key on the waiting list; FCFS by arrival, shortest-prompt-first, and priority with 20 percent of requests marked high.
  5. 5Run the same 500-request trace under each policy at two arrival rates (light and overloaded).
  6. 6Report TTFT p50 and p99, ITL p99, preemption count and goodput with an SLO of TTFT under 2 seconds.
  7. 7Write a paragraph on which policy you would ship for an interactive chat product and why.
Deliverable
scheduler_sim.py and a results table (policy x load) with the written recommendation.
Check
Under overload, shortest-prompt-first has lower TTFT p50 than FCFS but its p99 for long prompts is worse; preemption count is zero at light load.
L2

Observe scheduling in a live vLLM server

Colab T4 · freeexperiment60 minoptional

Goal · Watch running and waiting queue lengths and preemptions in vLLM metrics while varying max-num-seqs.

  1. 1Start vllm serve Qwen/Qwen2.5-0.5B-Instruct --max-num-seqs 4 --max-model-len 2048 on the T4.
  2. 2Fire 32 concurrent chat requests with asyncio and the openai client, each asking for 200 tokens.
  3. 3Poll /metrics every second and record vllm:num_requests_running, vllm:num_requests_waiting and vllm:num_preemptions_total.
  4. 4Restart with --max-num-seqs 32 and repeat; plot both runs.
Deliverable
A notebook with two time-series plots of running and waiting counts.
Check
With max-num-seqs 4 the waiting count peaks near 28; with 32 it stays near zero.

Build your own

Build your own scheduler policy lab

PythonCPU · your Mac~6 h

Swappable FCFS, shortest-job-first, priority and cache-aware policies over a KV-budgeted simulator, with tail-latency CDFs and goodput so you can argue for a policy with numbers.

The real thing: github.com/sgl-project/sglang/blob/main/python/sglang/srt/managers/schedule_policy.py ↗

How you know a stage passes

Build on your batching simulator (import it or vendor it). Write tests/test_policies.py first; each policy gets a tiny deterministic trace where the admission order is known by hand, plus comparative tests on a seeded 500-request trace that assert ordering relations (SJF mean latency lower than FCFS, and so on). A CLI, lab.py, runs policies and writes tables and plots. Run `pytest -q`; a stage passes when its tests pass and earlier tests still pass.

Stages, in order. Each one leaves you with something that runs.0/6
  1. 01a Policy interface and FCFS
    A Policy class with select(waiting, running, budget) returning the requests to admit this step; FCFS is the first implementation and the simulator logs every admission.
    test · On a 5-request trace with staggered arrivals the admission log lists request ids in strictly increasing arrival order, and pytest asserts it.
  2. 02shortest-job-first and priority with aging
    SJF orders by predicted output length; Priority orders by a user-supplied level plus an aging term so low-priority requests cannot starve.
    test · On a bimodal trace (short and long outputs) SJF mean latency is at least 20 percent lower than FCFS; under Priority no request waits longer than 50 steps with aging enabled, while without aging at least one does.
  3. 03budget-aware versus optimistic admission
    A conservative mode admits only when prompt plus max_tokens fits; an optimistic mode admits on prompt size and relies on preemption.
    test · Conservative mode reports zero preemptions on all traces; optimistic mode reports higher throughput on the same trace and the lab prints the preemption count and the throughput delta.
  4. 04SLO goodput
    The lab computes TTFT and end-to-end p50/p99 and goodput, the rate of requests meeting a TTFT and ITL SLO you pass on the command line.
    test · Goodput is never above throughput on any run, and equals throughput exactly when both SLO thresholds are set to infinity.
  5. 05tail-latency plots at three load levels
    lab.py plot runs every policy at low, medium and high arrival rate and writes a TTFT CDF per load level with one line per policy.
    test · Three PNGs are written; at the high load level FCFS p99 TTFT is higher than SJF p99 TTFT, and a markdown table with all p50/p99 numbers is produced alongside.
  6. 06a cache-aware policy
    Requests carry a prefix id; a longest-prefix-match policy admits requests whose prefix is already resident first, and the simulator charges prefill only for unmatched tokens.
    test · On a trace where 60 percent of requests share one of five 500-token system prompts, the cache-aware policy computes at least 30 percent fewer prefill tokens than FCFS, and the number is in the table.

Knowledge check

QIn what order does the vLLM scheduler consider sequences each step, and why?

Running sequences first so in-flight decodes are never starved, then waiting ones while KV blocks and the token budget allow; this protects ITL.

QWhy is shortest-job-first hard to apply to LLM requests?

Output length is unknown when a request arrives; only prompt length is known, so policies rely on heuristics or predictors.

QWhat happens to a preempted sequence under the recompute policy?

Its KV blocks are freed and it goes back to the waiting queue; when re-admitted its whole prompt and generated tokens are prefilled again.

QWhat does goodput measure that throughput does not?

Only the requests that met their latency SLO count, so a system that is fast on average but misses tail targets gets a lower score.

Completion

  • All 5 subtopics marked done
  • All 1 required lessons done
  • All subtopics marked completed.
  • Lesson l1 simulator produces the policy x load table and the recommendation is written.
  • Can walk through one scheduler step on a whiteboard including preemption.
Topic progress is derived; it reaches 100% when both checks above pass.Next recommended topic →