Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Model Serving Design

Overview

Model serving is the infrastructure that delivers ML model predictions to applications. Designing a model serving system involves trade-offs between latency, throughput, cost, and reliability. The architecture must handle varying traffic patterns, model updates, and feature lookups.

Serving Architectures

graph TD
    A[Serving Patterns] --> B[Online Real-time]
    A --> C[Batch]
    A --> D[Streaming]
    B --> B1[HTTP/gRPC endpoint]
    B --> B2[Feature lookup → Model → Response]
    C --> C1[Scheduled jobs - daily reports]
    D --> D1[Kafka → Model → Output topic]

Pattern Comparison

PatternLatencyThroughputComplexityUse Case
Online/Synchronous<100msMediumHighSearch, fraud detection
Online/AsynchronousSecondsHighMediumEmail notifications
BatchHoursVery highLowRecommendations, reports
StreamingSecondsHighHighEvent-driven, real-time features

System Design

graph LR
    A[Client] --> B[Load Balancer]
    B --> C[API Gateway]
    C --> D[Feature Service]
    D --> E[Model Server]
    E --> F[Response Cache]
    F --> G[Response]

Key Components

ComponentPurposeOptions
Load BalancerDistribute trafficNginx, ALB, Envoy
API GatewayAuth, rate limiting, routingKong, AWS API Gateway
Feature ServiceFetch features for inferenceFeature store client
Model ServerRun model inferenceTF Serving, TorchServe, Triton, vLLM
CacheAvoid recomputationRedis, Memcached

Model Server Comparison

ServerFrameworkProtocolGPU SupportBest For
TensorFlow ServingTensorFlowgRPC, RESTYesTF models
TorchServePyTorchRESTYesPyTorch models
Triton Inference ServerMulti-frameworkgRPC, RESTYesMixed models, production
vLLMPyTorch (LLM)OpenAI APIYesLLM inference
ONNX RuntimeONNXC++/PythonYesCross-framework
BentoMLMulti-frameworkRESTYesEasy packaging

Triton Inference Server Deep Dive

Triton is the most versatile production model server:

graph TD
    A[Triton Inference Server] --> B[Model Repository]
    B --> C[TF SavedModel]
    B --> D[PyTorch TorchScript]
    B --> E[ONNX Model]
    B --> F[TensorRT Engine]
    A --> G[Dynamic Batching]
    A --> H[Model Pipeline]
    A --> I[Concurrent Model Execution]

Key features:

  • Dynamic batching: Automatically batches incoming requests for GPU efficiency
  • Model ensemble: Chain pre-processing → model → post-processing
  • Concurrent execution: Run multiple models simultaneously on different GPUs
  • Model warmup: Pre-load models to avoid cold start latency

vLLM for LLM Serving

vLLM is the standard for LLM inference:

FeatureDescription
PagedAttentionEfficient KV cache management (inspired by OS virtual memory)
Continuous batchingDynamic request batching for high throughput
Tensor parallelismSplit model across multiple GPUs
QuantizationGPTQ, AWQ, FP8 support
OpenAI-compatible APIDrop-in replacement for OpenAI API

PagedAttention explained: Traditional LLM serving pre-allocates contiguous GPU memory for each request’s KV cache, leading to memory waste. PagedAttention divides memory into fixed-size blocks and maps them non-contiguously, achieving near-optimal memory utilization.

Scaling Strategies

Auto-scaling Configuration

autoscaling_config = {
    "min_replicas": 2,
    "max_replicas": 50,
    "target_cpu_utilization": 70,
    "target_gpu_utilization": 80,
    "target_latency_p99_ms": 200,
    "scale_up_rate": 2,      # Double replicas
    "scale_down_rate": 0.5,   # Halve replicas
    "scale_up_cooldown_sec": 60,
    "scale_down_cooldown_sec": 300,
}

Scaling Dimensions

DimensionHowWhen
HorizontalAdd more replicasTraffic increases
VerticalBigger instances/GPUsModel doesn’t fit in memory
Model parallelismSplit model across GPUsVery large models (LLMs)
Data parallelismSame model, different dataHigh throughput needed

Traffic Management

graph TD
    A[Traffic Spike] --> B[Request Queue]
    B --> C[Load Balancer]
    C --> D[Model Server Pool]
    D --> E{GPU Saturated?}
    E -->|Yes| F[Auto-scale Up]
    E -->|No| G[Process Normally]
    F --> H[New Instances Ready]
    H --> D

Model Optimization for Serving

Optimization Pipeline

