Scaling

Scale AI inference and training safely across traffic, data, model size, reliability, and cost constraints.

A prediction service may begin with a handful of users and later receive thousands of requests during a sale, weather event, product launch, or business deadline. Training pipelines can also grow from one small dataset to terabytes of data and large accelerator clusters. Without a scaling strategy, latency rises, queues grow, requests fail, and costs become unpredictable.

Scaling means increasing or decreasing capacity in response to useful work. The goal is not maximum infrastructure; it is enough capacity, in the right place and at the right time, to satisfy service objectives safely and economically.

First Understand the Workload

QuestionWhy it matters
Is work online, asynchronous, batch, streaming, or edge?Determines whether requests wait, queue, schedule, or run locally
What are the latency and availability objectives?Controls capacity headroom and acceptable queueing
What is normal, peak, and burst traffic?Guides baseline, maximum, and scale-out speed
How large are inputs and outputs?Affects network, memory, parsing, and model execution
How long does one unit of work take?Connects throughput to concurrency and replica count
Does the model require CPU, GPU, TPU, or large memory?Constrains instance types and scheduling
How long do replicas take to become ready?Determines prewarming and autoscaling responsiveness
Can requests be batched, cached, delayed, or dropped?Creates optimization and overload options
What quotas and dependency limits exist?Prevents scaling one tier into a downstream failure
What is the cost per successful outcome?Keeps scaling economically sustainable

Vertical and Horizontal Scaling

Vertical Scaling

Vertical scaling gives one instance more CPU, memory, storage bandwidth, or accelerator capacity. It is simple and sometimes necessary for a model that cannot fit on a smaller machine. However, machine sizes have limits, upgrades may require restarts, one large instance can remain a failure domain, and idle high-end hardware can be expensive.

Horizontal Scaling

Horizontal scaling adds replicas and distributes work through a load balancer or queue. Stateless inference services usually scale this way. It improves capacity and failure tolerance, but requires consistent model and configuration versions, health checks, safe traffic routing, shared or external state, and careful handling of startup and shutdown.

ApproachStrengthLimitation
Scale upSimple and helps models fitFinite sizes, restart risk, and possible idle cost
Scale outFlexible throughput and redundancyDistributed coordination and startup complexity
Scale out by partitionSeparates tenants, models, regions, or data shardsRouting, balancing, and hot-partition risks
Scale to zeroReduces idle costCold starts and unavailable warm capacity

Capacity Fundamentals

Measure throughput per ready replica under representative inputs and concurrency. A rough starting estimate divides peak request rate by sustainable per-replica throughput and then adds redundancy and safety headroom. Validate the result with load tests because preprocessing, payload size, batching, contention, dependency calls, and tail latency make simple arithmetic incomplete.

Output
Illustrative estimate
Peak demand = 240 requests/second
Measured sustainable capacity = 35 requests/second per replica
Raw replicas = ceil(240 / 35) = 7
Add zone redundancy and tested headroom -> choose a higher minimum or maximum as required

Do not treat this estimate as proof; verify p95/p99 latency, errors, saturation, and cost in a load test.

Little's Law provides another useful relationship for a stable system: average concurrency is approximately arrival rate multiplied by average time in the system. It helps estimate in-flight work, but tail latency, burstiness, queue limits, and unstable overload still require direct measurement.

Autoscaling

Autoscaling changes replica count or resources from observed demand. It works best when the signal reflects the actual bottleneck and provides enough time for new capacity to become ready. CPU alone is often insufficient for AI workloads.

SignalWhen usefulCaution
CPU utilizationCPU-bound preprocessing or inferenceCan remain low while GPU, memory, or concurrency is exhausted
GPU or accelerator utilizationCompute-bound model executionUtilization alone may hide memory pressure or queueing
Memory utilizationLarge models or feature payloadsOut-of-memory failure can occur before slow scale-out finishes
Requests or concurrency per replicaSynchronous stateless APIsRequires known sustainable concurrency
Queue depth and oldest-message ageAsynchronous workersScale on backlog age as well as count
Latency or SLO burnUser-impact-aware scalingLagging signals may react after harm begins
Schedule or forecastPredictable daily or event peaksNeeds correction when forecasts are wrong
Custom model signalToken rate, batch fullness, active sequencesMust be stable, timely, and correctly attributed
  • Set a safe minimum for normal traffic and resilience, plus a maximum that respects budget and dependency quotas.
  • Scale out quickly enough for bursts and scale in conservatively to avoid oscillation.
  • Use cooldown or stabilization windows and ignore replicas that are not yet ready when calculating capacity.
  • Prewarm capacity for known peaks and models with slow download, compilation, or accelerator initialization.
  • Drain in-flight work and stop accepting new requests before terminating a replica.
  • Monitor autoscaler decisions, pending capacity, quota failures, and time to readiness.

