KatInterview

Batching Service for Autocomplete Inference with Fixed Batch Time

System DesignModel answer
HardModel answerSystem Designsystem-designllm-servingbatching

Last updated

Design a low-latency serving stack that groups autocomplete requests into fixed-duration accelerator batches, sizes the fleet, handles failures, and critiques the fixed-time premise.

You are building the backend for an as-you-type code suggestion feature in an editor extension. Whenever the user stops typing briefly, the extension sends the text surrounding the cursor and waits for one suggested continuation. The model runs on GPU/accelerator hosts. Serving one prompt at a time leaves most of the hardware idle, so the service must combine requests into batches.

To get started, assume this simplification: every batch, of any size up to the cap, takes exactly 250 ms to execute.

Requirements

Functional

  • Each request gets exactly one completion or an explicit error.
  • Clients can cancel (the extension does this when the user resumes typing).
  • Two service tiers exist. Paying users should see lower latency when the system is loaded.

Non-functional (propose different numbers if you justify them)

  • Peak traffic: 40,000 requests/second across the world.
  • Maximum batch size per model replica: 64.
  • End-to-end p99 latency below 600 ms, network included.
  • 99.9% availability. Losing one accelerator host must not produce user-visible errors.

What to cover

  1. The end-to-end request path with each component named.
  2. The batch-forming policy: what causes a batch to launch, and how this differs at low versus high load.
  3. Capacity math: replica count at peak with headroom, and the queueing delay it implies.
  4. How outputs of a finished batch reach the right waiting connection, and what happens if that caller is gone.
  5. Failure handling: replica crash mid-batch, traffic spikes, loss of a region.
  6. Critique the fixed 250 ms premise. What might that number really include? What goes wrong when prompt and output lengths vary widely, and how would you change the design?

Clarifying questions you might raise

  • Is the 250 ms for prefill only, for full generation, or for the entire round trip?
  • Is it the same for batch size 1 and batch size 64, and for short and long prompts?
  • Is there a single model, or several that cannot be batched together?

Hints

Hint 1

Start with throughput per replica (batch size divided by batch time), then divide the peak load by it and add utilization headroom and redundancy across regions.

Hint 2

Under a fixed-cost model, a partial batch costs the same as a full one, so think about when waiting for more requests is pointless. Then add a maximum wait and a deadline-aware drop rule for cancelled or expired requests.

Hint 3

For the critique, separate prefill from decoding: decoding costs one forward pass per output token, so batch time depends on the longest output. Think about iteration-level (continuous) batching and KV-cache memory as the real capacity limit.