KatInterview

Design a Batching Service for GPU Model Inference

Machine LearningModel answer
MediumModel answerMachine Learningml-systemsbatchingdistributed-systems

Last updated

Build an online inference system that groups compatible GPU requests into batches to maximize throughput while meeting latency SLAs.

You're building an online inference service that runs machine learning models on GPUs. Client requests arrive individually through an API, but GPUs achieve much better throughput when processing multiple requests simultaneously—batching amortizes fixed overhead like kernel initialization and weight loading across many inputs. However, every request added to a batch forces earlier requests to wait longer. Your system must create batches large enough to be cost-efficient without violating latency requirements.

Design an inference service that:

  • Serves real-time prediction requests through an HTTP/gRPC API
  • Groups compatible requests into efficient batches
  • Distributes batches across a fleet of GPU workers
  • Handles multiple models and versions running simultaneously
  • Optimizes for both throughput (cost efficiency) and latency (SLA compliance)
  • Manages overload conditions, worker failures, and provides operational visibility

Your design should address the API contract, queue architecture, batch formation logic, worker management, scaling decisions, and key trade-offs.

Assumptions & Scope

Define your own assumptions where unstated, but consider this baseline:

  • Latency target: Synchronous API with p95 latency of 200-500ms for single-pass models, or p95 time-to-first-token plus streaming constraints for generative models
  • Heterogeneous traffic: Different models, varying input sizes (text token counts, image dimensions), fluctuating request rates with daily patterns and bursts
  • Shared infrastructure: Multiple teams use the same GPU pool; prevent any one team from monopolizing resources
  • Stateless inference: Predictions don't modify external state, simplifying retry logic
  • GPU instances take 30-60 seconds to become available after provisioning

Important Clarifications

Before designing, establish:

  • Model type: Single forward-pass (classification, embeddings) or autoregressive generation (text completion with unknown output length)?
  • Latency definition: Total request duration, time-to-first-token, or per-token generation speed?
  • Scale parameters: Expected requests/second, typical input size distribution, number of active models?
  • Tenancy model: Strict isolation with quotas, or best-effort fairness?
  • Reliability requirements: Can timed-out requests simply be retried by clients, or must the system guarantee exactly-once processing?
  • Response format: Complete responses only, or streaming token-by-token output?

Hints

Hint 1

Think about what makes two requests 'compatible' for batching—which mismatches waste compute versus which cause correctness errors?

Hint 2

The optimal wait time before flushing a batch depends on your latency budget and the current queue depth—derive it rather than guessing.

Hint 3

Autoregressive models generate tokens iteratively with unpredictable completion times; their batching strategy differs fundamentally from single-pass models.