Load Balancing and Health

A load balancer should route requests only to ready replicas. Liveness checks ask whether a process must restart; readiness checks ask whether it can currently serve traffic. A model server is not ready merely because its web process started—it may still be downloading a model, allocating accelerator memory, compiling kernels, or warming caches.

  • Keep services stateless where practical and store session or job state in an appropriate shared system.
  • Use connection draining and graceful shutdown during scale-in and deployments.
  • Avoid sticky sessions unless the workload truly requires affinity.
  • Distribute replicas across failure domains when availability objectives justify it.
  • Use locality-aware routing when model placement, data residency, or network cost matters.

Queues and Asynchronous Scaling

When users do not need an immediate result, a queue separates request acceptance from model execution. It absorbs bursts, protects workers, and allows scaling from backlog. Return a job ID, expose status, and store the result durably. Queueing does not create infinite capacity: define maximum backlog, message deadlines, retention, priority, and admission rules.

Output
Client -> API validates request -> durable queue -> autoscaled workers -> model inference -> result store -> status callback or polling
                                   |
                                   -> bounded retries -> dead-letter queue -> investigation
  • Make consumers idempotent because messages can be retried or delivered more than once.
  • Scale from queue depth, processing rate, and age of the oldest message.
  • Use bounded retries with backoff and jitter, then isolate poison messages.
  • Separate priority classes so large low-priority jobs cannot starve urgent work.
  • Apply backpressure before the queue becomes an expensive record of missed objectives.

Batching

Batching combines multiple inputs into one model execution and can greatly improve accelerator utilization and throughput. Static batching waits for a fixed group; dynamic batching groups requests within a short window. Larger batches can reduce cost per prediction but consume more memory and increase queueing latency, so tune against tail-latency objectives.

Batching choiceBenefitTrade-off
Larger batchHigher throughput and accelerator efficiencyMore memory and waiting time
Short batch windowLower latencyMay produce underfilled batches
Length-aware groupingLess padding for text or sequence modelsMore queue and scheduling complexity
Offline batch jobEfficient bulk predictionsNo immediate response and requires result management

Caching

Caching avoids repeated computation when requests and results are safely reusable. Cache keys must include every factor that changes the answer, such as normalized input, model version, prompt version, retrieval-index version, tenant or permission context, and relevant configuration. Define expiration, invalidation, encryption, size limits, and protection against cross-tenant leakage.

  • Cache deterministic preprocessing, embeddings, retrieval results, or predictions only when semantics permit it.
  • Do not cache sensitive responses across users or authorization contexts.
  • Prevent cache stampedes with request coalescing, locks, stale-while-revalidate, or jittered expiration.
  • Measure hit rate, latency saved, staleness, memory use, and cost—not hit rate alone.

CPU, GPU, and Accelerator Scaling

Choose hardware from measured model behavior. CPU can be cost-effective for small models and variable traffic. GPUs or other accelerators can improve throughput for compute-intensive models, especially with effective batching, but memory capacity, data transfer, startup, scheduling scarcity, and idle time often dominate design.

  • Measure preprocessing separately so CPU work does not starve the accelerator.
  • Monitor accelerator compute, memory, power, batch size, active sequences, and queue delay together.
  • Use quantization, compilation, optimized runtimes, distillation, or a smaller model only after validating quality.
  • Consider model sharing, multiplexing, or partitioning only with strong tenant, memory, and failure isolation.
  • Checkpoint interruptible training jobs and avoid spot capacity for work that cannot tolerate eviction.

Model Loading and Cold Starts

