AlgoViz

Serving mixed workloads on an inference cluster

hard 7 min

Prefill computes representations for the prompt and builds key/value state.

Video lesson · Chapter 1 of 4

A long document joins a short queue

0:00 / 3:55

Theory

Read or listen first. About 2 min.

What it is

Prefill computes representations for the prompt and builds key/value state. Decode repeatedly generates a token while reading cached state from previous positions. In a shared pool, a large prefill can compete with ongoing chats. Separating the stages permits independent sizing and scheduling, but the decode pool must receive the right state before it can safely proceed.

Why it exists

Disaggregation is a workload choice. A slow interconnect can cost more than the queueing it saves. Track queue delay, time to first token, gaps during decoding, and the share of requests meeting both latency objectives. Capacity needs explicit bounds for active tokens, resident KV memory and cancellation. Keep each cache allocation tied to a request and tenant through its entire lifetime.

A long document arrives among short conversations

Admission queue → Prefill worker (admitted prompt). Prefill worker → Decode worker (owned KV transfer). Admission queue → Decode worker (decode reservation). Decode worker → Outcome monitor (timing and outcome)
Admission queue → Prefill worker (admitted prompt). Prefill worker → Decode worker (owned KV transfer). Admission queue → Decode worker (decode reservation). Decode worker → Outcome monitor (timing and outcome)

Inside this chapter

Chapter sources
Scheduling and transfer
Group similar work, cap prefill chunks and reserve decode capacity for active users. In a split deployment the router also needs to account for cache locality and transfer bandwidth. A fast prefill worker can still feed an overloaded decoder.

In a shared pool the scheduler mixes prefill (one large burst of compute per new prompt) with decode (many small steps for active chats). Capping prefill chunk size stops one long prompt from freezing every chat for its whole duration. In a split deployment the router additionally picks a decode worker with free KV memory, ideally close on the network, and must account for the transfer: if the link is slow, the handoff can cost more than the queueing it was meant to remove.

Example. A 256 MiB handoff on an effective 8 GiB/s link has a 31.25 ms payload-transfer lower bound before synchronization and queueing.

Another example. A 100,000-token legal document arrives. Chunked prefill processes it in slices interleaved with decode steps, so existing chats see small pauses rather than one long freeze, at the cost of a somewhat later first token for the document.

Transfer floor for the KV handoff

tmin⁡=BKVRlink=256 MiB8 GiB/s=132 s=31.25 mst_{\min}=\frac{B_{\rm KV}}{R_{\rm link}}=\frac{256\ \text{MiB}}{8\ \text{GiB/s}}=\frac{1}{32}\ \text{s}=31.25\ \text{ms}
A lower bound before synchronisation, queueing and allocation are added.
Goodput under latency objectives
Throughput counts tokens or requests completed. Goodput counts successful requests meeting the declared service targets per unit time. Examine short and long workloads separately and include cancellations in reporting.

Throughput rewards a system for finishing work, however late. Goodput only counts requests that met the latency targets users care about, typically time to first token and the gap between tokens. Optimising throughput alone tends to push batch sizes up until individual users feel stutters. Compare deployments on goodput under the same traffic mix, and count cancellations, since a user who gives up is a failure.

Example. Of 100 admitted requests in ten seconds, 80 finish, but only 60 meet both timing limits. This interval delivers six on-target completions per second.

Another example. Raising the batch size from 32 to 64 increases tokens per second by a quarter but pushes inter-token gaps past the limit for a third of chats. Goodput falls even though the throughput chart looks better.

Throughput versus goodput in one ten-second window

MeasureCountPer second
Admitted requests10010
Finished requests (throughput)808
Finished within both TTFT and inter-token limits606 (goodput)
Figures from the example. Report short and long workloads separately.
Isolation and incident analysis
Admission, ownership and transfer completion are correctness boundaries. An allocator must not expose a previous tenant’s state when work is cancelled or a transfer fails. Cloudflare’s 2026 report documents cross-request exposure in one hosted model and motivates fail-closed handoff verification.

KV cache blocks are memory that held one tenant's prompt. If a cancelled or failed request's blocks are handed to the next request without verification, the second request can decode from the wrong state, which may expose another user's content or produce nonsense. Treat admission, block ownership and transfer completion as correctness checks that fail closed, and test them with deliberate cancellations and transfer faults.