graph LR
    A[PyTorch/TF Model] --> B[Export to ONNX]
    B --> C[ONNX Optimization]
    C --> D[TensorRT Compilation]
    D --> E[Quantized Model]
    E --> F[Triton Deployment]
OptimizationSpeedupAccuracy LossEffort
ONNX export2-3×NoneLow
TensorRT3-5×MinimalMedium
INT8 quantization2-4×SmallLow
FP16 inference1.5-2×NegligibleLow
Operator fusion1.5-2×NoneAutomatic
Knowledge distillation2-10×Small-MediumHigh

Request Batching

StrategyDescriptionTrade-off
Static batchingFixed batch sizeSimple, wastes GPU on small batches
Dynamic batchingBatch requests arriving within a time windowBetter utilization, adds latency
Continuous batchingAdd/remove requests from batch mid-generationBest for LLMs, complex
# Dynamic batching configuration (Triton)
model_config = {
    "max_batch_size": 64,
    "preferred_batch_size": [8, 16, 32],
    "max_queue_delay_microseconds": 100000,  # 100ms
}

Caching Strategies

Cache TypeWhat to CacheHit RateInvalidation
Exact matchSame input → same outputLowTTL
SemanticSimilar inputs → cached outputMediumSimilarity threshold
Feature cachePre-computed featuresHighOn feature update
Model cacheLoaded model in GPU memoryN/AOn model update

Semantic Caching for LLMs

graph TD
    A[User Query] --> B[Embed Query]
    B --> C[Search Cache - ANN]
    C --> D{Similarity > Threshold?}
    D -->|Yes| E[Return Cached Response]
    D -->|No| F[Run LLM Inference]
    F --> G[Cache Response]
    G --> H[Return Response]

Multi-Model Serving

StrategyDescriptionWhen to Use
Single model per serverOne model, dedicated resourcesSimple, predictable
Multi-model serverMultiple models on same serverLow-traffic models, cost savings
Model pipelineChain models (preprocess → model → postprocess)Complex workflows
A/B testingRoute % of traffic to different modelsExperimentation

A/B Testing Architecture

graph TD
    A[Request] --> B[Router]
    B --> C[Model A - 90% traffic]
    B --> D[Model B - 10% traffic]
    C --> E[Log Prediction + Metrics]
    D --> E
    E --> F[Statistical Analysis]
    F --> G{Significant Improvement?}
    G -->|Yes| H[Promote Model B]
    G -->|No| I[Keep Model A]

Deployment Patterns

PatternDescriptionRollback SpeedRisk
Blue-GreenTwo identical environments, switch trafficInstantLow
CanaryGradually route traffic to new modelFastLow
ShadowRun new model alongside, compare resultsN/A (no traffic)None
RollingUpdate instances one by oneMediumMedium

Interview Questions

  1. How do you design a low-latency model serving system? Use efficient model format (ONNX, TensorRT), feature caching, request batching, model optimization (quantization), and deployment close to users (edge/CDN). For LLMs, use vLLM with PagedAttention.

  2. How do you handle traffic spikes? Auto-scaling based on latency/CPU/GPU metrics, request queuing with backpressure, pre-warming instances (predictable spikes), caching frequent predictions, and graceful degradation (serve cached/simpler model under extreme load).

  3. What is model server comparison? TF Serving: TensorFlow native. TorchServe: PyTorch native. Triton: multi-framework, GPU optimization, production-grade. vLLM: LLM-specific, PagedAttention. BentoML: easy packaging, developer-friendly.

  4. How do you serve multiple models efficiently? Use Triton’s multi-model serving (share GPU memory), model ensemble pipelines, and dynamic batching. For LLMs, use vLLM’s continuous batching. Route requests to appropriate model based on task type.

  5. How do you handle model versioning in serving? Model registry with versioned artifacts. Serve multiple versions simultaneously (A/B testing). Use canary deployment for new versions. Keep previous version warm for instant rollback.

Summary

Model serving design must balance latency, throughput, and cost. Key decisions include serving pattern (online/batch/streaming), model format, caching strategy, and scaling approach. Production systems typically use a load balancer → API gateway → feature service → model server architecture.

References

  • NVIDIA Triton Inference Server documentation
  • vLLM: Easy, Fast, and Cheap LLM Serving (Kwon et al., 2023)
  • TensorFlow Serving architecture guide
  • BentoML documentation (bentoml.com)
  • Clipper: A Low-Latency Online Prediction Serving System (Crankshaw et al., 2017)

Cross-References