New replicas may need to download gigabytes, verify checksums, load weights, allocate memory, compile kernels, and run warm-up inference. If scale-out takes minutes, a CPU threshold that reacts after saturation cannot protect a sudden spike.

  • Keep a tested minimum of warm replicas for latency-sensitive services.
  • Place immutable artifacts near compute and use safe local caches when useful.
  • Optimize image and artifact size without weakening reproducibility or security.
  • Run readiness only after model validation and representative warm-up complete.
  • Use predictive or scheduled scaling for known events and reserve scarce accelerators early.
  • Measure startup distributions and include them in capacity and deployment planning.

Overload Protection

Autoscaling cannot respond instantly and quotas or budgets impose ceilings. A production service needs controlled behavior when demand exceeds capacity.

ControlPurpose
Admission controlReject work before an overloaded system collapses
Rate limits and quotasProtect capacity and allocate fair usage
Concurrency limitsBound in-flight memory and compute
Timeouts and deadlinesStop work after it can no longer help the caller
Load sheddingDrop lower-priority requests to preserve critical paths
Circuit breakersAvoid repeatedly calling a failing dependency
Graceful degradationUse a smaller model, cached result, rules, async mode, or human path
BackpressureSlow producers or reject work when downstream capacity is full

Return explicit retry guidance where appropriate and ensure retries have backoff and jitter. Immediate synchronized retries can amplify an outage into a retry storm.

Scaling Dependencies

An API can add replicas faster than its database, feature store, vector database, model provider, queue, network address pool, or quota can expand. Model the entire request path and define per-dependency connection pools, concurrency, timeouts, quotas, caches, and failure behavior. Scaling the front end into a fixed downstream limit makes failure arrive faster.

Scaling Training and Data Pipelines

WorkloadScaling methodMain concerns
Data preparationPartitioned batch or stream processingSkew, shuffles, small files, retries, and data quality
Distributed trainingData, model, or pipeline parallelismCommunication, synchronization, checkpointing, and convergence
Hyperparameter tuningParallel independent trialsSearch budget, early stopping, quotas, and experiment tracking
Batch predictionPartition inputs among workersIdempotency, partial failures, output lineage, and reconciliation
Many modelsPartition by tenant, segment, or modelScheduling, hot models, isolation, and registry consistency

More workers do not guarantee proportional speedup. Data skew, serialization, network communication, synchronization, storage throughput, and stragglers can dominate. Measure stage-level throughput and stop scaling when marginal time saved no longer justifies cost.

Generative AI Scaling

  • Track requests, input and output tokens, active sequences, time to first token, tokens per second, and end-to-end latency.
  • Separate short interactive requests from long generations to reduce head-of-line blocking.
  • Apply token, context, concurrency, tool-call, and per-user budgets—not request limits alone.
  • Use prompt and retrieval caching only with correct model, index, tenant, permission, and freshness keys.
  • Route work to smaller or specialized models when measured quality allows it.
  • Plan for provider quotas, regional failures, model-version changes, and a safe degraded experience.

Multi-Model and Multi-Tenant Serving

Serving many models or tenants on shared infrastructure improves utilization but creates noisy-neighbor, memory, routing, privacy, and blast-radius risks. Enforce authorization before selecting a model or feature set, apply tenant quotas, isolate sensitive workloads, and monitor utilization and quality by model and tenant where appropriate. Do not load unlimited models into one process without eviction and capacity controls.

Load and Resilience Testing

TestQuestion answered
BaselineWhat can one replica sustain at acceptable latency and quality?
LoadDoes the system meet objectives at expected peak traffic?
StressWhere is the limit and does failure remain controlled?
SpikeCan it survive a sudden burst before autoscaling responds?
SoakDo memory leaks, queue growth, thermal effects, or cost emerge over time?
Scale-outHow long until new replicas become ready and useful?
Dependency failureDo timeouts, circuits, fallbacks, and queues protect the service?
Zone or node lossDoes remaining capacity handle failover without collapse?
Scale-inAre in-flight requests drained without loss or duplication?

Use representative payload sizes, sequence lengths, feature calls, cache-hit ratios, tenant mix, model versions, and arrival patterns. Report throughput together with p95 and p99 latency, errors, saturation, queue age, quality, and cost.