Example. In a synthetic test with two labeled requests, cancel the first and confirm the second either receives its own complete state or fails cleanly.

Another example. A chaos test cancels every tenth request at random points during transfer while two labelled tenants send canary prompts. Any reply containing the other tenant's canary string fails the build.

Fail closed at every handoff

A synthetic two-request test checks that a cancelled request's blocks never reach the next one.
KV memory capacity planning
Each active token keeps key and value vectors for every layer, so resident KV memory, not compute, often limits how many requests a decode worker can hold. Estimate bytes per token from the model shape and size admission limits from the memory left after weights.

The leading 2 counts keys and values. Grouped-query attention lowers the head count and hence the memory; quantised KV caches lower the bytes per value. Admission control should reserve memory for each request's expected output length as well as its prompt, or long generations will force evictions mid-reply.

Example. For an illustrative model with 32 layers, 8 KV heads of width 128 and 16-bit values, each token needs 128 KiB, so a 2,048-token prompt holds 256 MiB, the same size as the handoff above.

Illustrative KV budget for one decode worker

QuantityValueHow it is derived
Bytes per token128 KiB2 x 32 layers x 8 heads x 128 x 2 bytes
2,048-token prompt256 MiB2,048 x 128 KiB
KV memory available40 GiBafter weights and overhead, assumed
Resident tokens327,68040 GiB / 128 KiB
Chats at 8,192 tokens each40327,680 / 8,192
Made-up model and memory sizes for illustration. Real numbers depend on the model, precision and runtime.
Cache-aware routing
Requests that share a long prefix, such as the same system prompt or document, can reuse cached KV blocks if they land on the worker that already holds them. The router balances that reuse against load, since sending everything to the warm worker overloads it.

Hash the prefix at block boundaries so partial overlaps can still be reused. Measure the cache hit rate alongside latency; a high hit rate with rising queueing on a few workers means affinity is too strong.

Example. Fifty agents share a 6,000-token tool manual as their prefix. Routing them to two workers that keep it cached avoids recomputing the manual on every request.

Prefer the warm worker unless it is overloaded

The load limit stops cache affinity from creating a hotspot.

How it works

Step through the idea, or press Play.

Step 1 of 4

Admission queue. Admit the long document within budget

Understand it

Worked example

A long document arrives among short conversations

  1. 1

    Admit the long document within budget

    Reserve capacity for prompt tokens, concurrent chats and the expected decode state. Queue or decline a request that cannot fit its declared limits.

  2. 2

    Build request-specific KV state

    The worker computes the prompt and records the eligible destination and completed state size.

  3. 3

    Start only after a valid handoff

    Match request identity, allocation and completion. Batch ready sequences and monitor pauses experienced by existing chats.

  4. 4

    Count requests meeting both targets

    Split transfer time from queueing and token generation. Compare a shared pool and disaggregated deployment under the same traffic mix.

Transfer time floor

tmin⁡=BKV/Rlinkt_{\min}=B_{\rm KV}/R_{\rm link}
At 256 MiB and 8 GiB/s, payload transmission alone takes 0.03125 seconds.

Follow the handoffs

Disaggregation is a workload choice. A slow interconnect can cost more than the queueing it saves. Track queue delay, time to first token, gaps during decoding, and the share of requests meeting both latency objectives. Capacity needs explicit bounds for active tokens, resident KV memory and cancellation. Keep each cache allocation tied to a request and tenant through its entire lifetime.

Long document request through split pools

Decode starts only after the verified handoff. The monitor splits transfer time from queueing.

Shared pool

  • No KV transfer step
  • Simple capacity planning
  • Good when prompts are similar in size

Split prefill and decode pools

  • Long prefills stop stalling active chats
  • Pools sized independently
  • Pays transfer time and needs fail-closed handoffs

Summary

The whole topic in one minute.

A faster first stage can move the bottleneck into transfer or decode and reduce the quality experienced by users.

Disaggregation is a workload choice. A slow interconnect can cost more than the queueing it saves. Track queue delay, time to first token, gaps during decoding, and the share of requests meeting both latency objectives. Capacity needs explicit bounds for active tokens, resident KV memory and cancellation. Keep each cache allocation tied to a request and tenant through its entire lifetime.

Sources