← Back to Index

11. Scalability

Horizontal Scaling

Add more machines to distribute the load. The standard approach for stateless inference servers.

# Horizontal scaling for inference
Load balancer distributes requests to N replicas
All replicas identical, stateless (model loaded from shared storage)
Auto-scaling based on:
  - CPU/GPU utilization (>70% → add replica)
  - Queue depth (>100 pending requests → add replica)
  - Request latency (p99 > 200ms → add replica)

Partitioning (Sharding)

For stateful components (feature stores, model registry), partition data across nodes.

Queues for Load Smoothing

Use message queues to decouple request arrival from processing. Queues absorb traffic spikes and provide back-pressure.

# Queue-based inference
Request → API Gateway → Request Queue (Kafka/SQS)
Inference workers consume from queue:
  - Pull batch of requests (up to 32)
  - Run batch inference (GPU)
  - Write results to response topic
  - Consumer reads response and returns to client

Benefits:
  - Burst absorption (10K requests arrive, queue buffers)
  - Worker auto-scaling (more workers = faster drain)
  - Retry on failure (messages return to queue)

Auto-Scaling Strategies

StrategyMetricExample
ReactiveCPU > 70% for 2 minutes+2 replicas
ProactiveSchedule: 8 AM daily traffic surge+5 replicas before 8 AM
PredictiveML model predicts trafficScale based on forecast

Load Balancing Algorithms for ML

Scaling the Data Pipeline

Exercise: You are designing a real-time ad bidding ML system that serves 100K QPS peak. The system must evaluate 50 features per request using an ensemble of 3 models. Design the scaling architecture: number of replicas, load balancing strategy, queue design, auto-scaling rules, and how you partition the feature store. Calculate the required infrastructure for peak load.