Monitoring Scaling

  • Demand: arrival rate, concurrency, payload or token volume, batch size, and traffic mix.
  • Capacity: ready and desired replicas, pending workers, accelerator availability, and quota headroom.
  • Performance: throughput, p50/p95/p99 latency, time to first token, queue age, and deadline misses.
  • Saturation: CPU, memory, accelerator, connections, threads, file descriptors, and downstream limits.
  • Autoscaler: trigger signal, decisions, cooldown, readiness delay, oscillation, and scale failures.
  • Reliability: rejected work, retries, duplicate jobs, fallbacks, errors, and dropped requests.
  • Economics: cost per request, token, batch, tenant, model, and successful outcome.

Cost-Aware Scaling

Scaling and cost are one design problem. Maintain enough warm capacity to meet objectives, then use autoscaling, batching, caching, scheduling, and workload-specific hardware to improve utilization. A cheaper instance that requires many replicas or misses objectives may cost more overall. Track cost per useful outcome and set maximum capacity, budgets, and anomaly alerts.

  • Separate steady baseline capacity from burst capacity.
  • Schedule fault-tolerant batch work during lower-cost or lower-demand periods where practical.
  • Use interruptible capacity only with checkpoints, idempotency, and a recovery plan.
  • Remove idle endpoints and development resources, but do not scale critical services below tested resilience needs.
  • Include data transfer, storage, logging, provider calls, and engineering operations in total cost.

Common Scaling Mistakes

MistakeConsequenceBetter approach
Scale only on CPUGPU, memory, concurrency, or queues saturate unnoticedUse the signal closest to the real bottleneck
No minimum warm capacityCold starts violate interactive latencyKeep tested baseline or prewarm before peaks
Unlimited queueWork becomes stale while cost and delay growSet admission, age, priority, and backlog limits
Immediate retriesFailure becomes a retry stormUse deadlines, bounded exponential backoff, and jitter
No readiness checkTraffic reaches replicas before models loadBecome ready only after validation and warm-up
Ignore dependenciesAutoscaling overwhelms databases or providersPlan end-to-end quotas and concurrency
Assume more GPUs always helpCommunication and data bottlenecks waste capacityProfile and measure scaling efficiency
Optimize throughput onlyTail latency and user experience degradeTest throughput, percentiles, quality, and cost together
Scale in abruptlyIn-flight work is lost or duplicatedDrain gracefully and make operations idempotent

Practical Exercise

  • Benchmark one prediction-service replica with representative inputs and increasing concurrency.
  • Identify the first bottleneck using latency percentiles, errors, CPU, memory, accelerator, and dependency telemetry.
  • Estimate peak replica needs, then validate them with a load test and zone-failure headroom.
  • Configure autoscaling from concurrency or queue age and measure time from trigger to ready capacity.
  • Add bounded queues, admission limits, timeouts, retry backoff, graceful shutdown, and a degraded mode.
  • Compare no batching with several batch sizes and windows for throughput, tail latency, memory, quality, and cost.
  • Run spike, soak, dependency-failure, scale-out, and scale-in tests.
  • Document objectives, limits, quotas, scaling rules, dashboards, alerts, and incident runbooks.

Production Scaling Checklist

Output
[ ] Online, async, batch, streaming, or edge workload and objectives defined
[ ] Normal, peak, burst, payload, model, accelerator, startup, and dependency behavior measured
[ ] Per-replica throughput, concurrency, tail latency, saturation, quality, and cost benchmarked
[ ] Vertical, horizontal, partition, batching, caching, and queue choices justified
[ ] Autoscaling signal, target, minimum, maximum, cooldown, and readiness delay tested
[ ] Load balancing, readiness, liveness, draining, statelessness, and failure-domain placement verified
[ ] Admission, rate, concurrency, deadline, retry, circuit, backpressure, and degradation controls enforced
[ ] Database, feature store, queue, provider, network, and quota capacity included
[ ] Load, stress, spike, soak, dependency, failover, scale-out, and scale-in tests pass
[ ] Demand, capacity, performance, saturation, autoscaler, reliability, and unit cost monitored